做了个效果,类似体重测试仪,先上效果
代码不多,就不上传了,我贴出来:
/**
* 测量体重 公斤数0->200kg
* Created by hailong on 2016/11/17 0017.
*/
public class WeightTestView extends View {
public int Max = 200;//最大200Kg
float weight;
int verticalLineHeight;
double indicateGap;//刻度之间的间距
Paint mPaint = new Paint();
int longWidth, middleWidth, shortWidth;//长刻度,中长刻度,短刻度
public WeightTestView(Context context, AttributeSet attrs) {
super(context, attrs);
setLayerType(View.LAYER_TYPE_HARDWARE, new Paint());
init();
}
void init() {
//计算
//h1 表示刻度高度,h2 表示gap
//h1 = 1px,h2 = ?,已知 (h1+h2)*200 = Math.PI*width,算出h2
int width = dip2px(getContext(), getResources().getDimensionPixelSize(R.dimen.weight_size));
indicateGap = width * Math.PI / 200;
verticalLineHeight = getResources().getDimensionPixelSize(R.dimen.weight_indicate_height);
mPaint.setStrokeWidth(verticalLineHeight);
mPaint.setColor(getResources().getColor(R.color.indicateColor));
mPaint.setAntiAlias(true);
mPaint.setDither(false);
mPaint.setFilterBitmap(true);
}
/**
* dip转px
*
* @param context
* @param dipValue
* @return
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
Resources resources = getResources();
longWidth = resources.getDimensionPixelSize(R.dimen.weight_long_width);
middleWidth = resources.getDimensionPixelSize(R.dimen.weight_middle_width);
shortWidth = resources.getDimensionPixelSize(R.dimen.weight_short_width);
}
private Bitmap createLabel(String label) {
TextView view = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.weight_label, null);
view.setText(label);
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
@Override
protected void onDraw(Canvas canvas) {
int width = getWidth();
int height = getHeight();
//画刻度
//angle*R = arcWidth
for (int i = 0; i < Max; i++) {
double angle = 360.0f / Max;
if (i % 5 == 0) {
if (i % 10 == 0) {
//长刻度
canvas.save();
canvas.rotate((float) (i * angle), width / 2, height / 2);
canvas.translate(0, height / 2);
canvas.drawLine(0, 0, longWidth, 0, mPaint);
Bitmap label = createLabel(String.valueOf(i));
canvas.translate(longWidth + 5, -(label.getHeight() - verticalLineHeight) / 2);
canvas.drawBitmap(label, 0, 0, mPaint);
canvas.restore();
//画label
} else {
//中长刻度
canvas.save();
canvas.rotate((float) (i * angle), width / 2, height / 2);
canvas.translate(0, height / 2);
canvas.drawLine(0, 0, middleWidth, 0, mPaint);
canvas.restore();
}
} else {
//短刻度
canvas.save();
canvas.rotate((float) (i * angle), width / 2, height / 2);
canvas.translate(0, height / 2);
canvas.drawLine(0, 0, shortWidth, 0, mPaint);
canvas.restore();
}
}
}
}
使用:
/**
* Created by hailong on 2016/11/17 0017.
*/
public class WeightTestActivity extends Activity {
WeightTestView weight_view;
EditText score_et;
View weight_point;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weight_test);
weight_view = (WeightTestView) findViewById(R.id.weight_view);
score_et = (EditText) findViewById(R.id.score_et);
weight_point = findViewById(R.id.weight_point);
}
public void onTest(View view) {
String lablel = score_et.getText().toString();
try {
float number = Float.parseFloat(lablel);
weight_point.setPivotX(weight_point.getWidth());
weight_point.animate().rotation(360.0f * (number * 1.0f / weight_view.Max)).setDuration(500).start();
} catch (Exception e) {
Toast.makeText(this, "请输入体重", Toast.LENGTH_SHORT).show();
}
}
public void onReset(View view) {
weight_point.animate().rotation(0).setDuration(500).start();
}
}
如果有疑问的或者想交流的,可以加我QQ
623144766