自定义View典型写法

来源:互联网 发布:淘宝二手手机苹果 编辑:程序博客网 时间:2024/06/03 16:37

自定义View

继承View或者现有控件, 根据需求加入自己的逻辑

使用自定义View

xml中使用全路径名

自定义属性

res/value目录创建 attrs.xml    <declare-styleable/>        创建自定义属性            <attr name format/>创建的属性可以在自定义View里面引用             

使用自定义属性

    添加app命名空间        xmlns:app="http://schemas.android.com/apk/res-auto"        以app开头使用自定义属性

java代码中获取自定义属性

TypedArray typedArray = context.obtainStyledAttributes

TypedArray可以使用很多getXXX()方法获取到自定义属性的值
用完记得typedArray.recycle();

支持wrap_content

判断宽/高测量模式, 是MeasureSpec.AT_MOST就设置默认宽高

支持padding

onDraw中计算padding

示例:

attrs.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="CircleView">        <attr name="circle_color" format="color|reference" />    </declare-styleable></resources>

CircleView.java

public class CircleView extends View {    public static final int DEFAULT_WIDTH = 200;    public static final int DEFAULT_HEIGHT = 200;    private int mColor = Color.RED;    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);    public CircleView(Context context) {        this(context, null);    }    public CircleView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public CircleView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleView);        mColor = typedArray.getColor(R.styleable.CircleView_circle_color, Color.RED);            typedArray.recycle();        init();    }    private void init() {        mPaint.setColor(mColor);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);        if (widthSpecMode == MeasureSpec.AT_MOST && widthSpecMode == MeasureSpec.AT_MOST) {            setMeasuredDimension(dp2px(DEFAULT_WIDTH), dp2px(DEFAULT_HEIGHT));        } else if (widthSpecMode == MeasureSpec.AT_MOST) {            setMeasuredDimension(dp2px(DEFAULT_WIDTH), heightSpecSize);        } else if (heightSpecMode == MeasureSpec.AT_MOST) {            setMeasuredDimension(widthSpecSize, dp2px(DEFAULT_HEIGHT));        }    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        int paddingLeft = getPaddingLeft();        int paddingRight = getPaddingRight();        int paddingTop = getPaddingTop();        int paddingBottom = getPaddingBottom();        int width = getWidth() - paddingLeft - paddingRight;        int height = getHeight() - paddingTop - paddingBottom;        int radius = Math.min(width, height) / 2;        canvas.drawCircle(paddingLeft + width / 2, paddingTop + height / 2, radius, mPaint);    }    public int dp2px(float dpValue) {        final float scale = getContext().getResources().getDisplayMetrics().density;        return (int) (dpValue * scale + 0.5f);    }}
阅读全文
1 0
原创粉丝点击