Android之ImageSwitcher,Gallery用法
今天在做一个软件界面时用到了ImageSwitcher和Gallery控件,在看API时,感觉上面的例子讲的不是很具体,效率并不高。在这里我就以一个图片浏览功能来具体说明这两个控件的用法。
首先看运行效果:
在这里图片我用的是API中的图片。先说下这个图片浏览的功能吧,首先,它要实现图片的切换,当点击上面的小图时,下方会出现对象的大图,其次就是实现上图中最上面的样式,即一个图片和一个文本。下来我们还要实现起始位置居中,滑动小图的速率的控制,最上面小图的无限循环等功能。下面我就将具体实现代码附下,供大家参考。
main.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical">
6 <cn.yj3g.gallery.MyGallery android:id="@+id/gallery"
7 android:background="#55000000"
8 android:layout_width="match_parent"
9 android:layout_height="80dp"
10 android:gravity="center_vertical"
11 android:spacing="2dp"
12 />
13 <ImageSwitcher android:id="@+id/switcher"
14 android:layout_width="match_parent"
15 android:layout_height="match_parent"
16 android:layout_weight="1"
17 />
18 </LinearLayout>
在上面我是自定义视图,引用自己定义的一个Gallery,在这个Gallery中我重新设置的滑动的速率,让它滑动速度放慢,下面是我自定义的Gallery 代码:
MyGallery.java:
1 package cn.yj3g.gallery;
2
3 import android.content.Context;
4 import android.util.AttributeSet;
5 import android.view.MotionEvent;
6 import android.widget.Gallery;
7
8 public class MyGallery extends Gallery {
9 public MyGallery(Context context, AttributeSet attrs) {
10 super(context, attrs);
11 }
12 /**
13 * 重写onFling方法,将滑动的速率降低
14 */
15 @Override
16 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
17 return super.onFling(e1, e2, velocityX/3, velocityY);
18 }
19 }
下面是在定义gallery布局文件的代码:
gallery_item.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical" android:layout_width="match_parent"
4 android:layout_height="match_parent">
5 <ImageView android:id="@+id/item_gallery_image"
6 android:layout_width="fill_parent"
7 android:layout_height="fill_parent"
8 android:layout_weight="1"
9 android:scaleType="fitXY"/>
10 <TextView android:id="@+id/item_gallery_text"
11 android:layout_width="fill_parent"
12 android:layout_height="wrap_content"
13 android:gravity="center"/>
14
15 </LinearLayout>
下面就是核心实现代码:
PictrueChangeActivity:
1 package cn.yj3g.PictrueChange;
2
3 import java.util.HashMap;
4
5 import android.app.Activity;
6 import android.content.Context;
7 import android.content.res.TypedArray;
8 import android.os.Bundle;
9 import android.util.Log;
10 import android.view.LayoutInflater;
11 import android.view.View;
12 import android.view.ViewGroup;
13 import android.view.Window;
14 import android.view.animation.AnimationUtils;
15 import android.widget.AdapterView;
16 import android.widget.BaseAdapter;
17 import android.widget.Gallery;
18 import android.widget.Gallery.LayoutParams;
19 import android.widget.ImageSwitcher;
20 import android.widget.ImageView;
21 import android.widget.TextView;
22 import android.widget.ViewSwitcher;
23
24 public class PictrueChangeActivity extends Activity implements AdapterView.OnItemClickListener,
25 ViewSwitcher.ViewFactory {
26 //定义ImageSwitcher类对象
27 private ImageSwitcher mSwitcher;
28 //文本资源
29 private String[] titles = {"标题1","标题2","标题3","标题4","标题5","标题6","标题7","标题8",};
30 //大图资源
31 private Integer[] mThumbIds = { R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
32 R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
33 R.drawable.sample_7 };
34 //大图对应的小图资源
35 private Integer[] mImageIds = { R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
36 R.drawable.sample_thumb_2, R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,
37 R.drawable.sample_thumb_5, R.drawable.sample_thumb_6, R.drawable.sample_thumb_7 };
38 @Override
39 public void onCreate(Bundle savedInstanceState) {
40 super.onCreate(savedInstanceState);
41 //设置窗体无标题
42 requestWindowFeature(Window.FEATURE_NO_TITLE);
43 setContentView(R.layout.main);<
补充:移动开发 , Android ,