阅读背景:

Android开发自定义圆角带点击效果的Button

来源:互联网 
public class AnimationButton extends Button {

    private int mBackGroundColor = Color.parseColor("#ffffff");
    private int normalColor;
    private int pressedColor;
    private float round;

    //默认画笔
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public AnimationButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        //获取自定义属性
        if (attrs != null) {
            TypedArray typedArray =context.obtainStyledAttributes(attrs,R.styleable.MyRoundButton);

            normalColor = typedArray.getColor(R.styleable.MyRoundButton_normalColor,getResources().getColor(R.color.btn_blue_bg));
            pressedColor = typedArray.getColor(R.styleable.MyRoundButton_pressedColor,getResources().getColor(R.color.btn_main_pressed));
            round = typedArray.getDimension(R.styleable.MyRoundButton_round,10);

            mBackGroundColor = normalColor;
            typedArray.recycle();
        }

        //设置抗锯齿
        mPaint.setAntiAlias(true);
        //设置防抖动
        mPaint.setDither(true);
    }

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

        //画笔颜色
        mPaint.setColor(mBackGroundColor);

        RectF mBackGroundRect = new RectF(0,0,canvas.getWidth(),canvas.getHeight());
        //绘制背景 圆角矩形
        if (mBackGroundRect != null) {
            canvas.drawRoundRect(mBackGroundRect, round, round, mPaint);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        //刷新
        invalidate();
        //判断点击操作
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mBackGroundColor = normalColor;

                break;
            case MotionEvent.ACTION_MOVE:

                break;
            case MotionEvent.ACTION_UP:
                mBackGroundColor = pressedColor;

                break;
            case MotionEvent.ACTION_CANCEL:

                break;
        }
        return super.onTouchEvent(event);
    }




}
public class AnimationButton extends Button {




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: