手机版
你好,游客 登录 注册
背景:
阅读新闻

Android游戏开发系统控件-ImageButton

[日期:2012-05-13] 来源:Linux社区  作者:wwj [字体:大 中 小]

Android游戏开发系统控件-ImageButton

ImageButton与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

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款