游戏技术文章

android自定义ImageView仿图片上传

时间:2017-4-21 22:51:08  作者:棋牌资源网  来源:棋牌资源网  查看:8854  评论:0
内容摘要:Activity代码 1 public class MainActivity extends AppCompatActivity {   2     ProcessIma...
android自定义ImageView仿图片上传

Activity代码

复制代码
 1 public class MainActivity extends AppCompatActivity {  
 2     ProcessImageView processImageView =null;  
 3     int progress=0;  
 4   
 5     @Override  
 6     protected void onCreate(Bundle savedInstanceState) {  
 7         super.onCreate(savedInstanceState);  
 8         setContentView(R.layout.activity_main);  
 9   
10         processImageView=(ProcessImageView) findViewById(R.id.image);  
11         //模拟图片上传进度  
12         new Thread(new Runnable() {  
13             @Override  
14             public void run() {  
15                 while (true){  
16                     if(progress==100){//图片上传完成  
17                         return;  
18                     }  
19                     progress++;  
20                     processImageView.setProgress(progress);  
21                     try{  
22                         Thread.sleep(200);  //暂停0.2秒  
23                     } catch (InterruptedException e){  
24                         e.printStackTrace();  
25                     }  
26                 }  
27             }  
28         }).start();  
29     }  
30 }  
复制代码

 

自定义view

复制代码
 1 public class ProcessImageView extends ImageView{  
 2     private Context context;  
 3     private Paint paint;  
 4     private LogUtil log=LogUtil.getInstance();  
 5     int progress = 0;  
 6     private boolean flag;  
 7   
 8     public ProcessImageView(Context context) {  
 9         super(context);  
10     }  
11   
12     public ProcessImageView(Context context, AttributeSet attrs) {  
13         this(context, attrs,0);  
14     }  
15   
16     public ProcessImageView(Context context, AttributeSet attrs, int defStyleAttr) {  
17         super(context, attrs, defStyleAttr);  
18         this.context=context;  
19         paint=new Paint();  
20     }  
21   
22     @Override  
23     protected void onDraw(Canvas canvas) {  
24         super.onDraw(canvas);  
25         paint.setAntiAlias(true); //消除锯齿  
26         paint.setStyle(Paint.Style.FILL); //设置paint为实心,  Paint.Style.STROKE为空心  
27         paint.setColor(Color.parseColor("#70000000")); //设置为半透明  
28         canvas.drawRect(0,0,getWidth(),getHeight()-getHeight()*progress/100,paint); //这里getWidth() 获取的是image对象宽高度 xml值*2  
29   
30         paint.setColor(Color.parseColor("#00000000"));// 全透明  
31         canvas.drawRect(0, getHeight() - getHeight() * progress / 100,  
32                 getWidth(), getHeight(), paint);  
33   
34         if(!flag){  
35             paint.setTextSize(30);  
36             paint.setColor(Color.parseColor("#FFFFFF"));  
37             paint.setStrokeWidth(2);  
38             Rect rect = new Rect();  
39             paint.getTextBounds("100%"0"100%".length(), rect);// 确定文字的宽度  
40             canvas.drawText(progress + "%", getWidth() / 2 - rect.width() / 2,  
41                     getHeight() / 2, paint);  
42         }  
43     }  
44   
45     public void setProgress(int progress) {  
46         this.progress = progress;  
47         if(progress==100){  
48             flag=true;  
49         }  
50         postInvalidate();  
51     }  
52 }  
复制代码

看下自定义view 类,主要onDraw()方法中.

绘制中分为三部分,

第一部分为上部分半透明区域

1
2
3
4
paint.setAntiAlias(true); //消除锯齿  
paint.setStyle(Paint.Style.FILL); //设置paint为实心,  Paint.Style.STROKE为空心  
paint.setColor(Color.parseColor("#70000000")); //设置为半透明  
canvas.drawRect(0,0,getWidth(),getHeight()-getHeight()*progress/100,paint); 

第二部分为下部分全透明区域

1
2
3
paint.setColor(Color.parseColor("#00000000"));// 全透明  
canvas.drawRect(0, getHeight() - getHeight() * progress / 100,  
               getWidth(), getHeight(), paint);  

第三部分就是中间的progress值变化

1
2
3
4
5
6
7
8
9
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);  

  

标签:android自定义ImageView仿图片上传 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2024 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved