Android 游戏开发之选关画面

在游戏开发中,往往要提供选关的页面,选择关卡可以简单地使用listView,如果想效果好一点,可以选择 用gallery控件。Gallery控件的使用在api demo里面有很详尽的用法介绍,如果不想看api demo,下面有我精简了的代码:

程序的效果是可以拖动图片,单击选择。

 

首先在layout里面定义gallery控件:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12. <Gallery   
  13.     android:id="@+id/Gallery01"   
  14.     android:layout_width="fill_parent"   
  15.     android:layout_height="wrap_content">  
  16. </Gallery>  
  17. </LinearLayout>  

 

再定义Adapter,这个类是用来控制gallery的图片源等操作的。

Java代码   收藏代码
  1. package com.ray.test;  
  2.   
  3. import android.content.Context;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6. import android.widget.BaseAdapter;  
  7. import android.widget.Gallery;  
  8. import android.widget.ImageView;  
  9.   
  10. public class ImageAdapter extends BaseAdapter {  
  11.     private Context mContext; //define Context   
  12.   
  13.     private Integer[] mImageIds = { //picture source  
  14.             R.drawable.p1,  
  15.             R.drawable.p2,  
  16.             R.drawable.p3,  
  17.             R.drawable.p4,  
  18.             R.drawable.p5,  
  19.             R.drawable.p6,  
  20.             R.drawable.p7,  
  21.             R.drawable.p8,  
  22.     };  
  23.   
  24.     public ImageAdapter(Context c) { //define ImageAdapter  
  25.         mContext = c;  
  26.     }  
  27.   
  28.     //get the picture number  
  29.     public int getCount() {   
  30.         return mImageIds.length;  
  31.     }  
  32.       
  33.     public Object getItem(int position) {  
  34.         return position;  
  35.     }  
  36.   
  37.     public long getItemId(int position) {  
  38.         return position;  
  39.     }  
  40.   
  41.     public View getView(int position, View convertView, ViewGroup parent) {  
  42.         ImageView i = new ImageView(mContext);  
  43.         i.setImageResource(mImageIds[position]);//set resource for the imageView  
  44.         i.setLayoutParams(new Gallery.LayoutParams(192192));//layout  
  45.         i.setScaleType(ImageView.ScaleType.FIT_XY);//set scale type  
  46.         return i;  
  47.     }  
  48. }  

 

最后是Activity调用:

Java代码   收藏代码
  1. package com.ray.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.AdapterView;  
  7. import android.widget.Gallery;  
  8. import android.widget.Toast;  
  9. import android.widget.AdapterView.OnItemClickListener;  
  10.   
  11. public class TestGallery extends Activity {  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         Gallery g = (Gallery) findViewById(R.id.Gallery01);//get Gallery component  
  17.         g.setAdapter(new ImageAdapter(this));//set image resource for gallery  
  18.   
  19.         //add listener  
  20.         g.setOnItemClickListener(new OnItemClickListener() {  
  21.             public void onItemClick(AdapterView parent, View v, int position, long id) {  
  22.                 //just a test,u can start a game activity  
  23.                 Toast.makeText(TestGallery.this"" + position, Toast.LENGTH_SHORT).show();  
  24.             }  
  25.         });  
  26.   
  27.   
  28.     }  


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值