仿照群英传-自定义View时钟样式

/** * 主要是支持wrap_content属性 * @param widthMeasureSpec * @param heightMeasureSpec */ 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int width; int height; if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } else { float textWidth = mWatch_watch_width * 2; int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight()); width = desired; }

    if (heightMode == MeasureSpec.EXACTLY) {
        height = heightSize;
    } else {

        float textHeight = mWatch_watch_height * 2;
        int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
        height = desired;
    }
    setMeasuredDimension((int) width, (int) height);
}

/**
 * 画圆
 * @param canvas
 */
@Override
protected void onDraw(Canvas canvas) {

    //画圆
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setAntiAlias(true);
    paint.setStrokeWidth(5);
    canvas.drawCircle(mWatch_watch_width, mWatch_watch_height, mWatch_watch_width - 5, paint);

    //画刻度

    Paint mPaintDegree = new Paint();
    mPaintDegree.setStrokeWidth(3);
    for (int i = 0; i < 24; i++) {
        if (i == 0 || i == 6 || i == 12 || i == 18) {
            mPaintDegree.setStrokeWidth(5);
            mPaintDegree.setTextSize(30);
            canvas.drawLine(mWatch_watch_width, mWatch_watch_height - mWatch_watch_width + 5, mWatch_watch_width,
                    mWatch_watch_height - mWatch_watch_width + 40, mPaintDegree);
            String degree = String.valueOf(i);
            canvas.drawText(degree, mWatch_watch_width - mPaintDegree.measureText(degree) / 2, mWatch_watch_height - mWatch_watch_width
                    + 90, mPaintDegree);
        } else {
            mPaintDegree.setStrokeWidth(3);
            mPaintDegree.setTextSize(15);
            canvas.drawLine(mWatch_watch_width, mWatch_watch_height - mWatch_watch_width + 5, mWatch_watch_width,
                    mWatch_watch_height - mWatch_watch_width + 20, mPaintDegree);
            String degree = String.valueOf(i);
            canvas.drawText(degree, mWatch_watch_width - mPaintDegree.measureText(degree) / 2, mWatch_watch_height - mWatch_watch_width
                    + 60, mPaintDegree);
        }
        canvas.rotate(15, mWatch_watch_width, mWatch_watch_height);
    }
    //画时针
    Paint mPaintHout = new Paint();
    mPaintHout.setStrokeWidth(20);
    Paint mPaintMinute = new Paint();
    mPaintMinute.setStrokeWidth(10);
    canvas.save();
    //translate  将原点平移。
    canvas.translate(mWatch_watch_width, mWatch_watch_height);
    canvas.drawLine(0, 0, 50, 80, mPaintHout);
    canvas.drawLine(0, 0, 100, 100, mPaintMinute);
    canvas.restore();
    super.onDraw(canvas);
}

github:https://github.com/yingtongwang/Hello

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值