自定义View

1,自定义时钟

public class MyView extends View {

    private int width;
    private int height;
    private Paint mPaintLine;
    private Paint mPaintCircle;
    private Paint mPaintText;
    private Calendar mCalender;
    public MyView(Context context) {
        super(context);
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mCalender = Calendar.getInstance();
            invalidate();
            handler.sendEmptyMessageDelayed(0x23,1000);
        }
    };

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaintLine = new Paint();
        mPaintLine.setColor(Color.BLUE);
        mPaintLine.setAntiAlias(true);
        mPaintLine.setStrokeWidth(10);

        mPaintCircle = new Paint();
        mPaintCircle.setColor(Color.GREEN);
        mPaintCircle.setStrokeWidth(10);
        mPaintCircle.setAntiAlias(true);            //设置抗锯齿
        mPaintCircle.setStyle(Paint.Style.STROKE);  //设置为空心

        mPaintText = new Paint();
        mPaintText.setColor(Color.BLACK);
        mPaintText.setTextSize(30);
        handler.sendEmptyMessage(0x23);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width =    getDefaultSize (getSuggestedMinimumWidth(), widthMeasureSpec);
        height =      getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width,height);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(width / 2, height / 2, 300, mPaintCircle);
        canvas.drawCircle(width / 2, height / 2, 10, mPaintCircle);
        for(int i=1;i<=12;i++){
//            canvas.save();    //保存当前画布状态
//            canvas.rotate(30*i, width / 2, height / 2);
            canvas.rotate(30, width / 2, height / 2);
            canvas.drawLine(width / 2, height / 2 - 300, width / 2, height / 2 - 280, mPaintLine);
            canvas.drawText("" + i, width / 2 - 15, height / 2 - 250, mPaintText);
//            canvas.restore();  //重置位置
        }
        mCalender =Calendar.getInstance();
        int minute = mCalender.get(Calendar.MINUTE);
        int hour = mCalender.get(Calendar.HOUR);
        int second = mCalender.get(Calendar.SECOND);
        float degreeMinute = minute/60f*360;
        canvas.save();
        canvas.rotate(degreeMinute, width / 2, height / 2);
        canvas.drawLine(width / 2, height / 2 - 150, width / 2, height / 2 + 30, mPaintLine);
        canvas.restore();
        float degreeHour = (hour*60+minute)/(12*60f)*360;
        canvas.save();
        canvas.rotate(degreeHour, width / 2, height / 2);
        canvas.drawLine(width / 2, height / 2 - 100, width / 2, height / 2 + 30, mPaintLine);
        canvas.restore();

        float degreeSecond = second*6;
        canvas.save();
        canvas.rotate(degreeSecond, width / 2, height / 2);
        canvas.drawLine(width / 2, height / 2 - 200, width / 2, height / 2 + 30, mPaintLine);
        canvas.restore();

    }
}
 
 

2,自定义进度条(3种)

第一种由圆心向外扩大:

public class Progress1 extends View {
    private int width;
    private int height;
    private Paint mPaintBackGround;
    private Paint mPaintProgress;
    private Paint mPaintText;
    private float progress;
    private float textSize = 70;  //textSize默认值等于70
    private float max = 100;      //max默认等于100

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public float getMax() {
        return max;
    }

    public void setMax(float max) {
        this.max = max;
    }



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

    public Progress1(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaintBackGround = new Paint();
        mPaintBackGround.setColor(Color.BLUE);
        mPaintBackGround.setStyle(Paint.Style.FILL);

        mPaintProgress = new Paint();
        mPaintProgress.setColor(Color.GREEN);
        mPaintProgress.setStyle(Paint.Style.FILL);

        mPaintText =new Paint();
        mPaintText.setTextSize(textSize);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);

    }

    public void setProgress(float progress) {
        this.progress = progress;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(width / 2, height / 2, width/2, mPaintBackGround);
        canvas.drawCircle(width/2,height/2,progress/max*width/2,mPaintProgress);
        canvas.drawText((float)Math.round(progress/max*10000)/100+"%",width/2-70,height/2,mPaintText);
//        float  b   =  (float)(Math.round(a*100))/100;    //保留两位小数
    }
}

2,扇形进度条

public class Progress2 extends View {
    private int width;
    private int height;
    private Paint mPaintBackGround;
    private Paint mPaintProgress;
    private Paint mPaintText;
    private float progress;
    private float textSize = 70;  //textSize默认值等于70
    private float max = 100;      //max默认等于100

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public float getMax() {
        return max;
    }

    public void setMax(float max) {
        this.max = max;
    }



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

    public Progress2(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaintBackGround = new Paint();
        mPaintBackGround.setColor(Color.BLUE);
        mPaintBackGround.setAntiAlias(true);
        mPaintBackGround.setStyle(Paint.Style.FILL);

        mPaintProgress = new Paint();
        mPaintProgress.setColor(Color.GREEN);
        mPaintProgress.setStrokeWidth(22);
        mPaintProgress.setAntiAlias(true);
        mPaintProgress.setStyle(Paint.Style.STROKE);

        mPaintText =new Paint();
        mPaintText.setTextSize(textSize);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);

    }

    public void setProgress(float progress) {
        this.progress = progress;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawCircle(width / 2, height / 2, width / 2, mPaintBackGround);
        //画弧线
        RectF oval = new RectF(20, 20, width-20, width-20);
        RectF oval1 = new RectF(30, 30, width-30, width-30);
        canvas.drawArc(oval, 0, 360, true, mPaintBackGround);
        canvas.drawArc(oval1, 0,progress/max*360,false, mPaintProgress);   //useCenter设为false表示里面的线不显示
        canvas.drawText((float) Math.round(progress / max * 10000) / 100 + "%", width/2-70, width/2, mPaintText);
    }
}
 
  

3,矩形进度条

public class Progress3 extends View {
    private int width;
    private int height;
    private Paint mPaintBackGround;
    private Paint mPaintProgress;
    private Paint mPaintText;
    private float progress;
    private float textSize = 70;  //textSize默认值等于70
    private float max = 100;      //max默认等于100

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public float getMax() {
        return max;
    }

    public void setMax(float max) {
        this.max = max;
    }



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

    public Progress3(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaintBackGround = new Paint();
        mPaintBackGround.setColor(Color.BLUE);
        mPaintBackGround.setStyle(Paint.Style.FILL);

        mPaintProgress = new Paint();
        mPaintProgress.setColor(Color.GREEN);
        mPaintProgress.setStyle(Paint.Style.FILL);

        mPaintText =new Paint();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        mPaintText.setTextSize(textSize);
    }

    public void setProgress(float progress) {
        this.progress = progress;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(0,0,width,height,mPaintBackGround);
        canvas.drawRect(0,height-progress/max*height,width,height,mPaintProgress);
        canvas.drawText((float) Math.round(progress / max * 10000) / 100 + "%", width/2-70, width/2, mPaintText);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值