首页 > 代码库 > 自定义仪表盘DashBoard - -kankanstyle
自定义仪表盘DashBoard - -kankanstyle
因为做一个奶站软件,需要用到仪表盘,显示质量数据
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。