您的位置:首页 > 移动开发 > Android开发

Gallery图片轮播

2014-10-20 14:37 302 查看
这是本人做的安卓Gallery图片轮番效果,相对比较简单,但是容易说明。我们看下效果图:



图片上面菜单是一个Gallery,下面图片显示是一个ImageSwitcher。

下面我们看下我们的主布局文件,比较简洁:(就定义了Gallery和ImageSwitcher)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <Gallery                                                //标题栏

        android:id="@+id/gallery"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

    

    <ImageSwitcher            //下面的大图片显示

        android:id="@+id/image"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        ></ImageSwitcher>

</LinearLayout>

下面时我们MainActivity的程序部分主要代码:

public class MainActivity extends Activity implements OnItemSelectedListener,ViewFactory{

    private int[] res;                     //用来装所要显示的图片

    private Gallery gallery;

    private ImageSwitcher imageSwitcher;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        gallery=(Gallery) findViewById(R.id.gallery);                          //初始化Gallery

        imageSwitcher=(ImageSwitcher) findViewById(R.id.image);              //初始化ImageSwitcher

        res=new int[]{R.drawable.four1,R.drawable.four2,R.drawable.four3,   //初始化图片资源,一共有七张图片

                R.drawable.four4,R.drawable.four5,R.drawable.four6,

                R.drawable.four7

        };

            Myadater adapter=new Myadater(res, this);                        //实例化自定义的适配器,将图片资源和上下文环境传进去

            gallery.setAdapter(adapter);                                                   //加载适配器

            gallery.setOnItemSelectedListener(this);                           //加载监听器,给imageSwitcher响应

            imageSwitcher.setFactory(this);                                         //ImageSwitcher的视图工厂,因为imageSwicther需要借助工厂中makeView方法的才能产生View

            imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//imageSwicther的动画效果,其比imageView强大的地方-淡入。

            imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

    }

    @Override

    public void onItemSelected(AdapterView<?> parent, View view, int position,

            long id) {

        // TODO Auto-generated method stub

        imageSwitcher.setBackgroundResource(res[position%res.length]);  //监听器响应,设置imageSwitcher的背景

    }

    @Override

    public View makeView() {                                    //该方法用于imageSwitcher的View制作,由imageSwitcher.setFactory(this)绑定 

        // TODO Auto-generated method stub

        ImageView imageView=new ImageView(this);

        imageView.setScaleType(ScaleType.FIT_CENTER);    //返回视图的样式,自适应居中

        return imageView;

    }

}

至此,我们的准备工作已经做完,其实无非就是在设置布局,配置Gallery监听器,和ImageSwitcher的图片样式。万事具备,只欠适配器,就能将图片放上安卓中。

我们自定义了一个适配器Myadater继承BaseAdapter。下面时Myadater的代码:

public class Myadater extends BaseAdapter{

    private int[]res;

    private Context context;

    public Myadater(int[] res, Context context) {         //接收传进来的图片资源和上下文对象

        super();

        this.res = res;

        this.context = context;

    }

    

    @Override

    public int getCount() {         //告诉MainActivity你所要显示内容的的个数,由于我们想要无线循环,即给了一个大数Integer.MAX_VALUE。这个必须得填,不然图片出不来

        // TODO Auto-generated method stub

        return Integer.MAX_VALUE;

    }

    @Override

    public Object getItem(int position) {        //获得item对象,一般都会写res[position],在本实例中写不写不重要

        // TODO Auto-generated method stub

        return res[position];

    }

    @Override

    public long getItemId(int position) {   //获得对象的位置id,同样在本实例不重要

        // TODO Auto-generated method stub

        return position;

    }

<!-- 这个非常重要,图片能不能出来就靠它-->

    @Override

    public View getView(int position, View convertView, ViewGroup parent) { 

     

        ImageView imageView=new ImageView(context);

        imageView.setBackgroundResource(res[position%res.length]);  //设置每个图片相对的背景

        imageView.setLayoutParams(new Gallery.LayoutParams(100,100));  //设置图片的大小

        imageView.setScaleType(ScaleType.FIT_XY);                                       //设置显示图片样式

        return imageView;

    }

}

总体思路:

利用适配器将上一栏的菜单栏显示出来,然后设置监听器设置imageSwitcher的背景。

这是本人的理解,还有什么完善的地方欢迎指出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息