第31讲 UI组件之 Gallery画廊控件

1.Gallery的简介

Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息。Gallery只能水平显示一行,且Gallery列表中的图片会根据不同的拖动情况向左或向右移动,直到最后一张图片为止。Gallery还可以和ImageSwitcher组件结合使用来实现一个通过缩略图来浏览图片的效果。

Gallery常用的XML属性

属性名称

描述

android:animationDuration

设置布局变化时动画的转换所需的时间(毫秒级)。仅在动画开始时计时。该值必须是整数,比如:100。

android:gravity

指定在对象的X和Y轴上如何放置内容。指定一下常量中的一个或多个(使用 “|”分割)

Constant

Value

Description

top

0x30

紧靠容器顶端,不改变其大小

bottom

0x50

紧靠容器底部,不改变其大小

left

0x03

紧靠容器左侧,不改变其大小

right

0x05

紧靠容器右侧,不改变其大小

center_vertical

0x10

垂直居中,不改变其大小

fill_vertical

0x70

垂直方向上拉伸至充满容器

center_horizontal

0x01

水平居中,不改变其大小

Fill_horizontal

0x07

水平方向上拉伸使其充满容器

center

0x11

居中对齐,不改变其大小

fill

0x77

在水平和垂直方向上拉伸,使其充满容器

clip_vertical

0x80

垂直剪切(当对象边缘超出容器的时候,将上下边缘超出的部分剪切掉)

clip_horizontal

0x08

水平剪切(当对象边缘超出容器的时候,将左右边缘超出的部分剪切掉)

android:spacing

图片之间的间距

android:unselectedAlpha

设置未选中的条目的透明度(Alpha)。该值必须是float类型,比如:“1.2”。

一、实现图片的左右滑动浏览效果。

//Gallery的使用方法类似于其余的ViewGroup控件,使用adapter进行布局。

Gallery gallery=(Gallery)findViewById(R.id.gallery1);

//设置图片适配器

gallery.setAdapter(new MyAdapter(image,this));

//image:定义整型数组 即图片源

final int[] image=new int[] {R.drawable.attack , R.drawable .boy1 , R.drawable .boy2 , R.drawable .doupo };

MyAdapter定义如下:

private class MyAdapter extends BaseAdapter{

private int[] image;

private Context context;

public MyAdapter(int[] image, Context context) {

super();

this.image = image;

this.context = context;

}

public int getCount() { return image.length; } //获取图片的个数

public Object getItem(int arg0) { return null; }

public long getItemId(int arg0) { return 0; }

public View getView(int arg0, View arg1, ViewGroup arg2) {

ImageView imageView=new ImageView(context);

imageView.setImageResource(image[arg0]); //给ImageView设置资源

return imageView;

}

}

二、实现缩略图加放大图显示

对上述程序做修改:

首先,将layout设置为上方为ImageView,下方为Gallery显示缩略图。

之后,通过设置点击Gallery时设置图片到ImageView中。

final ImageView imageView=(ImageView)findViewById(R.id.imageView1);

Gallery gallery=(Gallery) findViewById(R.id.gallery1);

//设置图片适配器

gallery.setAdapter(new MyAdapter(image,this));

//设置监听器

gallery.setOnItemClickListener(newOnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, intposition, long arg3) {

imageView.setImageResource(image[position]);

}

});

public View getView(int arg0, View arg1,ViewGroup arg2) {

ImageView imageView=new ImageView(context);

//设置布局图片120x120显示

imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));

imageView.setImageResource(image[arg0]); //给ImageView设置资源

return imageView;

}

