自定义表盘View

使用自定义View

属性attrs文件如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="WatchView">
        <attr name="watchRadius" format="dimension"/>                     //表盘半径
        <attr name="watchPadding" format="dimension"/>                    //表盘相对控件边框距离
        <attr name="watchScalePadding" format="dimension"/>               //刻度相对表盘距离
        <attr name="watchScaleColor" format="color|reference"/>           //常规刻度颜色
        <attr name="watchScaleLength" format="dimension|reference"/>      //常规刻度长度
        <attr name="watchHourScaleColor" format="dimension|reference"/>   //表盘整点刻度颜色
        <attr name="watchHourScaleLength" format="dimension|reference"/>  //整点刻度长度
        <attr name="hourPointColor" format="color|reference"/>            //时针颜色
        <attr name="hourPointLength" format="dimension|reference"/>       //时针长度
        <attr name="minutePointColor" format="color|reference"/>          //分针颜色
        <attr name="minutePointLength" format="dimension|reference"/>     //分针长度
        <attr name="secondPointColor" format="color|reference"/>          //秒针颜色
        <attr name="secondPointLength" format="dimension|reference"/>     //秒针长度
        <attr name="timeTextSize" format="dimension|reference"/>          //表盘字体大小
        <attr name="timeTextColor" format="color|reference"/>             //表盘字体颜色
    </declare-styleable>
</resources>

自定义View文件

public class WatchView extends View{

    /**表盘边距*/
    private float mWatchPadding = 5;
    /**表盘与刻度边距*/
    private float mWatchScalePadding = 5;
    /**表盘半径*/
    private float mWatchRadius = 250;
    /**表盘刻度长度*/
    private float mWatchScaleLength = 5;
    /**表盘刻度颜色*/
    private int mWatchScaleColor = Color.BLACK;
    /**表盘整点刻度长度*/
    private float mHourScaleLength = 8;
    /**表盘整点刻度颜色*/
    private int mHourScaleColor = Color.BLUE;
    /**表盘时针颜色*/
    private int mHourPointColor = Color.BLACK;
    /**表盘时针长度*/
    private float mHourPointLength = 100;
    /**表盘分针颜色*/
    private int mMinutePointColor = Color.BLACK;
    /**表盘分针长度*/
    private float mMinutePointLength = 130;
    /**表盘秒针颜色*/
    private int mSecondPointColor = Color.RED;
    /**表盘秒针长度*/
    private float mSecondPointLength = 160;
    /**表盘尾部指针长度*/
    private float mEndPointLength;
    /**表盘数字颜色*/
    private int mTimeTextColor = Color.BLACK;
    /**表盘数字大小*/
    private int mTimeTextSize = 15;
    private Paint mPaint;
    private String[] mTimes = {"XII","Ⅰ","Ⅱ","Ⅲ","Ⅳ","Ⅴ","Ⅵ","Ⅶ","Ⅷ","Ⅸ","Ⅹ","XI"};
    private Paint mHourPaint;
    private Paint mMinutePaint;
    private Paint mSecondPaint;

    public WatchView(Context context) {
        super(context);
    }

