自定义View(01)

(1)一个简单的自定义view的实现。设计到paint,自定义属性,invalidate,canvas的使用。

下面是实现的效果:圆弧的绘制,以及字体的绘制,圆弧的动画。




(2)主要代码实现

public class CircleView extends View {
    private int textNumber,time;
    private float itemNumber;
    private TextPaint mTextPaint;
    private Paint mArcPaint,mProgressPaint;
    private int mArcColor,mProgressColor,mTextColor;
    private int mArcWidth,mTextSize;
    private float radio;
    private float startAngle,scrollowAngle;

    private float progress;
    private int mWidth,mHeight;
    private int text = 0;

    public void setProgress(float progress) {
        this.progress = progress;
        if (progress > 0){
            text = (int) (itemNumber * progress);
            if (progress >= scrollowAngle){
                text = textNumber;
            }
        }
        invalidate();
    }

    public int getTime() {
        return time;
    }

    public float getScrollowAngle() {
        return scrollowAngle;
    }

    public CircleView(Context context) {
        this(context,null);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context,AttributeSet attrs){
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.circle_view_style);
        mArcColor =  array.getColor(R.styleable.circle_view_style_arc_color, Color.RED);
        mProgressColor = array.getColor(R.styleable.circle_view_style_progress_color,Color.BLUE);
        mArcWidth = array.getDimensionPixelOffset(R.styleable.circle_view_style_arc_width,20);
        mTextColor = array.getColor(R.styleable.circle_view_style_text_color,Color.GRAY);
        mTextSize = array.getDimensionPixelSize(R.styleable.circle_view_style_text_size,10);
        time = array.getInteger(R.styleable.circle_view_style_progress_time,10000);
        textNumber = array.getInteger(R.styleable.circle_view_style_max_number,1000);
        radio = array.getDimensionPixelOffset(R.styleable.circle_view_style_arc_radio,100);
        startAngle = array.getFloat(R.styleable.circle_view_style_start_angle,180);
        scrollowAngle = array.getFloat(R.styleable.circle_view_style_scrollow_angle,180);
        array.recycle();

        mArcPaint = new Paint();
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setColor(mArcColor);
        mArcPaint.setStrokeWidth(mArcWidth);
        mArcPaint.setAntiAlias(true);
        mArcPaint.setStrokeCap(Paint.Cap.ROUND);

        mProgressPaint = new Paint();
        mProgressPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint.setColor(mProgressColor);
        mProgressPaint.setStrokeWidth(mArcWidth);
        mProgressPaint.setAntiAlias(true);
        mProgressPaint.setStrokeCap(Paint.Cap.ROUND);

        mTextPaint = new TextPaint();
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);

        itemNumber = textNumber/scrollowAngle;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        RectF arcRect = new RectF(mWidth/2 - radio +mArcWidth,mHeight/2-radio+mArcWidth,mWidth/2 + radio-mArcWidth,mHeight/2+radio-mArcWidth);
        canvas.drawArc(arcRect,startAngle,scrollowAngle,false,mArcPaint);

        RectF progressRect = new RectF(mWidth/2 - radio +mArcWidth,mHeight/2-radio+mArcWidth,mWidth/2 + radio-mArcWidth,mHeight/2+radio-mArcWidth);
        canvas.drawArc(progressRect,startAngle,progress,false,mProgressPaint);

        String content = "计数:"+text;
        Log.e("test","aa=="+mTextPaint.measureText(content)/2);
        float textX = mWidth/2 - mTextPaint.measureText(content)/2;
        float textY = mHeight/2+(mTextPaint.ascent()+mTextPaint.density)/2;
        canvas.drawText(content,textX,textY,mTextPaint);
    }
}


(3)代码下载地址(http://download.youkuaiyun.com/download/huangma11/10125041)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值