android自定義ImageView仿圖片上傳示例
看下效果圖
主要看下自定義view 代碼
public class ProcessImageView extends ImageView{ private Context context; private Paint paint; private LogUtil log=LogUtil.getInstance(); int progress = 0; private boolean flag; public ProcessImageView(Context context) { super(context); } public ProcessImageView(Context context, AttributeSet attrs) { this(context, attrs,0); } public ProcessImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context=context; paint=new Paint(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setAntiAlias(true); //消除鋸齒 paint.setStyle(Paint.Style.FILL); //設(shè)置paint為實(shí)心, Paint.Style.STROKE為空心 paint.setColor(Color.parseColor("#70000000")); //設(shè)置為半透明 canvas.drawRect(0,0,getWidth(),getHeight()-getHeight()*progress/100,paint); //這里getWidth() 獲取的是image對(duì)象寬高度 xml值*2 paint.setColor(Color.parseColor("#00000000"));// 全透明 canvas.drawRect(0, getHeight() - getHeight() * progress / 100, getWidth(), getHeight(), paint); if(!flag){ paint.setTextSize(30); paint.setColor(Color.parseColor("#FFFFFF")); paint.setStrokeWidth(2); Rect rect = new Rect(); paint.getTextBounds("100%", 0, "100%".length(), rect);// 確定文字的寬度 canvas.drawText(progress + "%", getWidth() / 2 - rect.width() / 2, getHeight() / 2, paint); } } public void setProgress(int progress) { this.progress = progress; if(progress==100){ flag=true; } postInvalidate(); } }
里面代碼很詳細(xì)了。
然后看下 Activity代碼
public class MainActivity extends AppCompatActivity { ProcessImageView processImageView =null; int progress=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); processImageView=(ProcessImageView) findViewById(R.id.image); //模擬圖片上傳進(jìn)度 new Thread(new Runnable() { @Override public void run() { while (true){ if(progress==100){//圖片上傳完成 return; } progress++; processImageView.setProgress(progress); try{ Thread.sleep(200); //暫停0.2秒 } catch (InterruptedException e){ e.printStackTrace(); } } } }).start(); } }
下面來(lái)詳細(xì)介紹view代碼。
首先從圖中可以看到 中間有個(gè)參數(shù)變化,這個(gè)進(jìn)度值不斷變化,我們?cè)賏ctivity 中使用了一個(gè)線(xiàn)程 ,每隔0.2 秒會(huì)增加progress這個(gè)值,然后通過(guò) processImageView.setProgress(progress); 改變view類(lèi)中 progress重繪制這個(gè)定義view.
然后看下自定義view 類(lèi),主要onDraw()方法中.
繪制中分為三部分,
第一部分為上部分半透明區(qū)域
第二部分為下部分全透明區(qū)域
第三部分就是中間的progress值變化
先看第一個(gè)部分畫(huà)出上部分半透明,
paint.setAntiAlias(true); //消除鋸齒 paint.setStyle(Paint.Style.FILL); //設(shè)置paint為實(shí)心, Paint.Style.STROKE為空心 paint.setColor(Color.parseColor("#70000000")); //設(shè)置為半透明 canvas.drawRect(0,0,getWidth(),getHeight()-getHeight()*progress/100,paint);
第二部分畫(huà)出下面透明區(qū)域
paint.setColor(Color.parseColor("#00000000"));// 全透明 canvas.drawRect(0, getHeight() - getHeight() * progress / 100, getWidth(), getHeight(), paint);
第三部分動(dòng)態(tài)改變字符串
if(!flag){ paint.setTextSize(30); paint.setColor(Color.parseColor("#FFFFFF")); paint.setStrokeWidth(2); Rect rect = new Rect(); paint.getTextBounds("100%", 0, "100%".length(), rect);// 確定文字的寬度 canvas.drawText(progress + "%", getWidth() / 2 - rect.width() / 2, getHeight() / 2, paint); }
源碼地址 http://xiazai.jb51.net/201701/yuanma/ProcessImageDemo_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義ImageView實(shí)現(xiàn)自動(dòng)放大縮小動(dòng)畫(huà)
- Android自定義圓角ImageView控件
- Android 自定義imageview實(shí)現(xiàn)圖片縮放實(shí)例詳解
- Android自定義控件之圓形、圓角ImageView
- Android自定義GestureDetector實(shí)現(xiàn)手勢(shì)ImageView
- Android自定義ImageView實(shí)現(xiàn)在圖片上添加圖層效果
- Android通過(guò)自定義ImageView控件實(shí)現(xiàn)圖片的縮放和拖動(dòng)的實(shí)現(xiàn)代碼
- Android 自定義圓形頭像CircleImageView支持加載網(wǎng)絡(luò)圖片的實(shí)現(xiàn)代碼
- Android自定義圓角ImageView
- Android布局自定義Shap圓形ImageView可以單獨(dú)設(shè)置背景與圖片
- Android編程實(shí)現(xiàn)自定義ImageView圓圖功能的方法
相關(guān)文章
Android基于騰訊云實(shí)時(shí)音視頻仿微信視頻通話(huà)最小化懸浮
這篇文章主要為大家詳細(xì)介紹了Android基于騰訊云實(shí)時(shí)音視頻實(shí)現(xiàn)類(lèi)似微信視頻通話(huà)最小化懸浮,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Android編程之SharedPreferences文件存儲(chǔ)操作實(shí)例分析
這篇文章主要介紹了Android編程之SharedPreferences文件存儲(chǔ)操作方法,實(shí)例分析了SharedPreferences文件操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04Android Path繪制貝塞爾曲線(xiàn)實(shí)現(xiàn)QQ拖拽泡泡
本文主要介紹Android Path繪制貝塞爾曲線(xiàn),這里整理相關(guān)資料并運(yùn)用貝塞爾曲線(xiàn)實(shí)現(xiàn)QQ拖拽泡泡的示例,有興趣的小伙伴可以參考下2016-09-0921天學(xué)習(xí)android開(kāi)發(fā)教程之SurfaceView
21天學(xué)習(xí)android開(kāi)發(fā)教程之SurfaceView,SurfaceView由于可以直接從內(nèi)存或者DMA等硬件接口取得圖像數(shù)據(jù),因此是個(gè)非常重要的繪圖容器,操作相對(duì)簡(jiǎn)單,感興趣的小伙伴們可以參考一下2016-02-02android中AutoCompleteTextView的簡(jiǎn)單用法(實(shí)現(xiàn)搜索歷史)
本篇文章主要介紹了android中AutoCompleteTextView的簡(jiǎn)單用法(自動(dòng)提示),有需要的可以了解一下。2016-11-11PowerManagerService之喚醒鎖的使用獲取創(chuàng)建示例解析
這篇文章主要為大家介紹了PowerManagerService之喚醒鎖的使用獲取創(chuàng)建示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10關(guān)于Android多渠道打包的進(jìn)階知識(shí)
前一篇文章主要介紹了關(guān)于Android程序的多渠道打包方法,這一篇文章介紹了多渠道打包的進(jìn)階知識(shí),還不會(huì)的同學(xué)快進(jìn)來(lái)學(xué)習(xí)下吧,建議收藏以防迷路2021-08-08