android实现TextView、ImageView等按压效果

20 篇文章 0 订阅
3 篇文章 0 订阅

之前有一个项目有很多按钮需要一个按压的状态,之前一直用selector的xml配置的方式,但是需要这个效果的按钮太多,于是就想着琢磨一个省事点的方式

于是:

ImageView图片的按压效果实现

public class PressImageView extends ImageView {


public PressImageView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init(context, attrs);

}


public PressImageView(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();//注意回收

                

setImageDrawable(createDrawable(src_s, src_n));

}


public PressImageView(Context context) {

this(context, null);

}

              //创建drawable

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;

}

}



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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值