Android控件開發之Gallery

 GalleryAndroid中的圖片庫控件。可以瀏覽自己的圖片相冊。

 效果圖

 

 

 本程序main.xml源碼

 

本程序ImageAdapter類源碼

package com.sx.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/gallery"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"
    android:spacing="2px" />  
    <!-- android:spacing="2px"用來設置圖片之間的間距 --> 
    
</LinearLayout>




JAVA源碼

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 context;  
    
         private int[] MyImageIDs =  
         {   
		R.drawable.img1,
		R.drawable.img2,
		R.drawable.img3,
		R.drawable.img4,
		R.drawable.img5,
		R.drawable.img6,
		R.drawable.img7,
		R.drawable.img8,
		R.drawable.img9,
		R.drawable.img10,
		R.drawable.img11,
		R.drawable.img12,
		R.drawable.img13,
		R.drawable.img14,
		R.drawable.img15,
		R.drawable.img16,
		R.drawable.img17,
		R.drawable.img18,
		R.drawable.img19,
		R.drawable.img20
         };  
    
	public ImageAdapter(Context context)  
	{  
		this.context = context;   
	}  

	// 獲取圖片個數  
	public int getCount()  
	{    
		return MyImageIDs.length;
		//return Integer.MAX_VALUE;
	}  
	// 獲取圖片在庫中的位置  
	public Object getItem(int position)  
	{   
		return position;  
	}  

	// 獲取圖片在庫中的ID  
	public long getItemId(int id)  
	{   
		return id;  
	}  

	public View getView(int position, View convertView, ViewGroup parent)  
	{  
		ImageView imageview = new ImageView(this.context);  

		// 給ImageView設置資源   
		imageview.setImageResource(this.MyImageIDs[position]);  
		// 設置佈局 圖片120*120   
		imageview.setLayoutParams(new Gallery.LayoutParams(120, 120));   
		// 設置顯示比例類型   
		imageview.setScaleType(ImageView.ScaleType.FIT_XY);

		//imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);   
		return imageview;   
	}  
}  

//要想實現圖片無限循環可拖動修改getCount()  函數改爲返回值爲return Integer.MAX_VALUE;
//imageview.setImageResource(this.MyImageIDs[position] % MyImageIDs.length.);  即可

 

Activity源碼

package com.sx.Gallery;

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

public class GalleryActivity extends Activity 
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		CreateGallery();
	}
    
         private void CreateGallery()
         {
    	    //獲得Gallery對象   
               Gallery gellery=(Gallery)this.findViewById(R.id.gallery); 
           
             //添加ImageAdapter給Gallery對象 注意哦Gallery類並沒有setAdapter這個方法 這個方法是從AbsSpinner類繼承的  
               gellery.setAdapter(new ImageAdapter(this));  
  
             //設置Gallery的背景圖片   
               //gellery.setBackgroundResource(R.drawable.bg0);   
  
             //設置Gallery的事件監聽   
              gellery.setOnItemClickListener(new GalleryItemListener());   
  
         }   
         class GalleryItemListener implements OnItemClickListener
         {   
             public void onItemClick(AdapterView<?> parent, View view, int position,  long id)
             {   
                 Toast.makeText(GalleryActivity.this, "你選擇了" + (position + 1) + " 號圖片", Toast.LENGTH_SHORT).show();   
             }   

         }
}



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章