Android游戏开发系统控件-ImageButton

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;
        	}	
        });
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值