前几天项目中用到一个上阵指数大盘指数仪表图效果如下:
一开始觉得用自定义View来写这个界面,而且仪表盘的图片也给好了,本来想这会非常简单,结果因为指针也是一张图片,指针的圆心不好控制,在给数值的时候总是会有略微的偏差看,尤其是在转着转着的时候就看出来,这是因为指针本身有宽度的问题,代码如下:
public class DashboardCustom extends View {
private Paint paint;
private Paint textPaint;
int speed = 0;
float radius;
private Bitmap back;
private Bitmap center;
private Bitmap arrow;
public DashboardCustom(Context context) {
this(context, null, 0);
}
public DashboardCustom(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DashboardCustom(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
/**
* 获得我们所定义的自定义样式属性
*/
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DashboardCustom, defStyleAttr, 0);
speed = a.getInt(R.styleable.DashboardCustom_speed, 0);
paint = new Paint();
textPaint = new Paint();
radius = a.getDimension(R.styleable.DashboardCustom_radius, 200);
back = BitmapFactory.decodeResource(getResources(), R.drawable.dash);
arrow = BitmapFactory.decodeResourc