Gallery实现图片拖动切换

Gallery中文意思为画廊,通过Gallery能够实现用手指在屏幕上滑动实现图片的拖动。效果如下:

Gallery实现图片拖动切换

上面,为了学习了解,只用了android默认的Icon图片。

主程序中创建了一个继承自BaseAdapter的ImageAdapter方法,这个ImageAdapter的存在目的,是为了要暂存想要显示的图片,并作为Gallery控件图片的源引用。

MyActivity.java

 1 package com.example.test3_15;

 2 

 3 import android.app.Activity;

 4 import android.content.Context;

 5 import android.os.Bundle;

 6 import android.view.View;

 7 import android.view.ViewGroup;

 8 import android.widget.BaseAdapter;

 9 import android.widget.Gallery;

10 import android.widget.ImageView;

11 

12 public class MyActivity extends Activity {

13     /**

14      * Called when the activity is first created.

15      */

16     @Override

17     public void onCreate(Bundle savedInstanceState) {

18         super.onCreate(savedInstanceState);

19         setContentView(R.layout.main);

20         /* 获得Gallery对象,并添加ImageAdapter给Gallery对象*/

21         ((Gallery)findViewById(R.id.gallery)).setAdapter(new ImageAdapter(this));

22     }

23     public class ImageAdapter extends BaseAdapter

24     {

25         /* 定义Context */

26         private Context myContext;

27         /*使用android.R.drawable 里的图片作为图库源,类型为整数数组*/

28         private int[] myImageIds=

29                 {

30                         android.R.drawable.btn_minus,

31                         android.R.drawable.btn_radio,

32                         android.R.drawable.ic_lock_idle_low_battery,

33                         android.R.drawable.ic_menu_camera,

34                 };

35         /*构造器只有一个参数,即要存储的Context*/

36         public ImageAdapter(Context c){this.myContext=c;}

37         /*返回所有已定义的图片总数量*/

38         public int getCount(){return this.myImageIds.length;}

39         /*获取图片在库中的位置*/

40         public Object getItem(int posotion){return posotion;}

41         /*获取图片ID*/

42         public long getItemId(int posotion){return posotion;}

43         /*取得当前所要显示的图像View,传入数组ID值使之读取与成像*/

44         public View getView(int position,View convertView,ViewGroup parent)

45         {

46             /*创建一个ImageView对象*/

47             ImageView i=new ImageView(this.myContext);

48             /*给ImageView设置资源*/

49             i.setImageResource(this.myImageIds[position]);

50             /*设置显示比例类型 */

51             i.setScaleType(ImageView.ScaleType.CENTER);

52             /*设置这个ImageView对象的宽高,单位为dip*/

53             i.setLayoutParams(new Gallery.LayoutParams(120,120));

54             return i;

55 

56         }

57 

58     }

59 }

main.xml

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 3               android:orientation="vertical"

 4               android:layout_width="fill_parent"

 5               android:layout_height="fill_parent"

 6         >

 7     <TextView

 8             android:id="@+id/myTextView01"

 9             android:layout_width="fill_parent"

10             android:layout_height="wrap_content"

11             android:layout_marginTop="15dp"

12             android:gravity="center"

13             android:text="@string/textView"

14             android:textColor="@drawable/blue"

15 

16             />

17     <Gallery

18             android:id="@+id/gallery"

19             android:layout_width="wrap_content"

20             android:layout_height="wrap_content"

21             android:layout_gravity="center"

22             />

23 </LinearLayout>

  其实,对了context的作用还是不怎么懂!!有帮忙解释解释下不?

你可能感兴趣的:(gallery)