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" />


其它控件也可以使用


0 0
原创粉丝点击