一、基礎知識:
要實現這一效果,需要一個容器來存放Gallrey顯示的圖片,這裡使用一個繼承自BaseAdapter類的衍生類別來裝這些圖片。
我們需要監聽其事件setOnItemClickListener,從而確定使用者當前選中的是哪一張圖片。
首先,需要將所有要顯示的圖片的索引存放在一個int型數組中,然後通過setImageResource方法來設定ImageView要顯示的圖片資源,最後將
每張圖片的ImageView顯示在螢幕上。
二、代碼展示:
1."main.xml"
[html]
<?xml version="1.0" encoding="utf-8"?>
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<?xml version="1.0" encoding="utf-8"?>
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
2."Activity01.java"
[java] view plaincopyprint?package com.yarin.android.XX;
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 Activity01 extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//獲得Gallery對象
Gallery g = (Gallery) findViewById(R.id.Gallery01);
//添加ImageAdapter給Gallery對象
g.setAdapter(new ImageAdapter(this));
//設定Gallery的背景
g.setBackgroundResource(R.drawable.bg0);
//設定Gallery的事件監聽
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(Activity01.this,"你選擇了"+(position+1)+" 號圖片",
Toast.LENGTH_SHORT).show();
}
});
}
}
package com.yarin.android.XX;
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 Activity01 extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//獲得Gallery對象
Gallery g = (Gallery) findViewById(R.id.Gallery01);
//添加ImageAdapter給Gallery對象
g.setAdapter(new ImageAdapter(this));
//設定Gallery的背景
g.setBackgroundResource(R.drawable.bg0);
//設定Gallery的事件監聽
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(Activity01.this,"你選擇了"+(position+1)+" 號圖片",
Toast.LENGTH_SHORT).show();
}
});
}
}
3."ImageAdapter.java"
[java]
package com.yarin.android.XX;
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
{
// 定義Context
private Context mContext;
// 定義整型數組 即圖片源
private Integer[] mImageIds =
{
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5,
R.drawable.img6,
R.drawable.img7,
R.drawable.img8,
};
// 聲明 ImageAdapter
public ImageAdapter(Context c)
{
mContext = c;
}
// 擷取圖片的個數
public int getCount()
{
return mImageIds.length;
}
// 擷取圖片在庫中的位置
public Object getItem(int position)
{
return position;
}
// 擷取圖片ID
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageview = new ImageView(mContext);
// 給ImageView設定資源
imageview.setImageResource(mImageIds[position]);
// 設定布局 圖片120×120顯示
imageview.setLayoutParams(new Gallery.LayoutParams(120, 120));
// 設定顯示比例類型
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageview;
}
}
package com.yarin.android.XX;
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
{
// 定義Context
private Context mContext;
// 定義整型數組 即圖片源
private Integer[] mImageIds =
{
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5,
R.drawable.img6,
R.drawable.img7,
R.drawable.img8,
};
// 聲明 ImageAdapter
public ImageAdapter(Context c)
{
mContext = c;
}
// 擷取圖片的個數
public int getCount()
{
return mImageIds.length;
}
// 擷取圖片在庫中的位置
public Object getItem(int position)
{
return position;
}
// 擷取圖片ID
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageview = new ImageView(mContext);
// 給ImageView設定資源
imageview.setImageResource(mImageIds[position]);
// 設定布局 圖片120×120顯示
imageview.setLayoutParams(new Gallery.LayoutParams(120, 120));
// 設定顯示比例類型
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageview;
}
}
三、效果展示: