Android ImageButton

public class ImageButton extends ImageView java.lang.Object ↳ android.view.View ↳ android.widget.ImageView ↳ android.widget.ImageButton 直接子类 ZoomButton

ImageButton是上面带有图片的Button,与Button只是相似,按钮表面上的图像的定义是在xml文件中经过android:src或者是在java代码中经过imagebutton组件对象调用setImageResource(int a)方法来定义的。java

在xml中实现:android

<?xml version="1.0" encoding="utf-8"?>
<ImageButton 
	android:id="@+id/login_btn_id"
	android:layout_height="wrap_content"
	android:src="@drawable/button_2" //不是系统图片
    或者	Android:src="@android:drawable/sym_call_incoming" //系统自带的图片
		  
android:layout_margin="5px"
/>

在代码中实现:ide

//drawable下的图片 

m_ImageButton.setImageDrawable(getResources().getDrawable(R.drawable.my_button)); 

//系统自带的图片 
 m_ImageButton.setImageDrawable(getResources().getDrawable(Android.R.drawable.sym_call_incoming));

ImageButton设置图片方式,有如下三种:布局


setImageBitmap(Bitmap bm) 
setImageDrawable(Drawable drawable) 
setImageResource(int resId)
设置透明背景能够经过设置background完成

android:background="#00000000"便可spa

半透明 code

android:background="#7F000000"便可。orm

表示不一样的按钮状态,你可以为每个状态定义一个相对应的图片,一个很简单的方式就是经过xml绘制“selectorxml

例如:对象

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <!-- pressed 按下去--> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <!-- focused 焦点停留--> <item android:drawable="@drawable/button_normal" /> <!-- default正常默认 --> </selector>

元素的顺序是很重要的,由于他们是按顺序执行的,这就是为何normal按钮放在在最后了,由于在android:state_pressed and android:state_focused评估失效以后normal才被应用的。事件

state_enabled 是否有效
state_focused 是否聚焦
state_pressed 是否被按下
其中state_focused 和 state_pressed 可自由有以下4种组合

android:state_focused="true" android:state_pressed="true"
android:state_focused="true" android:state_pressed="false"
android:state_focused="false" android:state_pressed="true"
android:state_focused="false" android:state_pressed="false"

方法:

protected boolean onSetAlpha (int alpha);//设置透明度,参数alpha的取值范围是0到255,若是视图能够得出制定的alphe就会返回一个true

imagebutton.setVisibility(View.GONE);//隐藏ImageButton1 imagebutton.setVisibility(View.VISIBLE);//显示ImageButton2 imagebutton.setImageResource(R.drawable.icon2);//设置内容

ImageButton按下和释放背景图片改变事件:

imageButton.setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ //更改成按下时的背景图片 v.setBackgroundResource(R.drawable.pressed); }else if(event.getAction() == MotionEvent.ACTION_UP){ //改成抬起时的图片 v.setBackgroundResource(R.drawable.released); } return false; } }); imageButton.setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ //更改成按下时的背景图片 v.setBackgroundResource(R.drawable.pressed); }else if(event.getAction() == MotionEvent.ACTION_UP){ //改成抬起时的图片 v.setBackgroundResource(R.drawable.released); } return false; } }); 

设置背景色:

m_ll.setBackgroundColor(Color.rgb(127,127,127)); m_ll.setBackgroundColor(Color.TRANSPARENT); 

三:以上方式比较简单,可是须要不少的图片和布局文件,若是项目中的图片按钮比较多,那就很浪费资源。下面的方式使用矩阵颜色滤镜。

颜色过滤矩阵是一个4x5的矩阵, 四行分别是 红色通道值,绿色通道值,蓝色通道值和alpha通道值。五列分别是 对应通道的红色值,绿色值,蓝色值,alpha值和偏移量。

RGBAlpha的终值计算方法以下:

Red通道终值 = a[0] * srcR + a[1] * srcG + a[2] * srcB + a[3] * srcA + a[4]

Green通道终值 = a[5] * srcR + a[6] * srcG + a[7] * srcB + a[8] * srcA + a[9]

Blue通道终值 = a[10] * srcR + a[11] * srcG + a[12] * srcB + a[13] * srcA + a[14]

Alpha通道终值 = a[15] * srcR + a[16] * srcG + a[17] * srcB + a[18] * srcA + a[19]

备注:

srcR为原图Red通道值,srcG为原图Green通道值,srcB为原图Blue通道值,srcA为原图Alpha通道值。

每一个通道的源值和终值都在0255的范围内。即便计算结果大于255或小于0,值都将被限制在0255的范围内。

实现代码以下:

采用Drawable的颜色过滤:

/** * 按下这个按钮进行的颜色过滤 */ public final static float[] BT_SELECTED=new float[] { 2, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 1, 0 }; /** * 按钮恢复原状的颜色过滤 */ public final static float[] BT_NOT_SELECTED=new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 }; /** * 按钮焦点改变 */ public final static OnFocusChangeListener buttonOnFocusChangeListener=new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED)); v.setBackgroundDrawable(v.getBackground()); } else { v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED)); v.setBackgroundDrawable(v.getBackground()); } } }; /** * 按钮触碰按下效果 */ public final static OnTouchListener buttonOnTouchListener=new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED)); v.setBackgroundDrawable(v.getBackground()); } else if(event.getAction() == MotionEvent.ACTION_UP){ v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED)); v.setBackgroundDrawable(v.getBackground()); } return false; } }; /** * 设置图片按钮获取焦点改变状态 * @param inImageButton */ public final static void setButtonFocusChanged(View inView) { inView.setOnTouchListener(buttonOnTouchListener); inView.setOnFocusChangeListener(buttonOnFocusChangeListener); } /** * 按下这个按钮进行的颜色过滤 */ public final static float[] BT_SELECTED=new float[] { 2, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 1, 0 }; /** * 按钮恢复原状的颜色过滤 */ public final static float[] BT_NOT_SELECTED=new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 }; /** * 按钮焦点改变 */ public final static OnFocusChangeListener buttonOnFocusChangeListener=new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED)); v.setBackgroundDrawable(v.getBackground()); } else { v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED)); v.setBackgroundDrawable(v.getBackground()); } } }; /** * 按钮触碰按下效果 */ public final static OnTouchListener buttonOnTouchListener=new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED)); v.setBackgroundDrawable(v.getBackground()); } else if(event.getAction() == MotionEvent.ACTION_UP){ v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED)); v.setBackgroundDrawable(v.getBackground()); } return false; } }; /** * 设置图片按钮获取焦点改变状态 * @param inImageButton */ public final static void setButtonFocusChanged(View inView) { inView.setOnTouchListener(buttonOnTouchListener); inView.setOnFocusChangeListener(buttonOnFocusChangeListener); } 
相关文章
相关标签/搜索