Android自定义仪表盘
2017-01-01 18:53
453 查看
public class panelview
extends View {
private Paint
paint=new Paint();
Paint textpaint=new Paint();
Paint mPaint=new Paint();
TextView text;
float
dg=120;
float
num=66;
public panelview(Context context,AttributeSet attrs) {
super(context,attrs);
//
TODO Auto-generated constructor stub
//
handler.post(redrawRunnable);
}
public
void setdg(float dgree) {
dg=(dgree*180)/100;
num=dgree;
invalidate();
}
protected
void onDraw(Canvas canvas) {
//
TODO Auto-generated method stub
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
canvas.translate(canvas.getWidth()/3, 250);
paint.setStrokeWidth(110);
paint.setColor(0xff1e7d1c);
RectF vol1=new RectF(-120,-120,120,120);
canvas.drawArc(vol1, 180,
dg, false,
paint);
paint.setColor(0xffd3d3d3);
canvas.drawArc(vol1, 180+dg, 180-dg,
false, paint);
//canvas.drawCircle(0, 0, 200, paint); //??ԲȦ
mPaint.setAntiAlias(true);
mPaint.setStyle(Style.STROKE);
mPaint.setStrokeWidth(10);
mPaint.setColor(0xffd3d3d3);
canvas.drawCircle(0, 0, 15,
mPaint);
canvas.save();
Paint citePaint =
new Paint(paint);
citePaint.setColor(Color.BLACK);
citePaint.setTextSize(18);
citePaint.setStrokeWidth(2);
citePaint.setStyle(Style.FILL);
canvas.drawText("State of charge", -60, 50, citePaint);
citePaint.setTextSize(30);
citePaint.setStrokeWidth(4);
canvas.drawText((int)(num)+"%", -30, 90, citePaint);
canvas.restore();
Paint tmpPaint =
new Paint(paint);
//画白色小圆圈
tmpPaint.setStrokeWidth(1);
tmpPaint.setColor(Color.WHITE);
tmpPaint.setStyle(Style.FILL_AND_STROKE);
float y=150;
int count = 30;
//控制数量
for(int i=0 ; i <count ; i++){
if(i>=6&&i<=22){
if(i%5 == 0){
// canvas.drawLine(0, y, 0, y+10, tmpPaint);
canvas.drawCircle(0, y, 5, tmpPaint);
}else{
//canvas.drawLine(0f, y+3f, 0f, y +10f, tmpPaint);
canvas.drawCircle(0, y, 5, tmpPaint);
}
}
canvas.rotate(360/count,0f,0f);
//??ת??ֽ
}
//画指针
canvas.rotate(dg-90, 0, 0);
tmpPaint.setStrokeWidth(2);
tmpPaint.setStyle(Style.FILL);
tmpPaint.setColor(Color.BLACK);
// canvas.drawCircle(0, -150, 8, tmpPaint);
Path path=new Path();
path.moveTo(-7, 0);
path.lineTo(7, 0);
path.lineTo(0, -190);
path.close();
//canvas.drawLine(0, 0, 0, -150, tmpPaint);
canvas.drawPath(path, tmpPaint);
canvas.restore();
}
}
extends View {
private Paint
paint=new Paint();
Paint textpaint=new Paint();
Paint mPaint=new Paint();
TextView text;
float
dg=120;
float
num=66;
public panelview(Context context,AttributeSet attrs) {
super(context,attrs);
//
TODO Auto-generated constructor stub
//
handler.post(redrawRunnable);
}
public
void setdg(float dgree) {
dg=(dgree*180)/100;
num=dgree;
invalidate();
}
protected
void onDraw(Canvas canvas) {
//
TODO Auto-generated method stub
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
canvas.translate(canvas.getWidth()/3, 250);
paint.setStrokeWidth(110);
paint.setColor(0xff1e7d1c);
RectF vol1=new RectF(-120,-120,120,120);
canvas.drawArc(vol1, 180,
dg, false,
paint);
paint.setColor(0xffd3d3d3);
canvas.drawArc(vol1, 180+dg, 180-dg,
false, paint);
//canvas.drawCircle(0, 0, 200, paint); //??ԲȦ
mPaint.setAntiAlias(true);
mPaint.setStyle(Style.STROKE);
mPaint.setStrokeWidth(10);
mPaint.setColor(0xffd3d3d3);
canvas.drawCircle(0, 0, 15,
mPaint);
canvas.save();
Paint citePaint =
new Paint(paint);
citePaint.setColor(Color.BLACK);
citePaint.setTextSize(18);
citePaint.setStrokeWidth(2);
citePaint.setStyle(Style.FILL);
canvas.drawText("State of charge", -60, 50, citePaint);
citePaint.setTextSize(30);
citePaint.setStrokeWidth(4);
canvas.drawText((int)(num)+"%", -30, 90, citePaint);
canvas.restore();
Paint tmpPaint =
new Paint(paint);
//画白色小圆圈
tmpPaint.setStrokeWidth(1);
tmpPaint.setColor(Color.WHITE);
tmpPaint.setStyle(Style.FILL_AND_STROKE);
float y=150;
int count = 30;
//控制数量
for(int i=0 ; i <count ; i++){
if(i>=6&&i<=22){
if(i%5 == 0){
// canvas.drawLine(0, y, 0, y+10, tmpPaint);
canvas.drawCircle(0, y, 5, tmpPaint);
}else{
//canvas.drawLine(0f, y+3f, 0f, y +10f, tmpPaint);
canvas.drawCircle(0, y, 5, tmpPaint);
}
}
canvas.rotate(360/count,0f,0f);
//??ת??ֽ
}
//画指针
canvas.rotate(dg-90, 0, 0);
tmpPaint.setStrokeWidth(2);
tmpPaint.setStyle(Style.FILL);
tmpPaint.setColor(Color.BLACK);
// canvas.drawCircle(0, -150, 8, tmpPaint);
Path path=new Path();
path.moveTo(-7, 0);
path.lineTo(7, 0);
path.lineTo(0, -190);
path.close();
//canvas.drawLine(0, 0, 0, -150, tmpPaint);
canvas.drawPath(path, tmpPaint);
canvas.restore();
}
}
相关文章推荐
- 手把手带你画一个 时尚仪表盘 Android 自定义View
- 手把手带你画一个 时尚仪表盘 Android 自定义View
- android自定义View(仪表盘),多属性可设置
- Android 自定义View-时尚仪表盘
- 如何把Android的一款自定义仪表盘控件整合到你的项目里
- Android自定义view之仿支付宝芝麻信用仪表盘
- Android自定义仪表盘
- 手把手带你画一个 时尚仪表盘 Android 自定义View
- Android自定义View仿支付宝芝麻信用分仪表盘
- android 自定义View 仪表盘 DashboardView 的实现
- Android 自定义仪表盘
- Android自定义view之仿支付宝芝麻信用仪表盘 ---by ccy
- 手把手带你画一个 时尚仪表盘 Android 自定义View
- Android自定义View之仪表盘
- <Android 进阶(一)> 自定义View之仪表盘进度条
- Android自定义View 圆形刻度罗盘 仪表盘 指针动态改变
- Android自定义view之仿支付宝芝麻信用仪表盘示例
- Android自定义仪表盘视图
- 时尚仪表盘 Android 自定义View
- Android自定义仪表盘View(汽车速度仪,刻度盘等),多种自定义模式,满足多种需求