Android开发自定义圆角带点击效果的Button
来源:互联网 发布:工程造价软件广联达 编辑:程序博客网 时间:2024/06/05 12:23
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); }}
重点:
1.invalidate(),它会调onDraw()
2.RectF mBackGroundRect = new RectF(0,0,canvas.getWidth(),canvas.getHeight())
阅读全文
0 0
- Android开发自定义圆角带点击效果的Button
- Android自定义Button的点击效果
- Android自定义Button的点击效果
- Android自定义Button点击效果
- android自定义button点击效果
- Android中的Button自定义点击效果
- Android之Button自定义点击效果
- Android中的Button、ImageButton自定义点击效果
- Android中的Button自定义点击效果
- Android中的Button自定义点击效果
- Android中Button自定义点击效果
- Android中的Button自定义点击效果
- Android中的Button自定义点击效果
- Android之Button自定义点击效果
- Android之Button自定义点击效果
- Android之Button自定义点击效果
- Android之Button自定义点击效果
- Android之Button自定义点击效果
- Hdu 5792 World is Exploding
- JSONP的跨域请求
- unity用于android上的旋转缩放脚本
- hibernate_商城关系表的建立
- 二分查找
- Android开发自定义圆角带点击效果的Button
- 习题6.15
- 【图像处理】反走样技术
- 一些感想
- HDU 4686 Arc of Dream
- HDU 1231 最大连续子序列
- spring boot 集成mybatis
- 深入理解Java并发之synchronized实现原理
- 【2017年cs231n学习笔记3】Lecture4-1 反向传播