仿迅雷下载进度条

效果如下:

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;
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值