现在的位置: 首页 > 综合 > 正文

Android开发——ImageSwitcher和Gallery组合实现图片切换案例

2018年02月16日 ⁄ 综合 ⁄ 共 5587字 ⁄ 字号 评论关闭

功能:点击滚动的图片,上面的imageswitcher显示选择的大图。

ImageSwitcher:显示图片区域的控件

Gallery:控制图标列表索引。

      (1) Gallery控件也可译为画廊组件,主要用于横向显示图像列表。Gallery类的继承结构Spinner有共同的父类:AbsSpinner。从某种意义上来说,Gallery和Spinner都是一个列表框,只不过Spinner是一个垂直显示的列表模型,而Gallery则是一个水平显示的列表框。

      (2) Gallery主要是通过用户拖动来展示一组图片或其它组件,Spinner则是提供选择让用户选择。为它提供一个内容SimpleAdapter,该Adapter的getView()方法返回的View将作为Gallery组件的列表项。

     属性:

       animationDuration:布局变化时动画转换所需时间(毫秒)。动画开始时计时。该值必须是整数,比如:100。

       spacing:设置设置图片之间的间距.

       unselectedAlpha:设置未选中的条目的透明度(Alpha)。该值必须是float类型,“1.2”

       Gravity:

                top紧靠容器顶端                bottom紧靠容器底部                left紧靠容器左侧,不改变其大小

                right紧靠容器右侧                center_vertical垂直居中                fill_vertical垂直方向上拉伸至充满容器

                center_horizontal水平居中     Fill_horizontal水平方向上拉伸使其充满容器

                center居中对齐                       fill在水平和垂直方向上拉伸,使其充满容器

                clip_vertical垂直剪切(当对象边缘超出容器的时候,将上下边缘超出的部分剪切掉)

                clip_horizontal水平剪切(当对象边缘超出容器的时候,将左右边缘超出的部分剪切掉)

xml布局代码:

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
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
         
    </ImageSwitcher
                            
    <Gallery
        android:id="@+id/gallery1"
        android:animationDuration="10"
        android:background="#55000000"
        android:gravity="fill_vertical"
        android:spacing="20dp"
        android:unselectedAlpha="1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        /> 
</RelativeLayout>

activity.java代码

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
publicclassSwitcherActivityextendsActivityimplementsViewFactory{ 
    privateImageSwitcher switcher; 
    privateGallery gallery; 
    privateint[] images =newint[]
{ R.drawable.a, R.drawable.b, 
            R.drawable.c, R.drawable.d, R.drawable.e ,R.drawable.f}; 
                       
    //图片被显示屏幕时自动回调来提供要显示的ImageView 
    publicViewmakeView() { 
            ImageView i =newImageView(SwitcherActivity.this); 
            i.setBackgroundColor(55000000); 
            i.setScaleType(ImageView.ScaleType.FIT_CENTER); 
            returni; 
    
    @Override
    publicvoidonCreate(Bundle savedInstanceState)
        super.onCreate(savedInstanceState); 
        // 设置窗口的特征:必须在所有操作之前,没有主题条TitleBar使图片显示区域更大 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.activity_switcher); 
                     
        // 查找组件ImageSwitcher 
        switcher = (ImageSwitcher)this.findViewById(R.id.imageSwitcher1); 
        switcher.setFactory(this); 
        //设置默认显示的图片 
        switcher.setImageResource(images[0]); 
        //设置图片的滑动效果 
        switcher.setInAnimation(AnimationUtils.loadAnimation(this
                android.R.anim.fade_in)); 
        switcher.setOutAnimation(AnimationUtils.loadAnimation(this
                android.R.anim.fade_out)); 
                             
        //查找画廊gallery组件 
        gallery = (Gallery)this.findViewById(R.id.gallery1); 
        gallery.setSelection(images.length/2); 
        gallery.setAdapter(newImageAdapter()); 
        //处理gallery图片点击事件 
        gallery.setOnItemClickListener(newAdapterView.OnItemClickListener()
                     
            publicvoidonItemClick(AdapterView<?>
arg0, View arg1,
intarg2, 
                    longarg3) { 
                switcher.setImageResource(images[arg2]); 
            
        }); 
        /*处理gallery图片条目选择事件 
         * gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
                     
            public void onItemSelected(AdapterView<?> arg0, View arg1, 
                    int arg2, long arg3) { 
                switcher.setImageResource(images[arg2]); 
            
                     
            public void onNothingSelected(AdapterView<?> arg0) { 
            
        });*/
    
                             
                         
    publicclassImageAdapterextendsBaseAdapter
                     
        publicintgetCount() { 
            returnimages.length; 
                     
        
        publicObject getItem(intposition)
            returnposition; 
        
        publiclonggetItemId(intposition)
            returnposition; 
        
        //返回要显示的ImageView 
        publicViewgetView(intposition,
View convertView, ViewGroup parent) { 
            ImageView views =newImageView(SwitcherActivity.this); 
            // 设置图片资源 
            views.setImageResource(images[position]); 
            //保持宽高比,不设置则gallery显示一张图片 
            views.setAdjustViewBounds(true); 
            //设置固定大小 
            views.setMaxHeight(80); 
            views.setMaxWidth(120); 
            views.setScaleType(ImageView.ScaleType.FIT_CENTER); 
            // 设置图片大小 
            views.setLayoutParams(newGallery.LayoutParams( 
                    LayoutParams.WRAP_CONTENT,       LayoutParams.WRAP_CONTENT)); 
            returnviews; 
        
    
                     
    @Override
    publicbooleanonCreateOptionsMenu(Menu
menu) { 
        getMenuInflater().inflate(R.menu.activity_switcher, menu); 
        returntrue
    
                     
}

效果图:

注意的问题:图片选择时要注意不能太大,最好是选择一样大小的一组图片,否则可能

会发生内存溢出异常,所以定要选好图片素材。

抱歉!评论已关闭.