Android Canvas 绘图
生活随笔
收集整理的這篇文章主要介紹了
Android Canvas 绘图
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
畫圖
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
?
?
實(shí)現(xiàn)一個(gè)自定義的進(jìn)度條
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);}@Overridepublic 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();}}@Overrideprotected 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);// 可以使用圖片座位畫筆 注意圖片可以設(shè)置為平鋪 拉伸這些屬性paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));// 可以使用變換屬性來實(shí)現(xiàn)一些效果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);// 可以對位圖來進(jìn)行一些變換 效果然后繪制上去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);} }?
轉(zhuǎn)載于:https://www.cnblogs.com/xbx2015/p/5716574.html
總結(jié)
以上是生活随笔為你收集整理的Android Canvas 绘图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单节点部署Hadoop教程
- 下一篇: day12 Python操作rabbit