android 中ImageButton按下改变背景图片的效果
ImageButton btn = (ImageButton)findViewById(R.id.imageButton1); btn.setOnTouchListener(new View.OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ //重新设置按下时的背景图片 ((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.android_btn_pressed)); }else if(event.getAction() == MotionEvent.ACTION_UP){ //再修改为抬起时的正常图片 ((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.android_btn)); } return false; } }); |
<?xml version="1.0" encoding="utf-8"?> <selectorxmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"android:drawable="@drawable/android_btn" /> <item android:state_focused="true"android:drawable="@drawable/android_btn" /> <item android:state_pressed="true"android:drawable="@drawable/android_btn_pressed" /> </selector> |
<ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@layout/image_btn_press" /> |
posted on 2015-10-28 17:49 qingchen1984 阅读(6824) 评论(0) 编辑 收藏 举报