Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Android游戏开发系统控件-ImageButton

Android游戏开发系统控件-ImageButtonImageButton与Button类似,区别在于ImageButton可以自定义一张图片作为一个按钮;也正因为使用图片代替了按钮,所以ImageButton按下和抬起的样式效果需要自定义。下面为学习ImageButton做的的实例:创建ImageButton项目模拟器运行效果截图:按下按钮:抬起按钮:项目源码:main.xml修改如下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7. <ImageButton   
  8.     android:layout_width="wrap_content"  
  9.     android:layout_height="wrap_content"  
  10.     android:id="@+id/imageBtn"  
  11.     android:background="@drawable/nopress"/>  
  12.   
  13. </LinearLayout>  
ImageButtonActivity.java代码修改如下:
  1. package com.ImageButton;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.MotionEvent;  
  6. import android.view.View;  
  7. import android.view.View.OnTouchListener;  
  8. import android.widget.ImageButton;  
  9.   
  10. public class ImageButtonActivity extends Activity {  
  11.     private ImageButton Ibtn;  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         Ibtn = (ImageButton)findViewById(R.id.imageBtn);  
  18.         //为图片按钮添加触屏监听   
  19.         Ibtn.setOnTouchListener(new OnTouchListener(){  
  20.             public boolean onTouch(View v,MotionEvent event){  
  21.                 //当前用户为按下   
  22.                 if(event.getAction()==MotionEvent.ACTION_DOWN){  
  23.                     //设置图片按钮背景图   
  24.                       
  25.                     Ibtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.press));     
  26.                 //用户当前为抬起   
  27.                 }else if(event.getAction()==MotionEvent.ACTION_UP){  
  28.                       
  29.                     Ibtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.nopress));  
  30.                 }  
  31.                 return false;  
  32.             }     
  33.         });  
  34.     }  
  35. }  
更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11