Android圆盘刻度,类似体重测试仪,效果不错哦

做了个效果,类似体重测试仪,先上效果


代码不多,就不上传了,我贴出来:

/**
 * 测量体重 公斤数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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值