Android基础之ImageSwitcher与Gallery

1、配置布局文件 main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />
    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:spacing="8dp"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>

2、程序主要代码 MainActivity.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
95
96
97
package com.yin.picture_view;
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
 
public class MainActivity extends Activity {
 
    // 显示图片的资源
    private static int[] images = { R.drawable.s0, R.drawable.s1,
            R.drawable.s2, R.drawable.s3, R.drawable.s4, R.drawable.s5,
            R.drawable.s6, R.drawable.s7, R.drawable.s8, R.drawable.s9 };
    Gallery gallery;
    ImageSwitcher is;
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gallery = (Gallery) findViewById(R.id.gallery);
        is = (ImageSwitcher) findViewById(R.id.imageSwitcher);
 
        gallery.setAdapter(new ImageAdapter(this));
        // 让选定的图片在中心显示
        gallery.setSelection(images.length / 2);
 
        // 为Gallery绑定监听器;
        gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // 当在Gallery中选定一张图片是 ImageSwitcher同步显示同一张
                // position%images.length 为了让图片循环显示
                is.setImageResource(images[position % images.length]);
 
            }
 
            public void onNothingSelected(AdapterView<?> parent) {
 
            }
        });
 
        is.setFactory(new ImageFactory(this));
 
    }
 
    private class ImageAdapter extends BaseAdapter {
        private Context context;
 
        public ImageAdapter(Context context) {
            this.context = context;
        }
 
        // 可以return images.lenght(),在这里返回Integer.MAX_VALUE
        // 是为了使图片循环显示
        public int getCount() {
            return Integer.MAX_VALUE;
        }
 
        public Object getItem(int position) {
            return null;
        }
 
        public long getItemId(int position) {
            return 0;
        }
 
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView iv = new ImageView(context);
            iv.setImageResource(images[position % images.length]);
            iv.setLayoutParams(new Gallery.LayoutParams(90, 90));
            iv.setAdjustViewBounds(true);
            return iv;
        }
    }
 
    private class ImageFactory implements ViewFactory {
        private Context context;
 
        public ImageFactory(Context context) {
            this.context = context;
        }
 
        public View makeView() {
            ImageView iv = new ImageView(context);
            iv.setLayoutParams(new ImageSwitcher.LayoutParams(200, 200));
            return iv;
        }
    }
}
posted on 2012-05-04 23:34  emlinux  阅读(179)  评论(0)    收藏  举报