Android游戏开发系统控件-ImageButton

来源:互联网 发布:独立网店源码 编辑:程序博客网 时间:2024/06/06 09:35

Android游戏开发系统控件-ImageButton

ImageButton与Button类似,区别在于ImageButton可以自定义一张图片作为一个按钮;

也正因为使用图片代替了按钮,所以ImageButton按下和抬起的样式效果需要自定义。

下面为学习ImageButton做的的实例:

创建ImageButton项目

模拟器运行效果截图:

按下按钮:

 

抬起按钮:

 

项目源码:

main.xml修改如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" ><ImageButton     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/imageBtn"    android:background="@drawable/nopress"/></LinearLayout>

 

ImageButtonActivity.java代码修改如下:

package com.ImageButton;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageButton;public class ImageButtonActivity extends Activity {private ImageButton Ibtn;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Ibtn = (ImageButton)findViewById(R.id.imageBtn);        //为图片按钮添加触屏监听        Ibtn.setOnTouchListener(new OnTouchListener(){        public boolean onTouch(View v,MotionEvent event){        //当前用户为按下        if(event.getAction()==MotionEvent.ACTION_DOWN){        //设置图片按钮背景图                Ibtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.press));        //用户当前为抬起        }else if(event.getAction()==MotionEvent.ACTION_UP){                Ibtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.nopress));        }        return false;        }        });    }}


 


原创粉丝点击