自定义view之自定义属性

1.首先在res的values文件下新建一个名为attrs.xml文件
在该xml文件中编写我们需要的属性



    
        
        
        
        
        
    

declare-styleable后面的name必须要与接下来要自定义的view名一致。
attr 后面的name表示需要自定义的属性,format表示这些属性的类型
2.新建一个类继承textview

public class MyTextView extends TextView {

    private Paint paint;
    private float radius ;
    private boolean isOver = false;
    private float frameWidth ;
    private int color ;
    private Paint framePaint = new Paint();
    private int frameColor;
    private boolean isClick;
    private RectF rectF;

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

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        setClickable(true);
        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.MyTextView);
        radius = ta.getInteger(R.styleable.MyTextView_radius,10);
        color = ta.getColor(R.styleable.MyTextView_color,Color.parseColor("#00000000"));
        frameColor = ta.getColor(R.styleable.MyTextView_frameColor,Color.parseColor("#008577"));
        isOver = ta.getBoolean(R.styleable.MyTextView_isOver,false);
        frameWidth = ta.getInteger(R.styleable.MyTextView_frameWidth,0);
        ta.recycle();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

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


        paint.setColor(color);
        rectF = new RectF(frameWidth/2,frameWidth/2,getWidth()-frameWidth/2,getHeight()-frameWidth/2);
        canvas.drawRoundRect(rectF,radius,radius,paint);
        if(isOver){
            framePaint.setStyle(Paint.Style.STROKE);
            framePaint.setStrokeWidth(frameWidth);
            framePaint.setAntiAlias(true);
            framePaint.setDither(true);
            framePaint.setColor(frameColor);
            RectF frameRectF = new RectF(frameWidth/2,frameWidth/2,getWidth()-frameWidth/2,getHeight()-frameWidth/2);
            canvas.drawRoundRect(frameRectF,radius,radius,framePaint);
        }


        if (isClick) {
            paint.setColor(Color.parseColor("#22000000"));
            canvas.drawRoundRect(rectF,radius,radius,paint);
        }


    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == event.ACTION_DOWN|| event.getAction() == event.ACTION_MOVE) {
            isClick = true;
        } else {
            isClick = false;
        }

        invalidate();

        return super.onTouchEvent(event);

    }
}

其中以下代码用于获取自定义属性的值

TypedArray ta = context.obtainStyledAttributes(attrs,
          R.styleable.MyTextView);
radius = ta.getInteger(R.styleable.MyTextView_radius,10);
color = ta.getColor(R.styleable.MyTextView_color,Color.parseColor("#00000000"));
frameColor = ta.getColor(R.styleable.MyTextView_frameColor,Color.parseColor("#008577"));
isOver = ta.getBoolean(R.styleable.MyTextView_isOver,false);
frameWidth = ta.getInteger(R.styleable.MyTextView_frameWidth,0);
ta.recycle();

3.在Activity中使用



    

你可能感兴趣的