Gallery组件用于拖拽浏览图片,以下我们就来看一下怎样实现。
一、实现Gallery
1.布局文件非常easy:
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/MyLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <Gallery android:id="@+id/myGallery" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
2.自己定义适配器类,能够直接覆写BaseAdapter类中的几个方法。package org.yayun.demo;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.Gallery.LayoutParams;public class ImageGalleryAdapter extends BaseAdapter { private Context context; private int imgRes[] = new int[] { R.drawable.ispic_a, R.drawable.ispic_b, R.drawable.ispic_c, R.drawable.ispic_d, R.drawable.ispic_e, }; public ImageGalleryAdapter(Context c) {//构造方法,用于获取上下文对象 this.context = c; } public int getCount() { return imgRes.length; } public Object getItem(int position) { return imgRes[position]; } public long getItemId(int position) { return imgRes[position]; } public View getView(int position, View convertView, ViewGroup parent) { ImageView img = new ImageView(this.context); img.setBackgroundColor(0xFFFFFFFF); img.setImageResource(this.imgRes[position]);//设置资源 img.setScaleType(ImageView.ScaleType.CENTER);//居中显示 img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return img; }}3.MainActivity.java:
package org.yayun.demo;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;import android.widget.Gallery;public class MainActivity extends Activity { private Gallery gallery; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 生命周期方法 super.setContentView(R.layout.main); // 设置要使用的布局管理器 gallery = (Gallery) findViewById(R.id.myGallery); gallery.setAdapter(new ImageGalleryAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "您选择了第" + String.valueOf(position + 1) + "张图片", Toast.LENGTH_SHORT).show(); } }); }}4.执行实比例如以下:
二、Gallery和ImageSwitcher结合
这时的Gallery我们用SimpleAdapter类完毕。
1.布局文件:
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/MyLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:gravity="bottom" android:orientation="vertical" > <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ImageSwitcher> <Gallery android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:spacing="5dp" /> </LinearLayout>
2.定义显示模板:3.MainActivity.java程序:
package org.yayun.demo;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.SimpleAdapter;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;import android.widget.Gallery.LayoutParams;import android.widget.Gallery;import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity { private Gallery gallery; private List4.执行实例: