Android入门之Gallery+ImageSwitcher用法实例解析

时间:2022-06-14 07:10:02

继上一篇介绍了如何使用gallery控件之后,本文就来讲一下gallery 与imageswitcher的结合使用。本文所述实例代码将实现一个简单的浏览图片的功能。

先贴出程序运行截图如下:

Android入门之Gallery+ImageSwitcher用法实例解析

除了gallery可以拖拉切换图片,我在imageswitcher控件加入了setontouchlistener事件实现,使得imageswitcher也可以在拖拉中切换图片。本例子依然使用java的反射机制来自动读取资源中的图片。

main.xml的源码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  
  <imageswitcher android:id="@+id/switcher"
    android:layout_width="match_parent" android:layout_height="match_parent"/>
  
  <gallery android:id="@+id/gallery"
    android:background="#55000000"
    android:layout_width="match_parent"
    android:layout_alignparentbottom="true"
    android:layout_alignparentleft="true"
    
    android:gravity="center_vertical"
    android:spacing="16dp" android:layout_height="100dp"/>
</relativelayout>

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.testimageview;
import java.lang.reflect.field;
import java.util.arraylist;
import android.app.activity;
import android.content.context;
import android.os.bundle;
import android.view.motionevent;
import android.view.view;
import android.view.view.ontouchlistener;
import android.view.viewgroup;
import android.view.animation.animationutils;
import android.widget.adapterview;
import android.widget.baseadapter;
import android.widget.gallery;
import android.widget.imageswitcher;
import android.widget.imageview;
import android.widget.adapterview.onitemselectedlistener;
import android.widget.gallery.layoutparams;
import android.widget.viewswitcher.viewfactory;
public class testimageview extends activity implements viewfactory {
 private imageswitcher is;
 private gallery gallery;
 private int downx,upx;
  private arraylist<integer> imglist=new arraylist<integer>();//图像id
  
 @override
 protected void oncreate(bundle savedinstancestate) {
 // todo auto-generated method stub
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.main);
    //用反射机制来获取资源中的图片id
   field[] fields = r.drawable.class.getdeclaredfields();
   for (field field : fields)
   {
   if (!"icon".equals(field.getname()))//除了icon之外的图片
   {
    int index = 0;
  try {
   index = field.getint(r.drawable.class);
  } catch (illegalargumentexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (illegalaccessexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
    //保存图片id
    imglist.add(index);
   }
   }
   
   //设置imageswitcher控件
 is = (imageswitcher) findviewbyid(r.id.switcher);
 is.setfactory(this);
 is.setinanimation(animationutils.loadanimation(this,
  android.r.anim.fade_in));
 is.setoutanimation(animationutils.loadanimation(this,
  android.r.anim.fade_out));
 is.setontouchlistener(new ontouchlistener(){
  /*
  * 在imageswitcher控件上滑动可以切换图片
  */
  @override
  public boolean ontouch(view v, motionevent event) {
  if(event.getaction()==motionevent.action_down)
  {
   downx=(int) event.getx();//取得按下时的坐标
   return true;
  }
  else if(event.getaction()==motionevent.action_up)
  {
   upx=(int) event.getx();//取得松开时的坐标
   int index=0;
   if(upx-downx>100)//从左拖到右,即看前一张
   {
   //如果是第一,则去到尾部
   if(gallery.getselecteditemposition()==0)
     index=gallery.getcount()-1;
   else
    index=gallery.getselecteditemposition()-1;
   }
   else if(downx-upx>100)//从右拖到左,即看后一张
   {
   //如果是最后,则去到第一
   if(gallery.getselecteditemposition()==(gallery.getcount()-1))
    index=0;
   else
    index=gallery.getselecteditemposition()+1;
   }
   //改变gallery图片所选,自动触发imageswitcher的setonitemselectedlistener
   gallery.setselection(index, true);
   return true;
  }
  return false;
  }
  
 });
 
 //设置gallery控件
 gallery = (gallery) findviewbyid(r.id.gallery);
 gallery.setadapter(new imageadapter(this));
 gallery.setonitemselectedlistener(new onitemselectedlistener(){
  @override
  public void onitemselected(adapterview<?> arg0, view arg1,
   int position, long arg3) {
  is.setimageresource(imglist.get(position));
  }
  @override
  public void onnothingselected(adapterview<?> arg0) {
  // todo auto-generated method stub
  }
  
 });
 }
 //设置imgaeswitcher
 @override
 public view makeview() {
 imageview i = new imageview(this);
 i.setbackgroundcolor(0xff000000);
 i.setscaletype(imageview.scaletype.center);//居中
 i.setlayoutparams(new imageswitcher.layoutparams(//自适应图片大小
  layoutparams.fill_parent, layoutparams.fill_parent));
 return i;
 }
 public class imageadapter extends baseadapter {
 public imageadapter(context c) {
  mcontext = c;
 }
 public int getcount() {
  return imglist.size();
 }
 public object getitem(int position) {
  return position;
 }
 public long getitemid(int position) {
  return position;
 }
 public view getview(int position, view convertview, viewgroup parent) {
  imageview i = new imageview(mcontext);
  i.setimageresource(imglist.get(position));
  i.setadjustviewbounds(true);
  i.setlayoutparams(new gallery.layoutparams(
   layoutparams.wrap_content, layoutparams.wrap_content));
  return i;
 }
 private context mcontext;
 }
}