網站首頁 編程語言 正文
本文實例為大家分享了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
相關推薦
- 2022-05-26 Nginx多個前端服務配置方式詳解_nginx
- 2022-05-10 FactoryBean配置文件定義的 類型 調用時返回 不同的類型
- 2022-08-30 cvc-complex-type.2.4.a: 發現了以元素 ‘base-extension‘ 開頭
- 2023-04-02 使用Pytorch如何完成多分類問題_python
- 2022-05-24 淺談C#中Action和Func回調的常用方式_C#教程
- 2023-01-05 Python中glob類的使用方法_python
- 2022-10-25 python繪圖之坐標軸的超詳細講解_python
- 2022-06-02 詳解Python?flask的前后端交互_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支