android实现TextView、ImageView等按压效果
来源:互联网 发布:沈阳软件学院 编辑:程序博客网 时间:2024/05/02 17:18
之前有一个项目有很多按钮需要一个按压的状态,之前一直用selector的xml配置的方式,但是需要这个效果的按钮太多,于是就想着琢磨一个省事点的方式
于是:
ImageView图片的按压效果实现
public class PressImageViewextends ImageView {
public PressImageView(Context context, AttributeSet attrs, intdefStyle) {
super(context,attrs, defStyle);
init(context,attrs);
}
public PressImageView(Context context, AttributeSet attrs) {
this(context,attrs, 0);
}
private void init(Contextcontext, AttributeSet attrs) {
//获取到自定义属性
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PressImageView);
Drawable src_n = a.getDrawable(R.styleable.PressImageView_src_n);
Drawable src_s = a.getDrawable(R.styleable.PressImageView_src_s);
a.recycle();//注意回收
setImageDrawable(createDrawable(src_s,src_n));
}
public PressImageView(Context context) {
this(context,null);
}
//创建drawable
public Drawable createDrawable(Drawablepress, Drawable normal) {
StateListDrawable stateList =new StateListDrawable();
int statePressed = android.R.attr.state_pressed;
stateList.addState(newint[] { statePressed },press);
stateList.addState(newint[] {}, normal);
returnstateList;
}
}
TextView的按压效果:实现
public class PressTextView extends TextView { public PressTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context, attrs); } public PressTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } private void init(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PressImageView); Drawable src_n = a.getDrawable(R.styleable.PressImageView_src_n); Drawable src_s = a.getDrawable(R.styleable.PressImageView_src_s); a.recycle(); setBackground(createDrawable(src_s, src_n));// setImageDrawable(createDrawable(src_s, src_n)); } public PressTextView(Context context) { this(context, null); } public Drawable createDrawable(Drawable press, Drawable normal) { StateListDrawable stateList = new StateListDrawable(); int statePressed = android.R.attr.state_pressed; stateList.addState(new int[] { statePressed }, press); stateList.addState(new int[] {}, normal); return stateList; }}
布局文件的调用方式:
声明 :
xmlns:xyh="http://schemas.android.com/apk/res-auto"
ImageView的调用:
<com.xyh.view.PressImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:clickable="true" android:layout_centerVertical="true" android:src="@drawable/sure_n" xyh:src_n="@drawable/sure_n" xyh:src_s="@drawable/sure_s" />
TextView的调用:
<com.xyh.view.PressTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定"
android:clickable="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:src="@drawable/btn_sure_n"android:textSize="@dimen/font_normal"xyh:src_n="@drawable/btn_sure_n" xyh:src_s="@drawable/btn_sure_s" />
其它控件也可以使用
- android实现TextView、ImageView等按压效果
- 使用TextView实现ImageView效果
- Android 点击按压效果selector
- 旋转ImageView和TextView的效果实现
- Android基础button按压变化效果
- Android GridView 实现9宫格菜单,并实现item点击按压效果更改图片
- android ImageView实现变暗效果
- android ImageView实现变暗效果
- android ImageView实现变暗效果
- Android引入布局 GridView实现ImageView+TextView
- android Imageview Textview纯代码增加按下效果
- Android TextView实现滚动效果
- [Android] TextView实现走马灯效果
- [Android] TextView实现走马灯效果
- Android Textview实现阴影效果
- [Android] TextView实现走马灯效果
- Android:自定义ImageView实现缩放,回弹效果
- android拖动imageview实现复制效果
- Java并发编程的艺术(二)——重排序
- 为什么开源可以提高程序员的编程技能?
- 【菜鸟数据库学习日记】从头开始学MySQL(3)
- butter knife详解和使用
- React-Native应用ListView实现上拉下拉刷新效果实践
- android实现TextView、ImageView等按压效果
- 2017.1.13 周五
- C#根据文件路径获取该文件夹路径、文件名、后缀名
- codeforces 757 C. Felicity is Coming!
- 生成验证码
- Ehcache小结(一)
- 在spring boot项目中应用swagger2
- SyntaxNet是否可以实现中文语义分析
- 如何使用Windows命令行“Dos入门”(翻译)