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); } }
相关文章推荐
- android studio xposed教程(含jar下载地址)
- Android中常用的对话框的创建
- android Intent的相关Flag说明
- Android消息循环机制探讨
- 关于在Android中访问和使用到上下文变量
- Android开发学习:Android Studio如何签名
- 从android应用程序跳转到系统的各个设置页面
- Android抽象布局——include、merge 、ViewStub
- android-图片加载 Picasso 设计及优点
- android adb源码分析(2)
- Android如何判断时间是否在0到8点之间
- Android中过场动画
- Android 四大图片缓存原理,特性对比
- Android中Handler造成内存泄露解决方法
- Json数据格式介绍、使用Android自带类库完成Json生成与解析
- Android的四大组件之-Activity的状态和生命周期
- Android Toast优化,不看别后悔
- Android基本问题汇总
- 设置背景渐变色
- android轮播图的实现