Gallery实现幻灯片式的图片浏览器

来源:互联网 发布:帝国cms多图上传插件 编辑:程序博客网 时间:2024/04/30 11:41

布局文件如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageSwitcher        android:layout_width="320dp"        android:layout_height="320dp"        android:id="@+id/imageSwitcher"        android:layout_marginTop="20dp"        android:layout_gravity="center_horizontal"/>    <Gallery        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/gallery"        android:layout_marginTop="25dp"        android:unselectedAlpha="0.6"        android:spacing="3pt"        android:layout_gravity="center_horizontal" /></LinearLayout>

java代码如下:

package com.example.administrator.gallerytest;import android.content.res.TypedArray;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher;public class GalleryTest extends AppCompatActivity {    int[] imageIds = new int[]{            R.drawable.cat,//a picture about cat            R.drawable.cherrytree,//a picture about cherrytree            R.drawable.dog,//a picture about dog            R.drawable.house,//a picture about house            R.drawable.monkey,//a picture about monkey            R.drawable.mountriver,//a picture about mount and river            R.drawable.rabbit,//a picture about rabbit            R.drawable.yellowflowers,//a picture about yellow flowers            R.drawable.tree,//a picture about tree            R.drawable.dragon,//a picture about dragon            R.drawable.wolf,//a picture about wolf            R.drawable.eagle,    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        final Gallery gallery = (Gallery)findViewById(R.id.gallery);        //获取显示图片的ImageSwitcher对象        final ImageSwitcher switcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);        //为ImageSwitcher对象设置ViewFactory对象        switcher.setFactory(new ViewSwitcher.ViewFactory() {            @Override            public View makeView() {                ImageView imageView = new ImageView(GalleryTest.this);                //图像视图,用于支持 HTML <IMG> 标记。                // 支持通过标记的 HEIGHT 和 WIDTH 属性进行缩放。如果不能加载图像,                // 则将呈现任何通过 ALT 属性指定的文本。                imageView.setBackgroundColor(0xff0000);                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(                        ViewGroup.LayoutParams.WRAP_CONTENT,                        ViewGroup.LayoutParams.WRAP_CONTENT));                return imageView;            }        });        //设置图片更换的动画效果        switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));        switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));        //创建一个BaseAdapter对象,该对象负责提供Gallery所显示的每张图片        BaseAdapter adapter = new BaseAdapter() {            //data source(数据源)<------>adapter(适配器)<------>listview(视图控件)            @Override            public int getCount() {                return imageIds.length;            }            @Override            public Object getItem(int i) {                return i;            }            @Override            public long getItemId(int i) {                return i;            }            //该方法返回的View就代表了每个列表项            @Override            public View getView(int i, View view, ViewGroup viewGroup) {                //创建一个imageView                ImageView imageView = new ImageView(GalleryTest.this);                imageView.setImageResource(imageIds[i % imageIds.length]);                //设置ImageView的缩放类型                imageView.setScaleType(ImageView.ScaleType.FIT_XY);                //把图片按照指定的大小在View中显示,拉伸显示图片,不保持原比例,                // 全部显示图片填满View.关键字:不保持比例,拉伸显示全图,填满ImageView                imageView.setLayoutParams(new Gallery.LayoutParams(75, 100));                TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);//??????                imageView.setBackgroundResource(typedArray.getResourceId(                        R.styleable.Gallery_android_galleryItemBackground, 0));                return imageView;            }        };        gallery.setAdapter(adapter);        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {            @Override            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {                switcher.setImageResource(imageIds[i % imageIds.length]);            }            @Override            public void onNothingSelected(AdapterView<?> adapterView) {            }        });    }}

图片都是自己找的,放到drawable文件夹下面。


AVD运行结果:




0 0