Android界面开发之切换图片ImageSwitcher用法

我们在Windows平台上要查看多张图片,最简单的方法就是通过“Windows图片和传真查看器”在“下一张”和“上一张”之间切 换,Android平台上可以通过ImageSwitcher类来实现这一效果。ImageSwitcher类必须设置一个ViewFactory,主要 用来将显示的图片和父窗口区分开来,因此需要实现ViewSwitcher.ViewFactory接口,通过makeView()方法来显示图片,这里 会返回一个ImageView对象,而方法setImageResource用来显示指定的图片资源。我们先来看一个示例,其运行效果如图4-45所示。 当点击“上一张”或“下一张”按钮时,切换浏览另一张图片,如图4-46所示。

实现代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.yarin.android.Examples_04_18;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher.ViewFactory;
 
public class Activity01 extends Activity implements OnClickListener,ViewFactory
{
    /* 所有要显示的图片资源索引 */
    private static final Integer[] imagelist =
    {
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4,
        R.drawable.img5,
        R.drawable.img6,
        R.drawable.img7,
        R.drawable.img8,
    };
     
    //创建ImageSwitcher对象
    private ImageSwitcher           m_Switcher;
    //索引
    private static int              index           = 0;
 
    //“下一页”按钮ID
    private static final int        BUTTON_DWON_ID  = 0x123456;
    //“上一页”按钮ID
    private static final int        BUTTON_UP_ID    = 0x123457;
    //ImageSwitcher对象的ID
    private static final int        SWITCHER_ID     = 0x123458;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
 
        //创建一个线性布局LinearLayout
        LinearLayout main_view = new LinearLayout(this);
        //创建ImageSwitcher对象
        m_Switcher = new ImageSwitcher(this);
        //在线性布局中添加ImageSwitcher视图
        main_view.addView(m_Switcher);
        //设置ImageSwitcher对象的ID
        m_Switcher.setId(SWITCHER_ID);
        //设置ImageSwitcher对象的数据源
        m_Switcher.setFactory(this);
        m_Switcher.setImageResource(imagelist[index]);
         
        //设置显示上面创建的线性布局
        setContentView(main_view);
 
        //创建“下一张”按钮
        Button next = new Button(this);
        next.setId(BUTTON_DWON_ID);
        next.setText("下一张");
        next.setOnClickListener(this);
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100, 100);
        main_view.addView(next, param);
 
        //创建“上一张”按钮
        Button pre = new Button(this);
        pre.setId(BUTTON_UP_ID);
        pre.setText("上一张");
        pre.setOnClickListener(this);
        main_view.addView(pre, param);
 
    }
 
    //事件监听、处理
    public void onClick(View v)
    {
        switch (v.getId())
        {
            //下一页
            case BUTTON_DWON_ID:
                index++;
                if (index >= imagelist.length)
                {
                    index = 0;
                }
                //ImageSwitcher对象资源索引
                m_Switcher.setImageResource(imagelist[index]);
                break;
            //上一页
            case BUTTON_UP_ID:
                index--;
                if (index < 0)
                {
                    index = imagelist.length - 1;
                }
                //ImageSwitcher对象资源索引
                m_Switcher.setImageResource(imagelist[index]);
                break;
            default:
                break;
        }
    }
 
    public View makeView()
    {
        //将所有图片通过ImageView来显示
        return new ImageView(this);
    }
}

非常简单,如上所述,我们就完成了Android ImageSwitcher,可以在Android设备上浏览图片了。

posted on 2013-03-09 11:16  AI_JJ  阅读(483)  评论(0)    收藏  举报

导航