Gallery组件主要用于横向显示图像列表,不过按常规做法。Gallery组件只能有限地显示指定的图像。也就是说,如果为Gallery组件指定了10张图像,那么当Gallery组件显示到第10张时,就不会再继续显示了。这虽然在大多数时候没有什么关系,但在某些情况下,我们希望图像显示到最后一张时再重第1张开始显示,也就是循环显示。要实现这种风格的Gallery组件,就需要对Gallery的Adapter对象进行一番改进。
以下通过Gallery模拟循环显示图像,在单击某一个Gallery组件中的图像时在下方显示一个放大的图像(使用ImageSwitcher组件)。
main.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">
<Gallery android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/gallery"
android:spacing="10dip"
android:unselectedAlpha="1.2"/>
<ImageSwitcher
android:id = "@+id/imageSwitcher"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_margin = "30dp"/>
</LinearLayout>
galleryActivity.java
package com.android.forlinx;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;
public class GalleryActivity extends Activity implements ViewFactory{
private ImageSwitcher imageSwitcher = null;
private Integer [] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);
// 设置ImageSwitcher组件的工厂对象
imageSwitcher.setFactory(this);
Gallery gallery = (Gallery)findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
//设置淡入和淡出的动画效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(GalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
//选中Gallery中某个图像时,在ImageSwitcher组件中放大显示该图像
//取余用于循环显示
imageSwitcher.setImageResource(mImageIds[position%mImageIds.length]);
}
});
}
public class ImageAdapter extends BaseAdapter{
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.GalleryActivity);
mGalleryItemBackground = attr.getResourceId(R.styleable.GalleryActivity_android_galleryItemBackground, 0);
attr.recycle();
}
//用于返回图像总数,要注意的是,这个总数不能大于图像的实际数(可以小于图像的实际数),否则会抛出越界异常
public int getCount() {
// TODO Auto-generated method stub
return mImageIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//ScaleType的用法
//CENTER/center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示
//CENTER_CROP/centerCrop 按比例扩大图片的size居中显示,使得图片长 (宽)等于或大于View的长(宽)
//CENTER_INSIDE/centerInside 将图片的内容完整居中显示,通过按比例缩小 或原来的size使得图片长/宽等于或小于View的长/宽
//FIT_CENTER/fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
//FIT_END/fitEnd 把 图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
//FIT_START/fitStart 把 图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
//FIT_XY/fitXY 把图片 不按比例 扩大/缩小到View的大小显示
//MATRIX/matrix 用矩阵来绘制
public View getView(int position, View convertView, ViewGroup partent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position%mImageIds.length]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
// ImageSwitcher组件需要这个方法来创建一个View对象(一般为ImageView对象)
// 来显示图像
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFFFF00);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return imageView;
}
}