Android_CustomView

自定义控件基本步骤

1、自定义View的属性
2、在View的构造方法中获得我们自定义的属性
3、重写onMesure
4、重写onDraw

自定义View的属性

  • 在res/values/ 下建立一个attrs.xml

    
    
        
        
    


  • 在layout中使用
    添加xml命名空间
    xmlns:app="http://schemas.android.com/apk/res/com.speed.customview"





获取自定义属性

    //获取自定义的样式属性
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleView, defStyleAttr, 0);
    mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleView_border_width, DEFAULT_BORDER_WIDTH);
    mBorderColor = a.getColor(R.styleable.CircleView_border_color, DEFAULT_BORDER_COLOR);
    //释放属性数组
    a.recycle();

重写onMesure(), onDraw()

onMesure 在定义ViewGroup时才需要重写

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int radiu = (canvas.getWidth() - mBorderWidth)/2;
    /**
     * 中心的横坐标,中心的纵坐标, 半径
     */
    canvas.drawCircle(canvas.getWidth()/2, canvas.getWidth()/2, radiu, mPaint);
}

对外提供修改属性的线程安全方法

public synchronized void setBorderWidth(int borderWidth) {
    mBorderWidth = borderWidth;
    mPaint.setStrokeWidth(mBorderWidth);
    
    //调用重绘函数
    invalidate();
    //非UI线程是不能直接更新UI的,postInvalidate()替代我们原来的invalidate()即可用在子线程更新
}

public synchronized void setBorderColor(int borderColor) {
    mBorderColor = borderColor;
    mPaint.setColor(mBorderColor);
    invalidate();
}

效果图

Android_CustomView_第1张图片
CircleView.png

颜色交替的圆环eg

Paste_Image.png
  • 自定义属性

    
    
    
    

  • Java代码
public class CustomProgressBar extends View implements Runnable {

private static final int DEFAULT_BORDER_COLOR = Color.BLACK;

private int mFirstColor;
private int mSecondColor;
private int mCircleWidth;
private int mSpeed;

private int mProgress;
private Paint mPaint;

private boolean isFirst = true;

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

public CustomProgressBar(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CustomProgressBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
            R.styleable.CustomProgressBar, defStyle, 0);

    mFirstColor = a.getColor(R.styleable.CustomProgressBar_first_color,
            DEFAULT_BORDER_COLOR);
    mSecondColor = a.getColor(R.styleable.CustomProgressBar_second_color,
            DEFAULT_BORDER_COLOR);
    mCircleWidth = a.getDimensionPixelSize(
            R.styleable.CustomProgressBar_circle_width, 10);
    mSpeed = a.getInteger(R.styleable.CustomProgressBar_speed, 10);
    a.recycle();

    init();
    new Thread(this).start();
}

private void init() {
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setStyle(Paint.Style.STROKE);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int center = getWidth() / 2; // 获取圆心x坐标
    int radius = center - mCircleWidth / 2; // 半径
    mPaint.setStrokeWidth(mCircleWidth);
    RectF oval = new RectF(center - radius, center - radius, center
            + radius, center + radius);
    if (isFirst) {
        mPaint.setColor(mFirstColor);
        canvas.drawCircle(center, center, radius, mPaint);
        mPaint.setColor(mSecondColor);
        canvas.drawArc(oval, -90, mProgress, false, mPaint);
    } else {
        mPaint.setColor(mSecondColor);
        canvas.drawCircle(center, center, radius, mPaint);
        mPaint.setColor(mFirstColor);
        canvas.drawArc(oval, -90, mProgress, false, mPaint);
    }
}

@Override
public void run() {
    while (true) {
        mProgress++;
        if (mProgress == 360) {
            mProgress = 0;
            if (!isFirst)
                isFirst = true;
            else
                isFirst = false;
        }
        postInvalidate();
        try {
            Thread.sleep(mSpeed);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

}
  • 使用

http://blog.csdn.net/column/details/androidcustomview.html

你可能感兴趣的:(Android_CustomView)