自定义View实现图片上传进度显示

来源:互联网 发布:淘宝直播视频教程 编辑:程序博客网 时间:2024/05/07 15:04

转载jssyy123的文章,感谢其无私的奉献!

首先看下我们想要实现的效果如下图(qq聊天中发送图片时的效果):

再看一下我实现的效果:



1、效果已经看见了,下面我们来实现它。首先我创建一个android工程ProgressImageView。然后我们重写ImageView控件,创建ProcessImageView类代码如下:

[cpp] view plaincopy
  1. package com.example.processimageview;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.content.Context;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Rect;  
  9. import android.util.AttributeSet;  
  10. import android.view.ViewGroup.LayoutParams;  
  11. import android.widget.ImageView;  
  12.   
  13. public class ProcessImageView extends ImageView {  
  14.   
  15.     private Paint mPaint;// 画笔  
  16.     int width = 0;  
  17.     int height = 0;  
  18.     Context context = null;  
  19.     int progress = 0;  
  20.   
  21.     public ProcessImageView(Context context) {  
  22.         super(context);  
  23.         // TODO Auto-generated constructor stub  
  24.     }  
  25.   
  26.     public ProcessImageView(Context context, AttributeSet attrs) {  
  27.         this(context, attrs, 0);  
  28.     }  
  29.   
  30.     public ProcessImageView(Context context, AttributeSet attrs,  
  31.             int defStyleAttr) {  
  32.         super(context, attrs, defStyleAttr);  
  33.         this.context = context;  
  34.         mPaint = new Paint();  
  35.     }  
  36.   
  37.     @SuppressLint("DrawAllocation")  
  38.     @Override  
  39.     protected void onDraw(Canvas canvas) {  
  40.         super.onDraw(canvas);  
  41.         mPaint.setAntiAlias(true); // 消除锯齿  
  42.         mPaint.setStyle(Paint.Style.FILL);  
  43.           
  44.   
  45.         mPaint.setColor(Color.parseColor("#70000000"));// 半透明  
  46.         canvas.drawRect(0, 0, getWidth(), getHeight()- getHeight() * progress  
  47.                 / 100, mPaint);  
  48.   
  49.         mPaint.setColor(Color.parseColor("#00000000"));// 全透明  
  50.         canvas.drawRect(0, getHeight() - getHeight() * progress / 100,  
  51.                 getWidth(), getHeight(), mPaint);  
  52.   
  53.         mPaint.setTextSize(30);  
  54.         mPaint.setColor(Color.parseColor("#FFFFFF"));  
  55.         mPaint.setStrokeWidth(2);  
  56.         Rect rect = new Rect();  
  57.         mPaint.getTextBounds("100%", 0, "100%".length(), rect);// 确定文字的宽度  
  58.         canvas.drawText(progress + "%", getWidth() / 2 - rect.width() / 2,  
  59.                 getHeight() / 2, mPaint);  
  60.   
  61.     }  
  62.   
  63.     public void setProgress(int progress) {  
  64.         this.progress = progress;  
  65.         postInvalidate();  
  66.     };  
  67.   
  68. }  


2、将ProcessImageView控件加入activity_layout.xml布局文件中,代码如下;

[cpp] view plaincopy
  1.  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context="com.example.processimageview.MainActivity" >  
  10.   
  11.     <com.example.processimageview.ProcessImageView  
  12.      android:id="@+id/image"  
  13.      android:layout_centerInParent = "true"  
  14.      android:layout_width="200dp"  
  15.      android:layout_height="300dp"  
  16.      android:contentDescription="This is a ProcessImage"  
  17.      android:src="@drawable/image" />  
  18.      
  19. </RelativeLayout>  

3、最后一步,显示效果,在MainActivity类加入显示进度条的ImageView,代码如下:

[java] view plaincopy
  1. package com.example.processimageview;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.os.Message;  
  7. import android.view.Menu;  
  8. import android.view.MenuItem;  
  9. import android.view.View;  
  10. import android.view.ViewGroup.LayoutParams;  
  11. import android.widget.Toast;  
  12.   
  13. public class MainActivity extends Activity {  
  14.   
  15.     ProcessImageView processImageView =null;  
  16.     private final int SUCCESS=0;  
  17.     int progress=0;  
  18.       
  19.     Handler handler=new Handler(){  
  20.         @Override  
  21.         public void handleMessage(Message msg) {  
  22.             super.handleMessage(msg);  
  23.             switch (msg.what) {  
  24.             case SUCCESS:  
  25.                 Toast.makeText(MainActivity.this"图片上传完成", Toast.LENGTH_SHORT).show();  
  26.                 processImageView.setVisibility(View.GONE);  
  27.                 break;  
  28.             }  
  29.         }  
  30.     };  
  31.       
  32.     @Override  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.activity_main);  
  36.   
  37.       processImageView=(ProcessImageView) findViewById(R.id.image);  
  38.         //模拟图片上传进度  
  39.         new Thread(new Runnable() {  
  40.             @Override  
  41.             public void run() {  
  42.                  while (true){    
  43.                      if(progress==100){//图片上传完成  
  44.                          handler.sendEmptyMessage(SUCCESS);  
  45.                          return;  
  46.                      }  
  47.                      progress++;  
  48.                      processImageView.setProgress(progress);  
  49.                     try{    
  50.                         Thread.sleep(200);  //暂停0.2秒  
  51.                     } catch (InterruptedException e){    
  52.                         e.printStackTrace();    
  53.                     }    
  54.                 }    
  55.             }  
  56.         }).start();  
  57.     }  
  58.   
  59.     @Override  
  60.     public boolean onCreateOptionsMenu(Menu menu) {  
  61.         // Inflate the menu; this adds items to the action bar if it is present.  
  62.         getMenuInflater().inflate(R.menu.main, menu);  
  63.         return true;  
  64.     }  
  65.   
  66.     @Override  
  67.     public boolean onOptionsItemSelected(MenuItem item) {  
  68.         // Handle action bar item clicks here. The action bar will  
  69.         // automatically handle clicks on the Home/Up button, so long  
  70.         // as you specify a parent activity in AndroidManifest.xml.  
  71.         int id = item.getItemId();  
  72.         if (id == R.id.action_settings) {  
  73.             return true;  
  74.         }  
  75.         return super.onOptionsItemSelected(item);  
  76.     }  
  77. }  

0 0
原创粉丝点击