论坛首页 移动开发技术论坛

android游戏开发之选关画面

浏览 22952 次
该帖已经被评为良好帖
作者 正文
   发表时间:2009-09-21  

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

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

 

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Gallery 
	android:id="@+id/Gallery01" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content">
</Gallery>
</LinearLayout>

 

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

package com.ray.test;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
	private Context mContext; //define Context 

    private Integer[] mImageIds = { //picture source
            R.drawable.p1,
            R.drawable.p2,
            R.drawable.p3,
            R.drawable.p4,
            R.drawable.p5,
            R.drawable.p6,
            R.drawable.p7,
            R.drawable.p8,
    };

    public ImageAdapter(Context c) { //define ImageAdapter
        mContext = c;
    }

    //get the picture number
    public int getCount() { 
        return mImageIds.length;
    }
    
    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        i.setImageResource(mImageIds[position]);//set resource for the imageView
        i.setLayoutParams(new Gallery.LayoutParams(192, 192));//layout
        i.setScaleType(ImageView.ScaleType.FIT_XY);//set scale type
        return i;
    }
}

 

最后是Activity调用:

package com.ray.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class TestGallery extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Gallery g = (Gallery) findViewById(R.id.Gallery01);//get Gallery component
        g.setAdapter(new ImageAdapter(this));//set image resource for gallery

        //add listener
        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
            	//just a test,u can start a game activity
                Toast.makeText(TestGallery.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });


    }
}

 

   发表时间:2009-09-22  
很不错,raymondlueng兄总是有惊喜。
0 请登录后投票
   发表时间:2009-09-22  
pure 写道
很不错,raymondlueng兄总是有惊喜。

谢谢,谢谢你的鼓励!
0 请登录后投票
   发表时间:2009-09-23  
不错, 谢谢分享 ^_^)Y
0 请登录后投票
   发表时间:2009-09-23  
不错的东西~~~
0 请登录后投票
   发表时间:2009-09-23  
哎,我对游戏方面总是不能理解,那些屏幕的x,y呀,感觉很难读懂,感谢您的分享!
0 请登录后投票
   发表时间:2009-09-26  
在你的部落格确实能学到不少东西。 以后要常来学习。
0 请登录后投票
   发表时间:2009-09-27  
多谢楼主的文章。我更习惯使用listView再加imageview...好处是我可以加一些文字的注解和历史信息。Gallery的效果主要在drawable的设计是不是直观。
0 请登录后投票
   发表时间:2009-12-26  
下载下来了,试运行,效果杠杠的,谢谢楼主的分享!
0 请登录后投票
   发表时间:2009-12-27  
Gallery拖动的方向是不是只有横向拖动?可以垂直纵向拖动吗?
1 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics