类似pc版360扫描的高亮进度条。主要就是刷新图片,贴上代码。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import java.math.BigDecimal;
public class ProgressView extends View {
private int left;
private int start ;
private int end;
private int progress;
private int max;
private int speed;
private final static int DEFAULT_SPEED = 10;
private int width;
private int height;
private Paint paint;
private Bitmap progress_bg;
private Bitmap progress_shine;
private Bitmap progress_bar;
private static final int TIME_DURATION = 50;
public ProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
public ProgressView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
private void init() {
// TODO Auto-generated method stub
progress = 0;
max = 100;
speed = DEFAULT_SPEED;
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
progress_bg = BitmapFactory.decodeResource(getResources(), R.drawable.progress_bg);
progress_shine = BitmapFactory.decodeResource(getResources(),
R.drawable.progress_shine);
progress_bar = BitmapFactory.decodeResource(getResources(),
R.drawable.progress_bar);
width = progress_bg.getWidth();
height = progress_bg.getHeight();
left = 0;
start = 0;
end = start + progress_shine.getWidth();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
// super.onDraw(canvas);
long data = 0;
long cureentLong = System.currentTimeMillis();
render(canvas);
logic();
long endLong = System.currentTimeMillis();
data = endLong - cureentLong;
if (data < TIME_DURATION) {
try {
Thread.sleep(TIME_DURATION - data);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
postInvalidate();
}
private void render(Canvas canvas) {
left = width * progress / 100;
canvas.drawBitmap(progress_bg, 0, 0, paint);
canvas.save();
canvas.clipRect(new Rect(0, 0, (int) left, height));
canvas.drawBitmap(progress_bar, 0, 0, paint);
if (end != 0 && start <= left) {
canvas.drawBitmap(progress_shine, start, 0, paint);
}
canvas.clipRect(new Rect(0, 0, 800, 480));
canvas.restore();
}
private void logic() {
start += speed;
end = start + progress_shine.getWidth();
if ( end >= width) {
start = 0;
end = 0;
}
}
public void setProgress(int progress) {
if(max == 100)
{
setRelativeProgress(progress);
}
BigDecimal bdProgress = new BigDecimal(progress);
BigDecimal bdMax = new BigDecimal(max);
BigDecimal result = bdProgress.divide(bdMax, 2,
BigDecimal.ROUND_HALF_DOWN);
int r = (int) (result.doubleValue() * 100);
if(r > 100){
r = 100;
}
this.progress = r;
// postInvalidate();
}
/*
* @param progress as the max is 100
*/
public void setRelativeProgress(int progress) {
if(progress < 0)
{
return;
}
if(progress > 100)
{
this.progress = 100;
}
else
{
this.progress = progress;
}
// postInvalidate();
}
public void setMax(int max) {
this.max = max;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
水平有限,仅限于实现了效果,很多细节没处理好,简单的应用基本没问题。
下面是ui提供的图,ui功底好的同学可以设计其他的效果。