效果如下:
package com.apple.myview.widget;
/**
* 模仿迅雷下载的效果
*/
public class Progress4 extends View {
private int width;
private int height;
private Paint mPaintBackGround;
private Paint mPaintWave;
private Paint mPaintText;
private float textSize = 5; //textSize默认值等于5
private Path pathTriangle;
private Path pathBEZIER;
private Path pathWave;
private float progress;
private float max=100;
private int count;
private Canvas mCanvasBit;
private Bitmap mBitmapBubble;
private float waveHeight=5;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (count < 80) {
count += 2;
} else {
count = 0;
}
invalidate();
mHandler.sendEmptyMessageDelayed(0x11, 50);
}
};
public void setTextSize(float textSize) {
this.textSize = textSize;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
waveHeight = 10;
return true;
case MotionEvent.ACTION_UP:
waveHeight =5;
return true;
}
return super.onTouchEvent(event);
}
public Progress4(Context context) {
super(context);
}
public Progress4(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintBackGround = new Paint();
mPaintBackGround.setColor(getResources().getColor(R.color.lightblue));
mPaintBackGround.setAntiAlias(true);
mPaintBackGround.setStrokeWidth(20);
mPaintBackGround.setStyle(Paint.Style.FILL);
mPaintWave = new Paint();
mPaintWave.setColor(getResources().getColor(R.color.blue));
mPaintWave.setStyle(Paint.Style.FILL);
PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
mPaintWave.setXfermode(xfermode);
mPaintWave.setAntiAlias(true);
mPaintText = new Paint();
mPaintText.setTextSize(5);
mPaintText.setTextAlign(Paint.Align.CENTER);
mPaintText.setAntiAlias(true);
pathTriangle = new Path();
pathBEZIER = new Path();
pathWave = new Path();
mCanvasBit = new Canvas();
mHandler.sendEmptyMessage(0x11);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
mPaintText.setTextSize(textSize);
mBitmapBubble = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
mCanvasBit = new Canvas(mBitmapBubble);
}
public void setProgress(float progress) {
this.progress = progress;
}
public void setMax(float max) {
this.max = max;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 绘制圆形背景
*/
mCanvasBit.drawCircle(300, 300, 150, mPaintBackGround);
/**
* 绘制波浪线
*/
pathWave.reset();
float progress_percent =(float) Math.round(progress / max * 10000) / 100;
pathWave.moveTo(500, 450-progress_percent*3);
pathWave.lineTo(500, 500);
pathWave.lineTo(count, 500);
pathWave.lineTo(count,450-progress_percent*3);
for (int i = 0; i < 6; i++) {
pathWave.rQuadTo(20, waveHeight, 40, 0);
pathWave.rQuadTo(20, -waveHeight, 40, 0);
}
pathWave.close();
mCanvasBit.drawPath(pathWave, mPaintWave);
canvas.drawBitmap(mBitmapBubble, 0, 0, null);
canvas.drawText(progress_percent+"%",300,325,mPaintText);
}
}
本效果所使用的主函数:
public class Progress4Activity extends AppCompatActivity implements View.OnClickListener {
@ViewInject(R.id.button_progress4_start)
private Button mButtonStart;
@ViewInject(R.id.progress4)
private Progress4 mProgress4;
private float progress;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0x10:
if(progress<300){
progress++;
}else {
progress = 0;
}
mProgress4.setMax(300f);
mProgress4.setProgress(progress);
handler.sendEmptyMessageDelayed(0x10,400);
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress4);
ViewUtils.inject(this);
}
@OnClick(R.id.button_progress4_start)
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_progress4_start:
handler.sendEmptyMessageDelayed(0x10,400);
break;
default:
break;
}
}
}