首页 > 技术文章 > Android ImageButton的使用。

luxishi 2016-02-12 17:47 原文

1.首先是范例代码,一个基本的ImageButton响应。

 1 package com.example.arlxsdemo;
 2 
 3 import android.graphics.Bitmap;
 4 import android.graphics.drawable.BitmapDrawable;
 5 import android.os.Bundle;
 6 import android.support.v7.app.ActionBarActivity;
 7 import android.view.MotionEvent;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.ImageButton;
11 
12 public class abc extends ActionBarActivity{
13     private ImageButton Btn_collect;//声明
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18         
19         Btn_collect=(ImageButton)findViewById(R.id.collect);//与布局文件的ImageButton绑定
20         
21         Btn_collect.setOnClickListener(Btn_collectOnClick);//监听点击事件
22         Btn_collect.setOnTouchListener(Btn_collectOnTouch);//监听触摸事件
23         
24     }
25     /**
26      * 点击响应
27      */
28        private ImageButton.OnClickListener Btn_collectOnClick=new ImageButton.OnClickListener(){
29             public void onClick(View v){
30                 
31             }
32         };  
33      /**
34       * 触摸响应
35      */
36         private ImageButton.OnTouchListener Btn_collectOnTouch=new ImageButton.OnTouchListener(){
37             public boolean onTouch(View v,MotionEvent event){
38                   if(event.getAction() == MotionEvent.ACTION_UP){  
39                   
40                   }   
41                   if(event.getAction() == MotionEvent.ACTION_DOWN){  
42                  
43                   }  
44                 return true;
45             }
46         };     
47 }
基本响应范例

 

2.如果ImageButton如下图所示是不规则按钮,我们希望当点击到四个边角处的透明区域不响应时可以使用触摸事件监听。

修改如下。

 1 private ImageButton.OnTouchListener Btn_collectOnTouch=new ImageButton.OnTouchListener(){
 2             public boolean onTouch(View v,MotionEvent event){
 3                 /**
 4                  * 判断点击区域是否为透明区域,如果是,则不进行响应
 5                  */
 6                  Bitmap bitmap = ((BitmapDrawable)Btn_collect.getDrawable()).getBitmap();
 7                  if(bitmap.getPixel((int)(event.getX()),((int)event.getY()))==0) 
 8                    return true;
 9                  
10                   if(event.getAction() == MotionEvent.ACTION_UP){//抬起
11                   
12                   }   
13                   if(event.getAction() == MotionEvent.ACTION_DOWN){//按下图片  
14                  
15                   }  
16                 return true;
17             }
18         };
不规则按钮忽略透明区域

3.更换ImageButton的图片。

Btn_collect.setImageDrawable(getResources().getDrawable(R.drawable.img2));  

4.src中动态改变控件的大小。由于动态转换安卓默认用的单位为px,而控件一般用的是dp,所以需要创建dp和px的转换类。

 1 public class NumberChange {
 2     /** 
 3      * 根据手机的分辨率从 dp 的单位 转成为 px(像素) ,dp=dip
 4      */  
 5     public static int diptopx(Context context, float dpValue) {  
 6         final float scale = context.getResources().getDisplayMetrics().density;  
 7         return (int) (dpValue * scale + 0.5f);  
 8     }  
 9   
10     /** 
11      * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
12      */  
13     public static int pxtodip(Context context, float pxValue) {  
14         final float scale = context.getResources().getDisplayMetrics().density;  
15         return (int) (pxValue / scale + 0.5f);  
16     }  
17 }

然后来写控制控件大小的方法,注意这里的控件的父布局是什么就将RelativeLayout置换为相应的布局。

1  public void SetControlSize(View v,int width,int height)
2     {
3          RelativeLayout.LayoutParams LayoutParams =(RelativeLayout.LayoutParams) v.getLayoutParams(); //取控件View当前的布局参数
4          LayoutParams.height =NumberChange.diptopx(context,height);// 控件的高强制设成xxxdp      
5          LayoutParams.width = NumberChange.diptopx(context,width);// 控件的高强制设成xxxdp  
6          v.setLayoutParams(LayoutParams); //使设置好的布局参数应用到控件
7     }

 

推荐阅读