Gallery能够水平显示其内容,一般用来浏览图片,被选中的选项位于中间,并且可以相应事件显示信息。下面结合ImageSwitcher组件来实现一个通过缩略图来浏览图片的程序,具体步骤如下
java 代码
package com.test;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class mainactivity extends Activity implements
OnItemSelectedListener, ViewFactory {
private ImageSwitcher is;
private Gallery gallery;
private Integer[] mThumbIds = { R.drawable.b, R.drawable.c,
R.drawable.d, R.drawable.f,
R.drawable.g,
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置没有标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置布局
setContentView(R.layout.main);
is = (ImageSwitcher) findViewById(R.id.switcher);
//为ImageSwitcher设置工厂
is.setFactory(this);
//设置动画渐入效果
is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
//设置动画渐出效果
is.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(this);
}
//这个方法主要是返回一个view对象
public View makeView() {
ImageView i = new ImageView(this);
//设置背景颜色
i.setBackgroundColor(0xFF000000);
//设置精度类型
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return i;
}
class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
//获得数量
public int getCount() {
return mThumbIds.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 i = new ImageView(mContext);
//设置缩略图片资源
i.setImageResource(mThumbIds[position]);
//设置边界对齐
i.setAdjustViewBounds(true);
//设置布局参数
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//设置背景资源
i.setBackgroundResource(R.drawable.e);
return i;
}
private Context mContext;
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
is.setImageResource(mThumbIds[position]);//mImageIds
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
main.xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageSwitcher android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<Gallery android:id="@+id/gallery"
android:background="#242424"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="16dp"
/>
</RelativeLayout>