    public WatchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.WatchView);
        int n = array.getIndexCount();
        for (int i = 0;i<n;i++){
            int attr = array.getIndex(i);
            switch (attr){
                case R.styleable.WatchView_watchRadius:
                    mWatchRadius = array.getDimensionPixelSize(attr, dp2px(context,60));
                    break;
                case R.styleable.WatchView_watchPadding:
                    mWatchPadding = array.getDimensionPixelSize(attr,dp2px(context,5));
                    break;
                case R.styleable.WatchView_watchScalePadding:
                    mWatchScalePadding = array.getDimensionPixelSize(attr,dp2px(context,3));
                    break;
                case R.styleable.WatchView_watchScaleLength:
                    mWatchScaleLength = array.getDimensionPixelSize(attr,dp2px(context,5));
                    break;
                case R.styleable.WatchView_watchScaleColor:
                    mWatchScaleColor = array.getColor(attr, Color.parseColor("#50000000"));
                    break;
                case R.styleable.WatchView_watchHourScaleLength:
                    mHourScaleLength = array.getDimensionPixelSize(attr,dp2px(context,10));
                    break;
                case R.styleable.WatchView_watchHourScaleColor:
                    mHourScaleColor = array.getColor(attr,Color.BLACK);
                    break;
                case R.styleable.WatchView_hourPointLength:
                    mHourPointLength = array.getDimensionPixelSize(attr,dp2px(context,35));
                    break;
                case R.styleable.WatchView_hourPointColor:
                    mHourPointColor = array.getColor(attr,Color.BLACK);
                    break;
                case R.styleable.WatchView_minutePointLength:
                    mMinutePointLength = array.getDimensionPixelSize(attr,dp2px(context,40));
                    break;
                case R.styleable.WatchView_minutePointColor:
                    mMinutePointColor = array.getColor(attr,Color.BLACK);
                    break;
                case R.styleable.WatchView_secondPointLength:
                    mSecondPointLength = array.getDimensionPixelSize(attr,dp2px(context,50));
                    break;
                case R.styleable.WatchView_secondPointColor:
                    mSecondPointColor = array.getColor(attr,Color.BLUE);
                    break;
                case R.styleable.WatchView_timeTextSize:
                    mTimeTextSize = array.getDimensionPixelSize(attr,dp2px(context,15));
                    break;
                case R.styleable.WatchView_timeTextColor:
                    mTimeTextColor = array.getColor(attr,Color.BLACK);
                    break;
            }
        }
        array.recycle();
    }

    public WatchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int wrapContentSize = 1000;
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthMode == MeasureSpec.UNSPECIFIED && heightMode == MeasureSpec.UNSPECIFIED){
            wrapContentSize = (int) Math.max(wrapContentSize,mWatchRadius+mWatchPadding);
            setMeasuredDimension(wrapContentSize,wrapContentSize);
        }else {
            setMeasuredDimension(widthSize,heightSize);
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(getWidth()/2,getHeight()/2);
        paintWatchBoard(canvas); //画表盘
        paintScale(canvas); //画刻度
        paintPoint(canvas); //画指针
        canvas.drawCircle(0,0,15,mSecondPaint); //为了美观,也让表盘更接近我们显示生活中的样子,我在圆盘中心画了一个大红圆点装饰秒针
        postInvalidateDelayed(1000); //每隔一秒钟画一次
    }



    /**
     * 画表盘
     * @param canvas
     */
    private void paintWatchBoard(Canvas canvas){
        initPaint();
        canvas.save();
        canvas.drawCircle(0,0,mWatchRadius,mPaint); //画圆盘
        canvas.restore();
    }

    /**
     * 初始化画笔
     */
    private void initPaint(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
    }

    /**
     * 画刻度及整点数字
     * @param canvas
     */
    private void paintScale(Canvas canvas){
        int lineLength; //刻度线长度
        canvas.save();
        for (int i = 0;i<60;i++){
            if (i%5 == 0){//整点刻度下画笔相关属性
                mPaint.setStrokeWidth(dp2px(getContext(),1.5f));
                mPaint.setColor(mHourScaleColor);
                lineLength = dp2px(getContext(),8);
                canvas.drawLine(0,-mWatchRadius+mWatchScalePadding,0,-mWatchRadius+mWatchScalePadding+lineLength,mPaint);
                mPaint.setColor(mTimeTextColor);
                mPaint.setTextSize(mTimeTextSize);
                canvas.drawText(mTimes[i/5],-mTimeTextSize/2,-mWatchRadius+mWatchScalePadding + lineLength+mTimeTextSize,mPaint);//整点的位置标上整点时间数字
            }else {//非整点刻度下画笔相关属性
                mPaint.setStrokeWidth(dp2px(getContext(),0.8f));
                mPaint.setColor(mWatchScaleColor);
                lineLength = dp2px(getContext(),5);
                canvas.drawLine(0,-mWatchRadius+mWatchScalePadding,0,-mWatchRadius+mWatchScalePadding+lineLength,mPaint);
            }
            canvas.rotate(6);//每次画完一个刻度线,画笔顺时针旋转6度(360/60,相邻两刻度之间的角度差为6度)
        }
        canvas.restore();
    }

    /**
     * 初始化指针
     */
    private void initPointPaint(){
        mHourPaint = new Paint();
        mHourPaint.setAntiAlias(true);
        mHourPaint.setStyle(Paint.Style.FILL);
        mHourPaint.setStrokeWidth(16);
        mHourPaint.setColor(mHourPointColor);
 
        mMinutePaint = new Paint();
        mMinutePaint.set(mHourPaint);
        mMinutePaint.setStrokeWidth(12);
        mMinutePaint.setColor(mMinutePointColor);

        mSecondPaint = new Paint();
        mSecondPaint.set(mHourPaint);
        mSecondPaint.setStrokeWidth(7);
        mSecondPaint.setColor(mSecondPointColor);
        mEndPointLength = mWatchRadius/6; //(修饰部分)指针尾部长度,定义为表盘半径的六分之一
    }

    /**
     * 画指针
     * @param canvas
     */
    private void paintPoint(Canvas canvas){
        initPointPaint();
        Calendar c = Calendar.getInstance(); //取当前时间
        float hour = c.get(Calendar.HOUR_OF_DAY);
        float minute = c.get(Calendar.MINUTE);
        float second = c.get(Calendar.SECOND);
        //绘制时针
        canvas.save();
        canvas.rotate((hour+minute/60)*30);
        canvas.drawLine(0,0,0,-mHourPointLength,mHourPaint);
        canvas.drawLine(0,0,0,mEndPointLength,mHourPaint);
        canvas.restore();
        //绘制分针
        canvas.save();
        canvas.rotate((minute+second/60)*6);
        canvas.drawLine(0,0,0,-mMinutePointLength,mMinutePaint);
        canvas.drawLine(0,0,0,mEndPointLength,mMinutePaint);
        canvas.restore();

        //绘制秒针
        canvas.save();
        canvas.rotate(second*6);
        canvas.drawLine(0,0,0,-mSecondPointLength,mSecondPaint);
        canvas.drawLine(0,0,0,mEndPointLength,mSecondPaint);
        canvas.restore();
    }

    /**
     * dp转px
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}

使用如下

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.csun.nusing.myview.WatchView
        android:id="@+id/watchview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:timeTextSize="20dp"
        app:watchRadius="150dp"
        app:hourPointLength="80dp"
        app:minutePointLength="100dp"
        app:secondPointLength="115dp"/>

</RelativeLayout>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章