a303.bmp  

 

首先在layout裡面定義gallery元件:

Xml程式碼
  • <?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调用:

      Java代码 复制代码 收藏代码
      • 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();   
      •             }   
      •         });   
      •   
      •   
      •     }   
      • }  

         

        程式碼下載TestGallery.rar (1.3 MB)

        arrow
        arrow
          全站熱搜

          戮克 發表在 痞客邦 留言(0) 人氣()