Gallery组件主要用于横向显示图像列表,不过按常规做法。Gallery组件只能有限地显示指定的图像。也就是说,如果为Gallery组件指定了10张图像,那么当Gallery组件显示到第10张时,就不会再继续显示了。这虽然在大多数时候没有什么关系,但在某些情况下,我们希望图像显示到最后一张时再重第1张开始显示,也就是循环显示。要实现这种风格的Gallery组件,就需要对Gallery的Adapter对象进行一番改进。
以下通过Gallery模拟循环显示图像,在单击某一个Gallery组件中的图像时在下方显示一个放大的图像(使用ImageSwitcher组件)。
最终效果图如下:
一、Layout布局文件
A:主界面布局文件
View Code
<?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">
<ImageButton android:id="@+id/ibtnHead"
android:layout_height="60px" android:layout_width="60px"
android:src="@drawable/icon" android:scaleType="fitXY"></ImageButton>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:id="@+id/ibtnHead"
android:layout_height="60px" android:layout_width="60px"
android:src="@drawable/icon" android:scaleType="fitXY"></ImageButton>
</LinearLayout>
B:显示Gallery和ImageSwitcher的布局文件
View Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="@+id/img_gallery" android:layout_width="fill_parent"
android:layout_height="110px" android:layout_marginTop="2px"
android:layout_alignParentLeft="true"></Gallery>
<ImageSwitcher android:id="@+id/img_switcher"
android:layout_width="90px" android:layout_height="90px"
android:layout_centerHorizontal="true" android:layout_below="@+id/img_gallery"
></ImageSwitcher>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="@+id/img_gallery" android:layout_width="fill_parent"
android:layout_height="110px" android:layout_marginTop="2px"
android:layout_alignParentLeft="true"></Gallery>
<ImageSwitcher android:id="@+id/img_switcher"
android:layout_width="90px" android:layout_height="90px"
android:layout_centerHorizontal="true" android:layout_below="@+id/img_gallery"
></ImageSwitcher>
</RelativeLayout>
C:String文件
View Code
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="addContact_PleaseChooseImg">请选择图像</string>
</resources>
<resources>
<string name="addContact_PleaseChooseImg">请选择图像</string>
</resources>
二、设置图片资源
在drawable-hdpi文件夹下放置10个要显示的图片,如下图
三、主Activity类
View Code
package cn.moon.contact;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
public class AddContactActivity extends Activity {
ImageButton ibtnImgChoose;
AlertDialog imgChooseDialog;
Gallery gallery;
ImageSwitcher imageSwitcher;
int selectedImage;
private int[] images = { R.drawable.png0001, R.drawable.png0002,
R.drawable.png0003, R.drawable.png0004, R.drawable.png0005,
R.drawable.png0006, R.drawable.png0007, R.drawable.png0008,
R.drawable.png0009, R.drawable.png0010, };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcontact);
System.out.println("AddContact");
ibtnImgChoose = (ImageButton) findViewById(R.id.ibtnHead);
ibtnImgChoose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
initImageChoose();
imgChooseDialog.show();
}
});
}
private void initImageChoose() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.userheadchoose, null);
gallery = (Gallery) view.findViewById(R.id.img_gallery);
gallery.setAdapter(new imageAdapter(this));
gallery.setSelection(images.length / 2);
imageSwitcher = (ImageSwitcher) view.findViewById(R.id.img_switcher);
imageSwitcher.setFactory(new switcherFactory(this));
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
imageSwitcher.setImageResource(images[arg2]);
selectedImage = arg2;
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择图像");
builder.setPositiveButton("确认", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ibtnImgChoose.setImageResource(images[selectedImage]);
}
});
builder.setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setView(view);
imgChooseDialog = builder.create();
}
class imageAdapter extends BaseAdapter {
private Context context;
public imageAdapter(Context context) {
super();
this.context = context;
}
public int getCount() {
return images.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);// 构造一个ImageView
imageView.setImageResource(images[position]);// 设置ImageView图片源
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ScaleType.FIT_XY);// 自适应高和宽
imageView.setLayoutParams(new Gallery.LayoutParams(80, 80));// 设置显示图处的大小
imageView.setPadding(10, 5, 10, 5);// 设置四边的距离
return imageView;
}
}
class switcherFactory implements ViewFactory {
private Context context;
public switcherFactory(Context context) {
super();
this.context = context;
}
public View makeView() {
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(90, 90));
return imageView;
}
}
}
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
public class AddContactActivity extends Activity {
ImageButton ibtnImgChoose;
AlertDialog imgChooseDialog;
Gallery gallery;
ImageSwitcher imageSwitcher;
int selectedImage;
private int[] images = { R.drawable.png0001, R.drawable.png0002,
R.drawable.png0003, R.drawable.png0004, R.drawable.png0005,
R.drawable.png0006, R.drawable.png0007, R.drawable.png0008,
R.drawable.png0009, R.drawable.png0010, };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcontact);
System.out.println("AddContact");
ibtnImgChoose = (ImageButton) findViewById(R.id.ibtnHead);
ibtnImgChoose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
initImageChoose();
imgChooseDialog.show();
}
});
}
private void initImageChoose() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.userheadchoose, null);
gallery = (Gallery) view.findViewById(R.id.img_gallery);
gallery.setAdapter(new imageAdapter(this));
gallery.setSelection(images.length / 2);
imageSwitcher = (ImageSwitcher) view.findViewById(R.id.img_switcher);
imageSwitcher.setFactory(new switcherFactory(this));
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
imageSwitcher.setImageResource(images[arg2]);
selectedImage = arg2;
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择图像");
builder.setPositiveButton("确认", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ibtnImgChoose.setImageResource(images[selectedImage]);
}
});
builder.setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setView(view);
imgChooseDialog = builder.create();
}
class imageAdapter extends BaseAdapter {
private Context context;
public imageAdapter(Context context) {
super();
this.context = context;
}
public int getCount() {
return images.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);// 构造一个ImageView
imageView.setImageResource(images[position]);// 设置ImageView图片源
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ScaleType.FIT_XY);// 自适应高和宽
imageView.setLayoutParams(new Gallery.LayoutParams(80, 80));// 设置显示图处的大小
imageView.setPadding(10, 5, 10, 5);// 设置四边的距离
return imageView;
}
}
class switcherFactory implements ViewFactory {
private Context context;
public switcherFactory(Context context) {
super();
this.context = context;
}
public View makeView() {
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(90, 90));
return imageView;
}
}
}