您的位置:首页 > 移动开发 > Android开发

Android Canvas 绘图

2016-07-28 23:00 393 查看
画图 http://www.2cto.com/kf/201404/296296.html http://blog.csdn.net/tianjian4592/article/details/44783283 http://blog.csdn.net/wangfayinn/article/details/8685310 http://www.cnblogs.com/tianzhijiexian/p/4298660.html

实现一个自定义的进度条

public class ArcProgress extends View {
private Paint paint;
private RectF rectF = new RectF();
private float strokeWidth;
private int progress = 0;
private int max;
private float arcAngle;
private final float default_stroke_width;
private final int default_max = 100;
private final float default_arc_angle = 360;

public ArcProgress(Context context) {
this(context, null);
}

public ArcProgress(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public ArcProgress(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

default_stroke_width = Utils.dp2px(context, 4);

TypedArray attributes = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.ArcProgress, defStyleAttr, 0);
initByAttributes(attributes);
attributes.recycle();
}

protected void initByAttributes(TypedArray attributes) {
arcAngle = attributes.getDimension(R.styleable.ArcProgress_arc_angle,
default_arc_angle);
setMax(attributes.getInt(R.styleable.ArcProgress_arc_max, default_max));
setProgress(attributes.getInt(R.styleable.ArcProgress_arc_progress, 0));
strokeWidth = attributes.getDimension(
R.styleable.ArcProgress_arc_stroke_width, default_stroke_width);
}

@Override
public void invalidate() {
super.invalidate();
}

public int getProgress() {
return progress;
}

public void setProgress(int progress) {
this.progress = progress;
if (this.progress > getMax()) {
this.progress %= getMax();
}
invalidate();
}

public int getMax() {
return max;
}

public void setMax(int max) {
if (max > 0) {
this.max = max;
invalidate();
}
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float startAngle = 90 - arcAngle / 2f;
float finishedSweepAngle = progress / (float) getMax() * arcAngle;
float finishedStartAngle = startAngle;

int width = getMeasuredWidth();

rectF.set(strokeWidth / 2f, strokeWidth / 2f,
getMeasuredWidth() - strokeWidth / 2f,
getMeasuredWidth() - strokeWidth / 2f);

paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(strokeWidth);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeCap(Paint.Cap.ROUND);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.uu_loading_bg);
bitmap = Bitmap.createScaledBitmap(bitmap, width, width, false);
// 可以使用图片座位画笔 注意图片可以设置为平铺 拉伸这些属性
paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
// 可以使用变换属性来实现一些效果
canvas.scale((float) 0.9, (float) 0.9, width / 2, width / 2);
// 绘制圆弧 等矩形图形
canvas.drawArc(rectF, startAngle, arcAngle, false, paint);

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.uu_loading);
bitmap = Bitmap.createScaledBitmap(bitmap, width, width, false);
paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawArc(rectF, finishedStartAngle, finishedSweepAngle, false, paint);

// 可以对位图来进行一些变换 效果然后绘制上去
Bitmap bitmap_loadingpoint = BitmapFactory.decodeResource(getResources(), R.drawable.uu_loadingpoint);
Matrix matrix = new Matrix();
matrix.setTranslate(bitmap_loadingpoint.getHeight() / 2, bitmap_loadingpoint.getHeight() / 2);
matrix.postRotate(finishedSweepAngle + 45, getWidth() / 2, getWidth() / 2);
canvas.drawBitmap(bitmap_loadingpoint, matrix, null);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: