您的位置:首页 > 其它

自定义view实现仪表盘,时钟

2018-01-26 16:25 369 查看
简单粗暴上代码

WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
int mWidth = wm.getDefaultDisplay().getWidth();
int mHeight = wm.getDefaultDisplay().getHeight();
/* public int mWidth = getWidth();
public int mHeight = getHeight();*/

public YiBiaoPan(Context context) {
super(context);
}

public YiBiaoPan(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public YiBiaoPan(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
protected void onDraw(Canvas canvas) {
//画外圆
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 2, paint);
Log.e("adress",mWidth+","+mHeight);
canvas.drawLine(mWidth/2,mHeight/2,mWidth,mHeight,paint);
//画刻度线
Paint paint1 = new Paint();
paint.setStrokeWidth(3);
for (int i = 0; i < 24; i++) {
if (i == 0 || i == 6 || i == 12 || i == 18) {
paint1.setStrokeWidth(5);
paint1.setTextSize(30);
canvas.drawLine(mWidth / 2, mHeight / 2 - mWidth / 2, mWidth / 2, mHeight / 2 - mWidth / 2 + 60, paint1);
String degree = String.valueOf(i);
canvas.drawText(
degree,
mWidth / 2 - paint1.measureText(degree) / 2,
mHeight / 2 - mWidth / 2 + 90,
paint1
);
} else {
paint1.setStrokeWidth(3);
paint1.setTextSize(15);
canvas.drawLine(mWidth / 2, mHeight / 2 - mWidth / 2, mWidth / 2, mHeight / 2 - mWidth / 2 + 30, paint1);
String degree = String.valueOf(i);
canvas.drawText(
degree,
mWidth / 2 - paint1.measureText(degree) / 2,
mHeight / 2 - mWidth / 2 + 60,
paint1
);
}
canvas.rotate(15, mWidth / 2, mHeight / 2);
}
//画指针
Paint paintHour = new Paint();
paintHour.setStrokeWidth(20);
Paint paintMinute = new Paint();
paintMinute.setStrokeWidth(10);
canvas.save();
canvas.translate(mWidth/2,mHeight/2);
canvas.drawLine(0,0,100,100,paintHour);
canvas.drawLine(0,0,100,200,paintMinute);
canvas.restore();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息