最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條

 更新時(shí)間:2022年09月19日 09:53:10   作者:青人  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下

需求

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

分析

1、用xml文件畫(huà)一個(gè)正方形的shape
2、利用ClipdDrawable來(lái)實(shí)現(xiàn)圖片的動(dòng)態(tài)剪切
3、使用AsynTask來(lái)控制圖片的上傳,然后動(dòng)態(tài)的改變ClipDrawable.setLevel()方法中的值,這樣基本就能達(dá)到圖片上傳蒙層的效果。

其中,本文中,在將圖片數(shù)據(jù)流寫(xiě)向網(wǎng)絡(luò)時(shí),上傳進(jìn)度的值是根據(jù)正向的輸出流的速度來(lái)判斷的。也就是說(shuō)不是服務(wù)器接收?qǐng)D片的速度,即使進(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)聽(tīng)需要自己寫(xiě)一個(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);
? ? }
}

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

林甸县| 嵩明县| 江永县| 安阳市| 诸暨市| 湾仔区| 文水县| 渝中区| 勃利县| 德昌县| 靖宇县| 绥化市| 远安县| 安康市| 天峻县| 龙川县| 玉屏| 抚州市| 汕尾市| 南丹县| 壶关县| 乐亭县| 沙田区| 那曲县| 乳源| 葫芦岛市| 沈丘县| 马龙县| 荣成市| 大洼县| 丹阳市| 喀喇| 黎平县| 青铜峡市| 乌兰察布市| 肃南| 普洱| 兴山县| 莎车县| 霍山县| 吉林市|