自定义View之自定义按键圆角边框背景添加点击效果
2017-06-26 15:51
645 查看
自定义View之自定义按键圆角边框背景添加点击效果
设置边框可以参考前一篇博客
添加后效果如图
1、新添加了几个属性
相应的效果如注释
2、在定义控件中获取设置的属性
3、在设置点击效果的时候其实主要是用不同颜色的画笔进行绘制控件
if (borderWidth > 0 && borderColor != 0) {
paintBorder = new Paint();
if (isChick) {
paintBorder.setColor(clickedCornerRadiusColor);
} else {
paintBorder.setColor(borderColor);
}
paintBorder.setStyle(Paint.Style.STROKE);
paintBorder.setStrokeWidth(borderWidth);
paintBorder.setAntiAlias(true);
}
paintBg = new Paint();
if (isChick) {
paintBg.setColor(clickedBgColor);
setTextColor(clickedTextColor);
} else {
paintBg.setColor(bgColor);
setTextColor(textColor);
}
paintBg.setAntiAlias(true);
paintBg.setStyle(Paint.Style.FILL);
4、对点击效果后进行重新绘制
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isChick = !isChick;
drawBackgroud(isChick);
}
return super.onTouchEvent(event);
}
private void drawBackgroud(boolean b) {
if (b) {
paintBg.setColor(clickedBgColor);
if (borderWidth > 0 && borderColor != 0) {
paintBorder.setColor(clickedCornerRadiusColor);
}
setTextColor(clickedTextColor);
} else {
paintBg.setColor(bgColor);
if (borderWidth > 0 && borderColor != 0) {
paintBorder.setColor(borderColor);
}
setTextColor(textColor);
}
invalidate();
}
最后GitHub链接和使用方法:https://github.com/cc0819/BgButton
设置边框可以参考前一篇博客
添加后效果如图
1、新添加了几个属性
<!-- 点击状态 --> <attr name="isChick" format="boolean"/> <!--点击后背景颜色--> <attr name="clickedBgColor" format="color"/> <!--点击后边框颜色--> <attr name="clickedCornerRadiusColor" format="color"/> <!--点击后字体颜色--> <attr name="clickedTextColor" format="color"/>
相应的效果如注释
2、在定义控件中获取设置的属性
isChick = typedArray.getBoolean(R.styleable.BgButton_isChick, false); clickedBgColor = typedArray.getColor(R.styleable.BgButton_clickedBgColor, -1); clickedCornerRadiusColor = typedArray.getColor(R.styleable.BgButton_clickedCornerRadiusColor, -1); textColor = getCurrentTextColor(); clickedTextColor = typedArray.getColor(R.styleable.BgButton_clickedTextColor, -1);
3、在设置点击效果的时候其实主要是用不同颜色的画笔进行绘制控件
if (borderWidth > 0 && borderColor != 0) {
paintBorder = new Paint();
if (isChick) {
paintBorder.setColor(clickedCornerRadiusColor);
} else {
paintBorder.setColor(borderColor);
}
paintBorder.setStyle(Paint.Style.STROKE);
paintBorder.setStrokeWidth(borderWidth);
paintBorder.setAntiAlias(true);
}
paintBg = new Paint();
if (isChick) {
paintBg.setColor(clickedBgColor);
setTextColor(clickedTextColor);
} else {
paintBg.setColor(bgColor);
setTextColor(textColor);
}
paintBg.setAntiAlias(true);
paintBg.setStyle(Paint.Style.FILL);
4、对点击效果后进行重新绘制
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isChick = !isChick;
drawBackgroud(isChick);
}
return super.onTouchEvent(event);
}
private void drawBackgroud(boolean b) {
if (b) {
paintBg.setColor(clickedBgColor);
if (borderWidth > 0 && borderColor != 0) {
paintBorder.setColor(clickedCornerRadiusColor);
}
setTextColor(clickedTextColor);
} else {
paintBg.setColor(bgColor);
if (borderWidth > 0 && borderColor != 0) {
paintBorder.setColor(borderColor);
}
setTextColor(textColor);
}
invalidate();
}
最后GitHub链接和使用方法:https://github.com/cc0819/BgButton
相关文章推荐
- 自定义TextView设置边框与背景颜色添加点击事件,点击更改随机数与背景
- 自定义View之自定义按键圆角边框背景
- iOS 自定义view 是上面文字某区域有点击事件和点击背景效果
- AutoCompleteTextView控件自定义下拉框背景,点击效果,字体显示效果,匹配条件等效果
- 最方便的自定义带点击效果的圆角背景按钮
- Android在xml中设置组件风格(圆角,点击效果selector,边框,进度条风格,动画)TextView文字透明度
- 给UIImageview 添加边框 或者设置成圆角
- 给UIImageview 添加边框 或者设置成圆角
- 给 “a,b,c... 定位栏添加背景。。。。自定义 TABLEVIEW 的层
- 给图片设置圆角和圆形边框,添加点击事件
- Android使用XML的方式为背景添加渐变,描边,圆角的效果
- Android 自定义View消除锯齿实现图片旋转,添加边框及文字说明
- Android TextView富文本、超链接和添加文字背景等其他效果的实现
- UITableView Cell 点击时的带颜色背景效果,而不是选中颜色效果
- 为任意自定义的View隐藏时添加动画效果
- Android给TextView设置透明背景、圆角边框
- (转)#小美化#Android给TextView设置透明背景、圆角边框
- 给UIImageview 添加边框 或者设置成圆角
- 给UIImageview 添加边框 或者设置成圆角
- Android给TextView和EditText等控件设置透明背景、圆角边框