wzlyd1的专栏

<?xml version="1.0" encoding="utf-8"?><resources><attr name="firstColor" format="color" /><attr name="secondColor" format="color" /><attr name="circleWidth" format="dimension" /><attr name="speed" format="integer" /><declare-styleable name="CustomProgressBar"><attr name="firstColor" /><attr name="secondColor" /><attr name="circleWidth" /><attr name="speed" /></declare-styleable></resources>

安卓自定义view是一大难点,但是在实际工作中用的特别多,我也是初学者,这几天一直在研究自定义view,也参考了很多大神的文章,下面这个就是参考鴻洋大神的文章,自己在敲代码中遇到的实际问题,特写出来与大家共享。

首先,自定义view有如下步骤

1、自定义View的属性

2、在View的构造方法中获得我们自定义的属性

[ 3、重写onMesure ]

4、重写onDraw

第一步我们需要自定义属性,在我们的自定义view中获得我们的自定义属性

public CustomCirlceView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);setWillNotDraw(false);//如果你的ondraw函数不执行,那么就写这个函数,否则不用写TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.CustomProgressBar);int n = ta.getIndexCount();for (int i = 0; i < n; i++) {int attr = ta.getIndex(i);switch (attr) {case R.styleable.CustomProgressBar_circleWidth:mcircleWidth = ta.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20,getResources().getDisplayMetrics()));break;case R.styleable.CustomProgressBar_firstColor:mfirstColor = ta.getColor(attr, Color.RED);break;case R.styleable.CustomProgressBar_secondColor:msecondColor = ta.getColor(attr, Color.BLUE);break;case R.styleable.CustomProgressBar_speed:mprogressBar_speed = ta.getInt(attr, 15);break;default:break;}}ta.recycle();}

第三步,重写ondraw方法。这一步是最难理解的一步,我这里多费些口舌讲讲我的理解。

照例,先贴源码,一步步分析

@Overrideprotected void onDraw(Canvas canvas) {mPaint = new Paint();mCircleCentre = getWidth() / 2;// 圆心,有人不理解圆心为什么这么算,同理,,画图解释mRadius = mCircleCentre – mcircleWidth;// 半径RectF overal = new RectF(mCircleCentre – mRadius, mCircleCentre- mRadius, mCircleCentre + mRadius, mCircleCentre + mRadius);Log.i("CustomCircleView", "—mcircleWidth—" + mcircleWidth+ "—getWidth()—-" + getWidth());if (mPaint != null) {if (!isNext) {Toast.makeText(getContext(), "开始画", 0).show();mPaint.setColor(msecondColor);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(mcircleWidth);mPaint.setAntiAlias(true);canvas.drawArc(overal, -90, mProgress, false, mPaint);} else {mPaint.setColor(mfirstColor);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(mcircleWidth);mPaint.setAntiAlias(true);canvas.drawArc(overal, -90, mProgress, false, mPaint);}}super.onDraw(canvas);}

外面黄色的框框是我们手机屏幕的大小,黑色的框框是我们设置的控件大小,假设我们在写定义控件属性时候是这么写

<com.derek.view.CustomCirlceViewandroid:layout_width="120dp"android:layout_height="120dp"custom:circleWidth="20dip"custom:firstColor="#10A0F0"custom:secondColor="#F01255"custom:speed="10" ></com.derek.view.CustomCirlceView>

那么,黑色框框的宽和高都是120dp,那么,我们怎么将圆画到控件的正中央呢?必然是getWidth() / 2了啊,同理,那么半径呢?就是getWidth() / 2-画笔的宽度咯

所以,半径就是mRadius = mCircleCentre – mcircleWidth; 这下子有了圆心有了半径,我们开始画我们需要的扇形吧。用到的是这么一个函数。

RectF overal = new RectF(mCircleCentre – mRadius, mCircleCentre- mRadius, mCircleCentre + mRadius, mCircleCentre + mRadius);一开始我不明白这个函数是干嘛的,查看相关文档貌似是一个矩形,(好像自定义view不管画什么都需要一个矩形或者正方形)先不管这个函数功能,我们来看一下参数:

RectF(float left,float top,float right,float bottom)构造一个指定了4个参数的矩形,那么,left ,top,right,bottom的坐标怎么确定呢?

同理,画图来解释

。难点就是这些咯,下面附上鴻洋大神

博客地址

一个人,一条路,人在途中,心随景动,

wzlyd1的专栏

相关文章:

你感兴趣的文章:

标签云: