用户界面 View(八)

Gallery 画廊
Gallery是一个内部元素可以水平滚动,并且可以把当前选择的子元素定位在它中心的布局组件。
我们还是直接看看例子的运行效果。
1.png
下面上代码,相关解释都放在代码里了。
1、建立一个新项目 HelloGallery
2、拷贝wallpaper_0.jpg…wallpaper_9.jpg 10个图片文件到res/drawable目录
3、res/layout/main.xml文件的内容如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <framelayout android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/FrameLayout01">
  3. <imageview android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/ImageView01" android:src="@drawable/wallpaper_0">
  4. </imageview>
  5. <gallery android:layout_height="wrap_content" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Gallery01" android:spacing="5dp">
  6. </gallery>
  7. </framelayout>
复制代码
其中我们使用FrameLayout来实现叠加效果,使用ImageView来显示大图,Gallery来展示画廊,android:spacing="5dp" 属性则是用来设置元素之间的间隔。4、在res/values/目录中新建一个attrs.xml内容如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <resources>
  3.         <declare -styleable="" name="HelloGallery">
  4.                 <attr name="android:galleryItemBackground">
  5.         </attr></declare>
  6. </resources>
复制代码
5、在MainHelloGallery.java中的内容如下:
  1. package android.basic.lesson13;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.AdapterView;
  9. import android.widget.AdapterView.OnItemClickListener;
  10. import android.widget.BaseAdapter;
  11. import android.widget.Gallery;
  12. import android.widget.ImageView;
  13. import android.widget.Toast;
  14. public class MainHelloGallery extends Activity {
  15.         /** Called when the activity is first created. */
  16.         @Override
  17.         public void onCreate(Bundle savedInstanceState) {
  18.                 super.onCreate(savedInstanceState);
  19.                 setContentView(R.layout.main);
  20.                 //定义UI组件
  21.                 final ImageView iv= (ImageView)findViewById(R.id.ImageView01);
  22.                 Gallery g = (Gallery) findViewById(R.id.Gallery01);
  23.                 //设置图片匹配器
  24.                 g.setAdapter(new ImageAdapter(this));
  25.                 //设置AdapterView点击监听器,Gallery是AdapterView的子类
  26.                 g.setOnItemClickListener(new OnItemClickListener() {
  27.                         @Override
  28.                         public void onItemClick(AdapterView<?> parent, View view,
  29.                                         int position, long id) {
  30.                                 //显示点击的是第几张图片
  31.                                 Toast.makeText(MainHelloGallery.this, "" + position,
  32.                                                 Toast.LENGTH_LONG).show();
  33.                                 //设置背景部分的ImageView显示当前Item的图片
  34.                                 iv.setImageResource(((ImageView)view).getId());
  35.                         }
  36.                 });
  37.         }
  38.         //定义继承BaseAdapter的匹配器
  39.         public class ImageAdapter extends BaseAdapter {
  40.                 //Item的修饰背景
  41.                 int mGalleryItemBackground;
  42.                 //上下文对象
  43.                 private Context mContext;
  44.                 //图片数组
  45.                 private Integer[] mImageIds = { R.drawable.wallpaper_0,
  46.                                 R.drawable.wallpaper_1, R.drawable.wallpaper_2,
  47.                                 R.drawable.wallpaper_3, R.drawable.wallpaper_4,
  48.                                 R.drawable.wallpaper_5, R.drawable.wallpaper_6,
  49.                                 R.drawable.wallpaper_7, R.drawable.wallpaper_8,
  50.                                 R.drawable.wallpaper_9 };
  51.                 //构造方法
  52.                 public ImageAdapter(Context c){
  53.                         mContext = c;
  54.                         //读取styleable资源
  55.                 TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
  56.                 mGalleryItemBackground = a.getResourceId(
  57.                         R.styleable.HelloGallery_android_galleryItemBackground, 0);
  58.                 a.recycle();
  59.                 }
  60.                 //返回项目数量
  61.                 @Override
  62.                 public int getCount() {
  63.                         return mImageIds.length;
  64.                 }
  65.                 //返回项目
  66.                 @Override
  67.                 public Object getItem(int position) {
  68.                         return position;
  69.                 }
  70.                 //返回项目Id
  71.                 @Override
  72.                 public long getItemId(int position) {
  73.                         return position;
  74.                 }
  75.                 //返回视图
  76.                 @Override
  77.                 public View getView(int position, View convertView, ViewGroup parent) {
  78.                         ImageView iv = new ImageView(mContext);
  79.                         iv.setImageResource(mImageIds[position]);
  80.                         //给生成的ImageView设置Id,不设置的话Id都是-1
  81.                         iv.setId(mImageIds[position]);
  82.                         iv.setLayoutParams(new Gallery.LayoutParams(120, 160));
  83.                         iv.setScaleType(ImageView.ScaleType.FIT_XY);
  84.                         iv.setBackgroundResource(mGalleryItemBackground);
  85.                         return iv;
  86.                 }
  87.         }
  88. }
复制代码


我们点击某一张图片,会把该子元素的图片显示在放在后面一层的ImageView组件中。有兴趣的同学可以了解一下AdapterView的继承关系:2.jpg 
posted @ 2012-06-05 16:39  Johnny Yan  阅读(173)  评论(0)    收藏  举报