Android基本组件——1.Button、ImageView等的按下效果

本文介绍了如何在Android中为Button、ImageView等组件设置按下效果,主要通过XML中的selector实现,包括shape的使用如solid、stroke、corners、padding和gradient,并提及代码实现的两种方式。同时,文章提到了ImageView在设置点击效果时的注意事项。

有关Button、ImageView、ImageButton、TextView等组件都可以设置它们的按下效果,有两种解决方案,一是在代码里面写,二是在xml中写,个人偏向于在xml写。


一:在xml中,可以用selector完成,例如:

一个Button平时状态:按下的状态:

drawable中新建一个xml文件:shape_common_radiusbtn,控制背景色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item android:state_pressed="true">
        <shape>
            <corners android:radius="5dp" />
            <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
            <stroke android:width="1dp" android:color="@color/text_brown_d7c093" />
            <solid android:color="@color/text_brown_d7c093" />
        </shape></item>
 
    <item>
        <shape>
            <corners android:radius="5dp" />
            <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
            <stroke android:width="1dp" android:color="@color/text_brown_d7c093" />
            <solid android:color="@color/white" />
        </shape></item>
 
</selector>

一些shape的使用:

solid:实心,就是填充的意思
android:color指定填充的颜色

stroke:描边
android:width="2dp" 描边的宽度,android:color 描边的颜色。
我们还可以把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp" 
android:dashGap="3dp"
其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。

corners:圆角
android:radius为角的弧度,值越大角越圆。
我们还可以把四个角设定成不同的角度,方法为:
<corners 
        android:topRightRadius="20dp"    右上角
        android:bottomLeftRadius="20dp"    右下角
        android:topLeftRadius="1dp"    左上角
        android:bottomRightRadius="0dp"    左下角
 />
这里有个地方需要注意,bottomLeftRadius是右下角,而不是左下角,这个有点郁闷,不过不影响使用,记得别搞错了就行。
还有网上看到有人说设置成0dp无效,不过我在测试中发现是可以的,我用的是2.2,可能修复了这个问题吧,如果无效的话那就只能设成1dp了。

padding:间隔
这个就不用多说了,XML布局文件中经常用到。


gradient:渐变
android:startColor和android:endColor分别为起始和结束颜色,android:angle是渐变角度,必须为45的整数倍
另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"。

            <gradient
                android:startColor="#ff8c00"
                android:endColor="#FFFFFF"
                android:angle="270" />

这是背景色的改变,字颜色的改变还要再建一个:select_common_text_brownd

1
2
3
4
5
6
7
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
 
<item android:state_pressed="true" android:color = "@color/text_white_ffffff">
 </item>
<item android:state_pressed="false" android:color="@color/text_brown_d7c093">
</item>
</selector>

这样就可以了,建一个style:

1
2
3
4
5
<style name="CommonButtonRadiusBrownd" parent="@android:style/Widget.Button">
        <item name="android:textColor">@drawable/select_common_text_brownd</item>
        <item name="android:background">@drawable/shape_common_radiusbtn</item>
        <item name="android:textSize">16sp</item>
    </style>

在Button中引用:

1
2
3
4
5
6
7
<Button
               android:id="@+id/buttonEnable"
               style="@style/CommonButtonRadiusBrownd"
               android:layout_width="100dp"
               android:layout_height="35dp"
               android:layout_marginLeft="300dp"
               android:text="有效" />


如果只是简单的Button上面的图片切换,可以直接简单的写一个更改图片的selector:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version=”1.0″ encoding=”utf-8″?>
<selector xmlns:android=”http://schemas.android.com/apk/res/android“>  
<item android:state_focused=”true”     
         android:state_pressed=”false”   
         android:drawable=”@drawable/yellow” /> 
<item android:state_focused=”true”     
          android:state_pressed=”true”    
          android:drawable=”@drawable/green” />
<item android:state_focused=”false”     
          android:state_pressed=”true
          android:drawable=”@drawable/blue” />
<item android:drawable=”@drawable/grey” />
</selector>


特注:其他控件的效果,比如ImageView,也可以通过这种方法实现,但是由于ImageView默认是没焦点,不可点击的,需要自己更改(需要点击就设置android:clickable="true" , 需要能够选中就设置android:focusable="true" )。 

我在项目里面直接给ImageView的selector:android:state_pressed不起作用,给了android:state_focused="false"才起作用,不太明白!


selector属性用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8" ?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默认时的背景图片--> 
  <item android:drawable="@drawable/pic1" />   
<!-- 没有焦点时的背景图片 --> 
  <item android:state_window_focused="false"  
        android:drawable="@drawable/pic1" />  
<!-- 非触摸模式下获得焦点并单击时的背景图片 --> 
  <item android:state_focused="true" android:state_pressed="true"   android:drawable= "@drawable/pic2" />
<!-- 触摸模式下单击时的背景图片--> 
<item android:state_focused="false" android:state_pressed="true"   android:drawable="@drawable/pic3" /> 
<!--选中时的图片背景--> 
  <item android:state_selected="true"   android:drawable="@drawable/pic4" />  
<!--获得焦点时的图片背景--> 
  <item android:state_focused="true"   android:drawable="@drawable/pic5" />  
</selector>

如果在listView中引用selector时:

(1)在ListView中添加如下属性代码:android:listSelector="@drawable/mylist_view"

(2)在ListView的item界面中添加如下属性代码:android:background="@drawable/mylist_view"

(3)利用JAVA代码直接编写:

Drawable drawable = getResources().getDrawable(R.drawable.mylist_view); 

listView.setSelector(drawable);



二、用代码实现:

1、通过StateListDrawable定义Button背景

以下这个方法也可以把你的图片数组传过来,以StateListDrawable来设置图片状态,来表现button的各中状态。未选 中,按下,选中效果。 

1
2
3
4
5
6
7
8
9
10
11
12
public StateListDrawable setbg(Integer[] mImageIds) {
           StateListDrawable bg = new StateListDrawable();
           Drawable normal = this.getResources().getDrawable(mImageIds[0]);
           Drawable selected = this.getResources().getDrawable(mImageIds[1]);
           Drawable pressed = this.getResources().getDrawable(mImageIds[2]);
           bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
           bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
           bg.addState(View.ENABLED_STATE_SET, normal);
           bg.addState(View.FOCUSED_STATE_SET, selected);
           bg.addState(View.EMPTY_STATE_SET, normal);
           return bg;
       }


这种方法没有试过,感觉原理和selector一样


2、如果是ImageView,可以在代码里面用它的触摸事件来切换图片:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ImageButton btn = (ImageButton) findViewById(R.id.button);
         
        //to set its default *.png
        btn.setBackgroundResource(R.drawable.play);
        btn.setOnTouchListener(new ImageButton.OnTouchListener(){
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // TODO Auto-generated method stub
                if(arg1.getAction() == MotionEvent.ACTION_DOWN){
                    arg0.setBackgroundResource(R.drawable.play_down);
                }
                else if(arg1.getAction() == MotionEvent.ACTION_UP){
                    arg0.setBackgroundResource(R.drawable.play);
                }
                 
                return false;
            }
             
        });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值