日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android實現圖片上傳蒙層進度條_Android

作者:青人 ? 更新時間: 2022-11-09 編程語言

本文實例為大家分享了Android實現圖片上傳蒙層進度條的具體代碼,供大家參考,具體內容如下

需求

上傳圖片時在圖片上增加蒙層,蒙層隨著上傳的大小由下自上逐漸縮短。

分析

1、用xml文件畫一個正方形的shape
2、利用ClipdDrawable來實現圖片的動態剪切
3、使用AsynTask來控制圖片的上傳,然后動態的改變ClipDrawable.setLevel()方法中的值,這樣基本就能達到圖片上傳蒙層的效果。

其中,本文中,在將圖片數據流寫向網絡時,上傳進度的值是根據正向的輸出流的速度來判斷的。也就是說不是服務器接收圖片的速度,即使進度條表示圖片已完全上傳,也只是表示將圖片上傳到網絡的大小。

實現

1、定義正方形的shape,drawable\mask.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:shape="rectangle">
? ? <!--大小-->
? ? <size android:height="82dp" android:width="82dp"/>
? ? <!--<solid android:color="#3f000000" />-->
? ? <solid android:color="#ffff4444"/>
</shape>

2、定義ClipDrawable由下自上的方式剪切,drawable\clip.xml

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:drawable="@drawable/bg_feedback_mask"
? ? android:clipOrientation="vertical"
? ? android:gravity="top"/>

3、在主界面中定義ImageView,將clip.xml引入

<ImageView
? android:id="@+id/thumbnail"
? android:layout_width="match_parent"
? android:layout_height="match_parent"
? android:scaleType="fitXY" />

<ImageView
? android:id="@+id/picture_upload_progress"
? android:layout_width="match_parent"
? android:layout_height="match_parent"
? android:src="@drawable/bg_feedback_clip"
? android:visibility="gone" />

第一個是放縮略圖的,第一個是放進度條的,我們可以將它們放在FrameLayout中表示上下層的關系。

4、AsynTask處理上傳圖片

private class UploadPictureTask extends AsyncTask<String, Integer, UploadPictureResult> {
?
? ? ? ? private String filePath;
? ? ? ? private CountingTypedFile.ProgressListener listener;
?
? ? ? ? public UploadPictureTask(String filePath) {
? ? ? ? ? ? this.filePath = filePath;
? ? ? ? }
?
? ? ? ? @Override
? ? ? ? protected void onPreExecute() {
?
? ? ? ? ? ? uploadProgressImageView.setVisibility(View.VISIBLE);
? ? ? ? ? ? uploadProgressImageView.getDrawable().setLevel(MAX_LEVEL);
? ? ? ? ? ? super.onPreExecute();
? ? ? ? }
?
? ? ? ? @Override
? ? ? ? protected UploadPictureResult doInBackground(String... params) {
?
? ? ? ? ? ? File file = new File(filePath);
? ? ? ? ? ? fileSize = file.length();
? ? ? ? ? ? listener = new CountingTypedFile.ProgressListener() {
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void transferred(long num) {
? ? ? ? ? ? ? ? ? ? publishProgress((int) ((num / (float) fileSize) * NUM_100));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ? ? ? UploadPictureResult uploadPictureResult = uploadImage(id, new CountingTypedFile("multipart/form-data", file, listener)
? ? ? ? ? ? return uploadPictureResult;
? ? ? ? }
?
?
? ? ? ? @Override
? ? ? ? protected void onProgressUpdate(Integer... values) {
? ? ? ? ? ? //蒙層進度條
? ? ? ? ? ? (uploadProgressImageView.getDrawable()).setLevel(MAX_LEVEL - values[0] * NUM_100);
? ? ? ? }
?
? ? ? ? @Override
? ? ? ? protected void onPostExecute(UploadPictureResult uploadImageResult) {
? ? ? ? ? ? findViewById(R.id.send_feedback).setVisibility(View.VISIBLE);
? ? ? ? ? ? if (exception != null || uploadImageResult == null) {
? ? ? ? ? ? ? ? Toast.makeText(FeedbackActivity.this, "上傳失敗", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? Toast.makeText(FeedbackActivity.this, "上傳成功", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? pictureUrl = uploadImageResult.getUrl();
? ? ? ? ? ? super.onPostExecute(uploadImageResult);
? ? ? ? }
? ? }

上傳進度條的監聽需要自己寫一個。

public class CountingTypedFile extends TypedFile {
?
? ? private static final int BUFFER_SIZE = 4096;
?
? ? private final ProgressListener listener;
?
? ? /**
? ? ?* Constructs a new typed file.
? ? ?*
? ? ?* @param mimeType
? ? ?* @param file
? ? ?* @throws NullPointerException if file or mimeType is null
? ? ?*/
? ? public CountingTypedFile(String mimeType, File file, ProgressListener listener) {
? ? ? ? super(mimeType, file);
? ? ? ? this.listener = listener;
? ? }
?
? ? @Override
? ? public void writeTo(OutputStream out) throws IOException {
? ? ? ? byte[] buffer = new byte[BUFFER_SIZE];
? ? ? ? FileInputStream in = new FileInputStream(super.file());
? ? ? ? long total = 0;
? ? ? ? try {
? ? ? ? ? ? int length;
? ? ? ? ? ? while ((length = in.read(buffer)) != -1) {
? ? ? ? ? ? ? ? total += length;
? ? ? ? ? ? ? ? this.listener.transferred(total);
? ? ? ? ? ? ? ? out.write(buffer, 0, length);
? ? ? ? ? ? }
? ? ? ? } finally {
? ? ? ? ? ? in.close();
? ? ? ? }
? ? }
? ? public interface ProgressListener {
? ? ? ? void transferred(long num);
? ? }
}

好了,以上步驟就差不多完成了。

原文鏈接:https://blog.csdn.net/fumier/article/details/49976729

欄目分類
最近更新