转载请注明出处:http://blog.csdn.net/magic_jss/article/details/52348392;
群里聊天一哥们发的一个自定义View感觉很perfection,于是就根据他发的图写了个类似的效果。一个仪表盘,描述用户的账号安全等级。希望能够帮助到需要类似效果的猿/媛们,如果存在什么问题恳请指正!
效果图:
模拟器上运行效果可能有点差,真机上就比较清晰了。
1、自定义View
废话少说,直接上码;
/**
* Created by magic on 2016年7月28日.账户安全 仪表盘view
*/
@SuppressLint({
"DrawAllocation", "ResourceAsColor" })
public class DashBoardView extends View {
private Context context;
// 圆
private Paint mPaint;
// 弧
private Paint mPaintArc;
// 弧的外接矩形
private RectF rectFArc, rectFArc2;
// text
private Paint mPaintText;
// line
private Paint mPaintLine;
// 指针路径
private Path path;
// 宽、高
private float width, height;
private float radius, arcWidth, arcWidth2, arcStrokeWidth, pointerLength,
lineLength;
// 设置旋转角度
public float degrees = 0;
private float paddingLeft, paddingTop, paddingRight, paddingBottom;
// 底部描述文字
private String desc;
public DashBoardView(Context context) {
super(context);
this.context = context;
init();
}
public DashBoardView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public DashBoardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
private void init() {
mPaint = new Paint();
mPaint.setColor(context.getResources().getColor(R.color.zs));
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
mPaintArc = new Paint();
mPaintArc.setStyle(Paint.Style.STROKE);
mPaintArc.setAntiAlias(true);
mPaintLine = new Paint();
mPaintLine.setColor(context.getResources().getColor(R.color.zs));
mPaintLine.setStyle(Paint.Style.FILL);
mPaintLine.setAntiAlias(true);
path = new Path(