GalleryDemo幻灯片
vGallery:和Spinner都继承自AbsSpinner,这说明Gallery和Spinner都是一个列表框。它们之间的区别在于Spinner显示的是一个垂直的列表选择框,而Gallery显示的是一个水平的列表框。
vSpinner的作用是供用户选择,而Gallery则允许用户通过拖动来查看上一个、下一个列表项。
v用法相似,只要提供一个内容Adapter即可。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <Gallery
- android:id="@+id/gallery_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:unselectedAlpha="0.5"
- android:spacing="3pt"/>
- <ImageView
- android:id="@+id/gallery_p_w_picpath"
- android:layout_width="280dp"
- android:layout_height="280dp"
- android:layout_gravity="center_horizontal"/>
- </LinearLayout>
需要定义一个类,继承BaseAdapter,应为它继承了SpinnerAdapter
- 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.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.Gallery;
- import android.widget.ImageView;
- import android.widget.AdapterView.OnItemSelectedListener;
- public class GalleryDemo extends Activity{
- p_w_picpathadapter ida;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.gallery);
- Gallery gallery = (Gallery) findViewById(R.id.gallery_view);
- ida=new p_w_picpathadapter(this);
- gallery.setAdapter(ida);
- gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view,
- int position, long id) {
- ImageView p_w_picpathView=(ImageView) findViewById(R.id.gallery_p_w_picpath);
- p_w_picpathView.setImageResource(ida.mImageIds[position]);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- }
- }
- //********************************************************************************//
- //重写BaseAdapter方法,它继承了SpinnerAdapter
- class p_w_picpathadapter extends BaseAdapter{
- //定义数组,加入图片id
- public int[] mImageIds=new int[]{
- R.drawable.jinniu,R.drawable.chunv,R.drawable.juxie,R.drawable.mojie,
- R.drawable.sheshou,R.drawable.shizi,R.drawable.shuangyu,R.drawable.shuangzi,
- R.drawable.shuiping,R.drawable.tiancheng,R.drawable.tianxie,R.drawable.baiyang
- };
- int mGalleryItemBackground;
- private Context mContext;
- //有参构造,参数是context:指向对象
- public p_w_picpathadapter(Context c){
- mContext =c;
- TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
- mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
- attr.recycle();
- }
- @Override
- public int getCount() {
- return mImageIds.length;
- }
- @Override
- public Object getItem(int position) {
- return position;
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- // 该方法的返回的View就是代表了每个列表项
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView p_w_picpathView = new ImageView(mContext); //创建一个p_w_picpathview
- p_w_picpathView.setImageResource(mImageIds[position]); //给p_w_picpathview添加图片
- p_w_picpathView.setLayoutParams(new Gallery.LayoutParams(150, 100)); //设置布局参数
- p_w_picpathView.setScaleType(ImageView.ScaleType.FIT_XY); //设置类型适合xy
- p_w_picpathView.setBackgroundResource(mGalleryItemBackground); //设置背景
- return p_w_picpathView;
- }
- }
转载于:https://blog.51cto.com/siyanpeng/751710