码迷,mamicode.com
首页 > 移动开发 > 详细

Android 自学之画廊视图(Gallery)功能和用法

时间:2014-08-19 16:09:05      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   http   color   java   os   

Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框。他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显示的是一个水平的列表框。Gallery与Spinner有一个区别:Spinner的作用是供用户选择,而Gallery则允许用户通过拖动来查看上一个、下一个列表项。

Gallery常用的XML属性及相关方法

XML属性 相关方法 说明
android:animationDuration setAnimationDuration(int) 设置列表项切换时的动画持续时间
android:gravity setGravity(int) 设置对齐方式
android:spacing setSpacing(int) 设置Gallery内列表项之间的间距
android:unselectedAlpha setUnselectedAlpha(int) 设置没有选中列表项的透明性

Gallery本身的用法非常简单----基本上和Spinner的用法相似,只要为他提供一个内容Adapter即可,该Adapter的getView方法返回的View作为Gallery列表的列表项;如果程序需要监控到Gallery选择项的改变,可以通过为Gallery添加OnItemSelectedListener监听器即可实现。

为此做了一个关于“幻灯片”式图片浏览器的案例,这个案例和我们前面写的Android 自学之网格试图(GridView)和图片切换器(ImageSwitcher)功能和用法这个里面的案例很相似;二者都是带有预览图片的效果,但本案例采用Gallery作为图片预览器,所以界面会更加的友好。Gallery会生成一个“水平列表”,每个列表项就是一张图片预览,而Gallery生成“水平列表”可以让用户拖动来切换列表项。

下面我们先看看案例的布局部分的代码:Layout/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 <!-- 定义一个ImageSwitcher组件 -->
 7 <ImageSwitcher android:id="@+id/switcher"
 8     android:layout_width="320dp"
 9     android:layout_height="320dp"
10     />
11 <!-- 定义一个Gallery组件 -->
12 <Gallery android:id="@+id/gallery"
13     android:layout_width="fill_parent"
14     android:layout_height="wrap_content"
15     android:layout_marginTop="25dp" 
16     android:unselectedAlpha="0.6"
17     android:spacing="3pt"
18     />    
19 </LinearLayout>

上面的布局很简单,只定义了两个组件:ImageSwitcher和Gallery组件。主程序也很简单,为ImageSwitcher传入一个ViewFactory对象,为Gallery传入一个Adapter对象。这样ImageSwitcher和Gallery就可以工作了。

为了让ImageSwitcher可以显示Gallery中选中的图片,为Gallery绑定一个叫做OnItemSelectedListener监听。

主程序:GalleryTest.java

  1 package com.yangjing.gallarytest;
  2 
  3 import android.app.Activity;
  4 import android.content.res.TypedArray;
  5 import android.os.Bundle;
  6 import android.view.View;
  7 import android.view.ViewGroup;
  8 import android.view.ViewGroup.LayoutParams;
  9 import android.view.animation.AnimationUtils;
 10 import android.widget.AdapterView;
 11 import android.widget.AdapterView.OnItemSelectedListener;
 12 import android.widget.BaseAdapter;
 13 import android.widget.Gallery;
 14 import android.widget.ImageSwitcher;
 15 import android.widget.ImageView;
 16 import android.widget.ViewSwitcher.ViewFactory;
 17 
 18 public class GallaryTest extends Activity{
 19 
 20     int[] imageIds = new int[]
 21     {
 22         R.drawable.shuangzi, R.drawable.shuangyu,
 23         R.drawable.chunv, R.drawable.tiancheng, R.drawable.tianxie,
 24         R.drawable.sheshou, R.drawable.juxie, R.drawable.shuiping,
 25         R.drawable.shizi, R.drawable.baiyang, R.drawable.jinniu,
 26         R.drawable.mojie };
 27 
 28     @Override
 29     public void onCreate(Bundle savedInstanceState)
 30     {
 31         super.onCreate(savedInstanceState);
 32         setContentView(R.layout.main);
 33         final Gallery gallery = (Gallery) findViewById(R.id.gallery);
 34         // 获取显示图片的ImageSwitcher对象
 35         final ImageSwitcher switcher = (ImageSwitcher) 
 36             findViewById(R.id.switcher);
 37         // 为ImageSwitcher对象设置ViewFactory对象
 38         switcher.setFactory(new ViewFactory()
 39         {
 40             @Override
 41             public View makeView()
 42             {
 43                 ImageView imageView = new ImageView(GallaryTest.this);
 44                 imageView.setBackgroundColor(0xff0000);
 45                 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
 46                 imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
 47                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
 48                 return imageView;
 49             }
 50         });
 51         // 设置图片更换的动画效果
 52         switcher.setInAnimation(AnimationUtils.loadAnimation(this,
 53             android.R.anim.fade_in));
 54         switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
 55             android.R.anim.fade_out));
 56         // 创建一个BaseAdapter对象,该对象负责提供Gallery所显示的图片
 57         BaseAdapter adapter = new BaseAdapter()
 58         {
 59             @Override
 60             public int getCount()
 61             {
 62                 return imageIds.length;
 63             }
 64             @Override
 65             public Object getItem(int position)
 66             {
 67                 return position;
 68             }
 69             @Override
 70             public long getItemId(int position)
 71             {
 72                 return position;
 73             }
 74 
 75             // 该方法的返回的View就是代表了每个列表项
 76             @Override
 77             public View getView(int position, View convertView, ViewGroup parent)
 78             {
 79                 // 创建一个ImageView
 80                 ImageView imageView = new ImageView(GallaryTest.this);
 81                 imageView.setImageResource(imageIds[position % imageIds.length]);
 82                 // 设置ImageView的缩放类型
 83                 imageView.setScaleType(ImageView.ScaleType.FIT_XY);
 84                 imageView.setLayoutParams(new Gallery.LayoutParams(75, 100));
 85                 TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
 86                 imageView.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0));
 87                 return imageView;
 88             }
 89         };
 90         gallery.setAdapter(adapter);
 91         gallery.setOnItemSelectedListener(new OnItemSelectedListener()
 92         {
 93             // 当Gallery选中项发生改变时触发该方法
 94             @Override
 95             public void onItemSelected(AdapterView<?> parent, View view,
 96                 int position, long id)
 97             {
 98                 switcher.setImageResource(imageIds[position % imageIds.length]);
 99             }
100 
101             @Override
102             public void onNothingSelected(AdapterView<?> parent)
103             {
104             }
105         });
106     }
107 }

 

程序运行后的效果展示:

bubuko.com,布布扣

Android 自学之画廊视图(Gallery)功能和用法,布布扣,bubuko.com

Android 自学之画廊视图(Gallery)功能和用法

标签:des   android   style   blog   http   color   java   os   

原文地址:http://www.cnblogs.com/Yang-jing/p/3922119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!