第31讲 UI组件之 Gallery画廊控件的更多相关文章

  1. 第32讲 UI组件之 时间日期控件DatePicker和TimePicker

    第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置,    Time ...

  2. UI组件之AdapterView及其子类(四)Gallery画廊控件使用

    听说 Gallery如今已经不使用了,API使用ViewPaper取代了,以后再学专研ViewPaper吧如今说说Gallery画廊,就是不停显示图片的意思 Gallery是用来水平滚动的显示一系列项 ...

  3. Android 高级UI设计笔记11:Gallery(画廊控件)之Gallery基本使用

    1. 这里要向大家介绍Android控件Gallery(画廊控件) Gallery控件主要用于横向显示图像列表,不过按常规做法.Gallery组件只能有限地显示指定的图像.也就是说,如果为Galler ...

  4. 第29讲 UI组件之 ListView与 BaseAdapter,SimpleAdapter

    第29讲 UI组件之 ListView与 BaseAdapter,SimpleAdapter 1.BaseAdapter BaseAdapter是Android应用程序中经常用到的基础数据适配器,它的 ...

  5. 第16讲- UI组件之TextView

    第16讲 UI组件之TextView Android系统所有UI类都是建立在View和ViewGroup这两类的基础上的. 所有View的子类称为widget:所有ViewGroup的子类称为Layo ...

  6. 第34讲 UI组件之 ProgressDialog和Message

    第34讲UI组件之 ProgressDialog和Message 1.进度对话框 ProgressDialog <1>简介 ProgressDialog是AlertDialog类的一个扩展 ...

  7. 第33讲 UI组件_进度条ProcessBar和消息队列处理器handler

    第33讲UI组件_进度条ProcessBar和消息队列处理器handler 1. 进度条ProcessBar 一个可视化的进度指示器,代表正在执行的耗时任务.可以为用户展示一个进度条,表示正在执行的任 ...

  8. 第30讲 UI组件之 GridView组件

    第30讲 UI组件之 GridView组件 1.网格布局组件GridView GridView是一个ViewGroup(布局控件),可使用表格的方式显示组件,可滚动的控件.一般用于显示多张图片,比如实 ...

  9. 第28讲 UI组件之 ListView和ArrayAdapter

    第28讲 UI组件之 ListView和ArrayAdapter 1. Adapter 适配器 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的 ...

随机推荐

  1. 验证合法的url

    package test; import java.util.regex.Matcher;import java.util.regex.Pattern; public class Test { pub ...

  2. 命令行修改linux系统IP

    修改配置文件/etc/sysconfig/network-scrips/ifcfg-eth0.因为机子启动的时候加载的就是这个文件的配置参数.对这个文件进行修改:   [root@localhost ...

  3. [RxJS] Creation operator: of()

    RxJS is a lot about the so-called "operators". We will learn most of the important operato ...

  4. Bitmap的一些操作

    1.截取 Bitmap 的部分区域 mBitmap = Bitmap.createBitmap(bmp, 100, 100, 120, 120); 这句代码从 bmp 的 (100,100) 处截取 ...

  5. 输入一个字符串,输出时数字倒序。例如:输入"hello2345wo7654",输出则为"hello5432wo4567"

    public class ReserveString { public static void main(String[] args) { System.out.println("Pleas ...

  6. virtual-虚方法

    看来本人理论果然不行啊,这个东西折腾死我了.即便是到现在,还是云里雾里.... 个人认为virtual的特点就是可以被override而不是必需的,到目前为止我用它的地方也比较少. public cl ...

  7. 整理:C#写ActiveX, 从代码到打包到签名到发布的示例

    对于不懂C++和VB的我, 在工作上却遇到需要重写旧ActiveX控件的任务. 好在客户机都是Windows PC, 基本上都有.net framework 2.0, 勉强用C#实现可以满足需求 所以 ...

  8. javascript sort 用法

    <html> <head> <title></title> <script type="text/javascript" sr ...

  9. svn服务器的配置步骤

    1.安装客户端: TortoiseSVN-1.9.3.27038-x64-svn-1.9.3.msi下载地址:http://jaist.dl.sourceforge.net/project/torto ...

  10. ORACLE网络配置大全没有比这个更详细的【weber出品】

    一.起篇 现在怎么说也是互联网时代,数据库也要联网,很多朋友学习Oracle的时候无外乎搭建的是以下两种学习环境: 1.直接在windows环境下安装Oracle后直接sqlplus连接. 2.在wi ...