【Android 开发】:UI控件之 ImageSwitcher 图片切换控件的使用

1. ImageSwitcher 概要

1). ImageSwitcher 控件可以用在不同的图像之间切换,其中切换的过程可以采用动画的方法,如淡入淡出的效果。

2). ImageSwitcher 需要一个图像工厂(ViewFactory)来创建用于显示图像的ImageView对象,因此我们需要一个实现  

    android.widget.ViewSwitcher.ViewFactory接口的类。

2. 程序案例的实现

[需求]:我们需要根据 ImageSwitcher 的特性完成一个图片切换的Demo,在学习之前我们需要掌握一些知识点

1) 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageSwitcher
        android:id="@+id/imageswitcher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="100dp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
			android:layout_marginLeft="50dp"
			android:layout_weight="1"
            android:text="next" />

        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="50dp"
            android:layout_weight="1"
            android:text="forward" />
    </LinearLayout>

</LinearLayout>

2) 主程序代码

public class ImageSwitcherDemo extends Activity implements OnClickListener, ViewFactory{
    
    private ImageSwitcher imageSwitcher;
    private Button button1, button2;
    private int index = 0; //用于浏览图片的次序
    
    /*
     * 用户存放用户的图片信息
     * Drawable对象主要是用来干什么用的呢?
     */
    private List<Drawable> list = new ArrayList<Drawable>();
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initComponent();       
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);    
        imageSwitcher.setFactory(this);
        
        //往list集合里装载图片信息
        list.add(getResources().getDrawable(R.drawable.item1));
        list.add(getResources().getDrawable(R.drawable.item2));
        list.add(getResources().getDrawable(R.drawable.item3));
        list.add(getResources().getDrawable(R.drawable.item4));
        list.add(getResources().getDrawable(R.drawable.item5));
        list.add(getResources().getDrawable(R.drawable.item6));
        list.add(getResources().getDrawable(R.drawable.item7));
        list.add(getResources().getDrawable(R.drawable.item8));
        
        /*
         * 初始化加载图片的信息
         * 查看api文档 ImageSwitcher
         * public void setImageDrawable (Drawable drawable)方法知它接受的是一个drawable对象
         */
        if(list.size() > 0){
            imageSwitcher.setImageDrawable(list.get(0));
        }
        
    }
    
    private void initComponent(){
        imageSwitcher = (ImageSwitcher)findViewById(R.id.imageswitcher);
        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);

 
    }

    @Override
    public View makeView() {
        // TODO Auto-generated method stub
        return new ImageView(ImageSwitcherDemo.this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                //表示按下一张图片,如果图片走到末尾,则重新浏览,这样就可以循环浏览了
                index--;
                if(index < 0){
                    index = list.size() - 1;
                }
                imageSwitcher.setImageDrawable(list.get(index));
                break;

            case R.id.button2:
                //表示按上一张图片,如果图片走到首张,则重新浏览,这样就可以循环浏览了
                index++;
                if(index >= list.size()){
                    index = 0;
                }
                imageSwitcher.setImageDrawable(list.get(index));
                break;
        }
        
    }
}

3. 程序案例的执行

这样的效果主要是用在家具或者服装中使用向下或者向上来浏览图片的功能

4. 程序案例的说明

1) Drawable 对象

Drawable 对象主要是用来干什么用的呢?查看一下 Android api文档: android.graphics.drawable.Drawable
它主要是用来描述当前资源文件Drawable文件夹下面的图片的一个信息,它可以构建图形的字节数组对象,也可以是单纯的一张图片作为一个对象。

2) ImageSwitcher

查看api文档 ImageSwitcher,主要涉及到两个方法:
public void setFactory (ViewSwitcher.ViewFactory factory) [如果在ImageSwitcher 中找不到这个方法,就往父类去找] 
public void setImageDrawable (Drawable drawable) 设置一个Drawable对象的Image

3) makeView()方法
ImageSwitcher 中,由于他实现了 ViewFactory,所以需要重写的这个 public View makeView() 方法,这个方法是比较重要的查看 ViewFactory api文档说明它可以在ViewSwitcher中创建一个视图,它里面有一个抽象方法如下图所示:

 
创建一个新的视图然后添加到ViewSwicher中。 【意思就是当我们在点击向前向后退的时候,它需要图片的资源加载到 ImageView 中,这样它就可以去显示了】 所以返回的是一个 ImageView的对象了 new ImageView。

[注意]:android-support-v4.jar 是Anroid官方版本升级2.3.3之后给我们打的一个补丁包



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值