Android實現(xiàn)圖片上傳蒙層進度條
本文實例為大家分享了Android實現(xiàn)圖片上傳蒙層進度條的具體代碼,供大家參考,具體內(nèi)容如下
需求
上傳圖片時在圖片上增加蒙層,蒙層隨著上傳的大小由下自上逐漸縮短。
分析
1、用xml文件畫一個正方形的shape
2、利用ClipdDrawable來實現(xiàn)圖片的動態(tài)剪切
3、使用AsynTask來控制圖片的上傳,然后動態(tài)的改變ClipDrawable.setLevel()方法中的值,這樣基本就能達到圖片上傳蒙層的效果。
其中,本文中,在將圖片數(shù)據(jù)流寫向網(wǎng)絡(luò)時,上傳進度的值是根據(jù)正向的輸出流的速度來判斷的。也就是說不是服務(wù)器接收圖片的速度,即使進度條表示圖片已完全上傳,也只是表示將圖片上傳到網(wǎng)絡(luò)的大小。
實現(xiàn)
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中表示上下層的關(guān)系。
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); ? ? ? ? } ? ? }
上傳進度條的監(jiān)聽需要自己寫一個。
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); ? ? } }
好了,以上步驟就差不多完成了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實現(xiàn)計算器(計算表達式/計算小數(shù)點以及括號)
這篇文章主要為大家詳細介紹了Android實現(xiàn)計算器功能,計算表達式,能計算小數(shù)點以及括號,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09Android中使用Matrix控制圖形變換和制作倒影效果的方法
這篇文章主要介紹了Android中使用Matrix控制圖形變換和制作倒影效果的方法,用Matrix來作矩陣變化十分強大,文中的制作倒影的例子便是一個十分巧妙的運用,需要的朋友可以參考下2016-04-04360瀏覽器文本框獲得焦點后被android軟鍵盤遮罩該怎么辦
最近接了個項目,項目需求是這樣的,站點上篩選按鈕點擊后彈出層(fixed),當輸入框獲取焦點以后彈出系統(tǒng)自帶的軟鍵盤,在android上十款瀏覽器挨個測試比對,發(fā)現(xiàn)在360瀏覽器彈出鍵盤以后獲取焦點的文本框被軟鍵盤覆蓋了,下面分享我的解決辦法2015-12-12Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實例詳解
這篇文章主要介紹了Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實例詳解的相關(guān)資料,希望通過本文能幫助大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08