Gallery与Spinner有共同父类:AbsPinner。说明Gallery与Spinner都是一个列表框。
它们之间的差别在于Spinner显示的是一个垂直的列表选择框,而Gallery显示的是一个水平的列表选择框。Spinner的作用是供用户选择,而Gallery则同意用户通过拖动查看上一个,下一个。
Gallery使用方法与Spinner的使用方法形似。仅仅要为它提供一个内容Adapter就能够了。
Adapter的getView方法返回View作为Gallery列表的列表项。假设程序须要监控Gallery选择项的改变,能够加入OnItemSelectedListener监听就可以。
Gallery 的xml属性
以下通过一个幻灯片样例来熟悉Gallery
(1)activity_main.xml 布局一个ImageSwitcher 和Gallery ImageSwitcher用于显示Gallery选中的图片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageSwitcher
android:id="@+id/imgSwt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:unselectedAlpha="0.7"
android:spacing="2pt"/>
</LinearLayout>
(2)MainActivity.java
package com.example.gallery; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
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.RadioGroup.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { //定义组件
private ImageSwitcher imgSwt = null;
private Gallery gallery = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //获取布局组件
imgSwt = (ImageSwitcher) findViewById(R.id.imgSwt);
gallery = (Gallery) findViewById(R.id.gallery); //图片
final int images[] = new int[]{
R.drawable.name01,R.drawable.name02,R.drawable.name03,
R.drawable.name04,R.drawable.name05,R.drawable.name06,
R.drawable.name07,R.drawable.name08,R.drawable.name09,
R.drawable.name10,R.drawable.name11,R.drawable.name12
};
//设置图片切换效果
imgSwt.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
imgSwt.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
//设置ViewFactory对象
imgSwt.setFactory(new ViewFactory() { @Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(350,350));
return imageView;
}
}); //创建BaseAdapter对象,负责提供Gallery显示全部图像
BaseAdapter adapter = new BaseAdapter() { @Override
public View getView(int position, View convertView, ViewGroup parent) {
//创建imageview
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(images[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
return imageView;
} @Override
public long getItemId(int position) {
return position;
} @Override
public Object getItem(int position) {
return position;
} @Override
public int getCount() {
return images.length;
}
}; //给Gallery设置适配器
gallery.setAdapter(adapter);
//加入事件
gallery.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
imgSwt.setImageResource(images[position]);
} @Override
public void onNothingSelected(AdapterView<? > parent) {
// TODO Auto-generated method stub }
}); } }
执行效果例如以下: