Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條
本文實(shí)例為大家分享了Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下
需求
上傳圖片時(shí)在圖片上增加蒙層,蒙層隨著上傳的大小由下自上逐漸縮短。
分析
1、用xml文件畫一個(gè)正方形的shape
2、利用ClipdDrawable來實(shí)現(xiàn)圖片的動(dòng)態(tài)剪切
3、使用AsynTask來控制圖片的上傳,然后動(dòng)態(tài)的改變ClipDrawable.setLevel()方法中的值,這樣基本就能達(dá)到圖片上傳蒙層的效果。
其中,本文中,在將圖片數(shù)據(jù)流寫向網(wǎng)絡(luò)時(shí),上傳進(jìn)度的值是根據(jù)正向的輸出流的速度來判斷的。也就是說不是服務(wù)器接收圖片的速度,即使進(jìn)度條表示圖片已完全上傳,也只是表示將圖片上傳到網(wǎng)絡(luò)的大小。
實(shí)現(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" />
第一個(gè)是放縮略圖的,第一個(gè)是放進(jìn)度條的,我們可以將它們放在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) { ? ? ? ? ? ? //蒙層進(jìn)度條 ? ? ? ? ? ? (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); ? ? ? ? } ? ? }
上傳進(jìn)度條的監(jiān)聽需要自己寫一個(gè)。
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android ProgressDialog用法之實(shí)現(xiàn)app上傳文件進(jìn)度條轉(zhuǎn)圈效果
- Android Volley擴(kuò)展實(shí)現(xiàn)支持進(jìn)度條的文件上傳功能
- Android實(shí)現(xiàn)文件上傳和下載倒計(jì)時(shí)功能的圓形進(jìn)度條
- Android上傳文件到服務(wù)端并顯示進(jìn)度條
- Android帶進(jìn)度條的文件上傳示例(使用AsyncTask異步任務(wù))
- Android頁面中引導(dǎo)蒙層的使用方法詳解
- Android實(shí)現(xiàn)新手引導(dǎo)半透明蒙層效果
- Android 新手引導(dǎo)蒙層效果實(shí)現(xiàn)代碼示例
相關(guān)文章
Android實(shí)現(xiàn)計(jì)算器(計(jì)算表達(dá)式/計(jì)算小數(shù)點(diǎn)以及括號(hào))
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)計(jì)算器功能,計(jì)算表達(dá)式,能計(jì)算小數(shù)點(diǎn)以及括號(hào),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09Android中使用Matrix控制圖形變換和制作倒影效果的方法
這篇文章主要介紹了Android中使用Matrix控制圖形變換和制作倒影效果的方法,用Matrix來作矩陣變化十分強(qiáng)大,文中的制作倒影的例子便是一個(gè)十分巧妙的運(yùn)用,需要的朋友可以參考下2016-04-04360瀏覽器文本框獲得焦點(diǎn)后被android軟鍵盤遮罩該怎么辦
最近接了個(gè)項(xiàng)目,項(xiàng)目需求是這樣的,站點(diǎn)上篩選按鈕點(diǎn)擊后彈出層(fixed),當(dāng)輸入框獲取焦點(diǎn)以后彈出系統(tǒng)自帶的軟鍵盤,在android上十款瀏覽器挨個(gè)測試比對,發(fā)現(xiàn)在360瀏覽器彈出鍵盤以后獲取焦點(diǎn)的文本框被軟鍵盤覆蓋了,下面分享我的解決辦法2015-12-12Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實(shí)例詳解
這篇文章主要介紹了Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08Android插件化-RePlugin項(xiàng)目集成與使用詳解
這篇文章主要介紹了Android插件化-RePlugin項(xiàng)目集成與使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11android小動(dòng)畫:不斷擴(kuò)散的圓點(diǎn)
這篇文章介紹了如何實(shí)現(xiàn)android小動(dòng)畫:不斷擴(kuò)散的圓點(diǎn),文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,下面的實(shí)例代碼,大家可以看看2021-11-11Android實(shí)現(xiàn)圓形圖片或者圓角圖片
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圓形圖片或者圓角圖片的代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06