码迷,mamicode.com
首页 > 其他好文 > 详细

自定义仪表盘DashBoard - -kankanstyle

时间:2014-08-25 17:02:14      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   数据   代码   sp   new   on   


因为做一个奶站软件,需要用到仪表盘,显示质量数据


public class MDashboard extends ImageView {



private Bitmap mPointerBitmap;


private int max = 100;
private int progress = 0;


//构????
public MDashboard(Context context) {
super(context);
init();
}


public MDashboard(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}


public MDashboard(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
    //初始化布局   Initializes layout
private void init() {
setScaleType(ScaleType.FIT_CENTER);
//设置表盘图片
setBackgroundDrawableId(R.drawable.base__view_dashboard_bg);
//设置表针图片Set the dial pictures
setPointerDrawableId(R.drawable.base__view_dashboard);
  //设置进度,与当前进度Sets the progress and current progress
setMax(100);
setProgress(0);
}


//设置仪表盘背景Set the dashboard background
public void setBackgroundDrawableId(int id) {
setImageResource(id);
invalidate();
}
    //设置指针的
public void setPointerDrawableId(int id) {
mPointerBitmap = BitmapFactory.decodeResource(getResources(), id);
invalidate();
}


public void setMax(int max) {
this.max = max;
invalidate();
}


public int getMax() {
return max;
}


public void setProgress(int progress) {
this.progress = progress;
//刷新进度
invalidate();
}


public int getProgress() {
return progress;
}


@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = this.getWidth();
int height = this.getHeight();
int rotate = (int) ((float) ((float) progress / (float) max) * (float) 180);
Matrix m = new Matrix();
m.preRotate(rotate);
Bitmap bitmap = Bitmap.createBitmap(mPointerBitmap, 0, 0,
mPointerBitmap.getWidth(), mPointerBitmap.getHeight(), m, true);
int dx = width / 2;
int dy = height - bitmap.getHeight();
if (rotate == 90) {
dx -= bitmap.getWidth() / 2;
} else if (rotate < 90) {
dx -= bitmap.getWidth();
float f = ((float) rotate / (float) 180)
* (float) mPointerBitmap.getHeight() / 2;
dx += f;
} else if (rotate > 90) {
float f = ((float) rotate / (float) 180)
* (float) mPointerBitmap.getHeight() / 2;
dx -= f;
}
canvas.drawBitmap(bitmap, dx, dy, null);
}


}


csdn下载   http://download.csdn.net/download/kan1kan5/7816367


整个代码

自定义仪表盘DashBoard - -kankanstyle

标签:style   http   io   ar   数据   代码   sp   new   on   

原文地址:http://blog.csdn.net/kan1kan5/article/details/38821491

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!