Android绘制自定义饼状图
2017-06-25 17:20
411 查看
Android绘制自定义饼状图
Android绘制自定义饼状图绘制圆环
创建自定义圆环对应的类
修改布局文件 使用自定义的 视图对象
绘制饼状图
创建饼状图对应对象
使用自定义的试图对象
效果
1.绘制圆环
草图1. 创建自定义圆环对应的类
package com.example.a01drawround.ui; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; import com.example.a01drawround.UIUtils.UIUtils; /** * Created by chen on 2017/6/24. * 自定义视图1:绘制圆环比例图的 */ public class RoundCircleProgress extends View{ public RoundCircleProgress(Context context) { this(context,null); } public RoundCircleProgress(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public RoundCircleProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //初始化画笔 paint = new Paint(); paint.setAntiAlias(true); //画笔去除毛边 } //1. 测量尺寸--对于圆形 private int width ;//视图宽度(高度) private int hight ;//视图高度==宽度 private int roundCircleWidth = UIUtils.px2dp(this.getContext(),40); //2. 设置颜色 private int roundCircleColor = Color.GRAY; //圆环颜色 private int roundProgressColor = Color.RED; //进度颜色 private int roundTextColor = Color.BLUE; //字体颜色 //3. 字体大小 private int roundTextSize = UIUtils.px2dp(this.getContext(),80); private float roundMax = 100; private float roundProgress = 30f; //4. 画笔 private Paint paint; @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); width = this.getMeasuredWidth(); //视图宽 hight = this.getMeasuredHeight();//获取视图高 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //圆心位置 int cx = width/2; int cy = hight/2; //半径 int radios = width/2 - roundCircleWidth/2; //1. 绘制圆环 paint.setColor(roundCircleColor); paint.setStyle(Paint.Style.STROKE); //边界 paint.setStrokeWidth(roundCircleWidth); canvas.drawCircle(cx,cy,radios,paint); //2. 绘制进度 //绘制圆形对应的矩形的左上 右下坐标 RectF oval = new RectF(roundCircleWidth/2,roundCircleWidth/2,width-roundCircleWidth/2,hight-roundCircleWidth/2); float startAngle = 0; float sweepAngle = roundProgress*360/roundMax; paint.setColor(roundProgressColor); canvas.drawArc(oval,startAngle,sweepAngle,false,paint); //3. 绘制文字 String text = roundProgress +"%"; //获取文字对应的包裹矩形的左下角坐标 Rect textRect = new Rect(); paint.setTextSize(roundTextSize); paint.setColor(roundTextColor); paint.setStrokeWidth(0);// paint.getTextBounds(text,0,text.length(),textRect); //文字包裹矩形的坐下标 int textX = cx- textRect.width()/2; int textY = cx+ textRect.height()/2; canvas.drawText(text,textX,textY,paint); } }
02 修改布局文件 使用自定义的 视图对象
<!--绘制圆环 --> <com.example.a01drawround.ui.RoundCircleProgress android:id="@+id/circle1" android:layout_width="120dp" android:layout_height="120dp" android:layout_gravity="center" />
2. 绘制饼状图
1. 创建饼状图对应对象
package com.example.a01drawround.ui; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; import com.example.a01drawround.UIUtils.UIUtils; /** * Created by chen on 2017/6/24. * 自定义视图1:绘制圆环比例图的 */ public class RoundFillProgress extends View { //1. 测量尺寸--对于圆形 private int width;//视图宽度(高度) private int hight;//视图高度==宽度 private int roundCircleWidth = UIUtils.px2dp(this.getContext(), 40); //2. 设置颜色 private int roundCircleColor = Color.GRAY; //圆环颜色 private int roundProgressColor = Color.RED; //进度颜色 private int roundTextColor = Color.BLUE; //字体颜色 //3. 字体大小 private int roundTextSize = UIUtils.px2dp(this.getContext(), 80); private float roundMax = 100; private float roundProgress = 60f; //4. 画笔 private Paint paint; public RoundFillProgress(Context context) { this(context, null); } public RoundFillProgress(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public RoundFillProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //初始化画笔 paint = new Paint(); paint.setAntiAlias(true); //画笔去除毛边 } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); width = this.getMeasuredWidth(); //视图宽 hight = this.getMeasuredHeight();//获取视图高 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //圆心位置 int cx = width / 2; int cy = hight / 2; //半径 int radios = width / 2 - roundCircleWidth / 2; //1. 绘制圆环 paint.setColor(roundCircleColor); paint.setStyle(Paint.Style.FILL); //边界 paint.setStrokeWidth(roundCircleWidth); canvas.drawCircle(cx, cy, radios, paint); //2. 绘制进度 //绘制圆形对应的矩形的左上 右下坐标 RectF oval = new RectF(roundCircleWidth / 2, roundCircleWidth / 2, width - roundCircleWidth / 2, hight - roundCircleWidth / 2); float startAngle = 0; float sweepAngle = roundProgress * 360 / roundMax; paint.setColor(roundProgressColor); canvas.drawArc(oval, startAngle, sweepAngle, true, paint); //3. 绘制文字 String text = roundProgress + "%"; //获取文字对应的包裹矩形的左下角坐标 Rect textRect = new Rect(); paint.setTextSize(roundTextSize); paint.setColor(roundTextColor); paint.setStrokeWidth(0); paint.getTextBounds(text, 0, text.length(), textRect); //文字包裹矩形的坐下标 int textX = cx - textRect.width() / 2; int textY = cx + textRect.height() / 2; canvas.drawText(text, textX, textY, paint); } }
2. 使用自定义的试图对象
<!--绘制饼状图 --> <com.example.a01drawround.ui.RoundFillProgress android:id="@+id/circle2" android:layout_width="120dp" android:layout_height="120dp" android:layout_gravity="center" />
相关文章推荐
- Android自定义View --- 绘制圆环
- android绘制view的过程(自定义view一)
- Android学习自定义View(二)——View和ViewGroup绘制流程以及invalidate()
- Android自定义组件系列【9】——Canvas绘制折线图
- Android实现自定义view---绘制图片
- android自定义view--绘制顺序及相关原理
- android自定义View绘制几何图形
- android自定义view–绘制顺序及相关原理
- android自定义view-绘制顺序及相关原理
- 自定义绘制android EditText的背景,定义EditText文字的显示样式
- google 地图和baidu地图 在android上的实现过程和对比 包含 自定义图层的绘制
- 【eoe教程】Android中自定义视图的绘制方法
- 【eoe教程】Android中自定义视图的绘制方法
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- android自定义绘制TableLayout,类似.net中DataGrid控件
- Android中使用ListView绘制自定义表格
- Android UI 之居中绘制文本内容的正确方法——实现自定义一个TextView
- Android UI 之居间绘制文本内容的正确方法——实现自定义一个TextView
- android自定义View绘制字符串