Gallery和GridView浅析
来源:互联网 发布:算法的基本特征 编辑:程序博客网 时间:2024/06/10 05:56
一.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单独使用的例子:
MainActivity.java
- package com.android.gallerydemo;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.BaseAdapter;
- import android.widget.Gallery;
- import android.widget.ImageView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private Gallery gallery;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- gallery = (Gallery)findViewById(R.id.gallery);
- //设置图片适配器
- gallery.setAdapter(new ImageAdapter(this));
- //设置监听器
- gallery.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?>parent, View v, int position, long id) {
- Toast.makeText(MainActivity.this, "点击了第"+(position+1)+"张图片", Toast.LENGTH_LONG).show();
- }
- });
- }
- }
- class ImageAdapter extends BaseAdapter{
- //声明Context
- private Context context;
- //图片源数组
- private Integer[] imageInteger={
- R.drawable.pic1,
- R.drawable.pic2,
- R.drawable.pic3,
- R.drawable.pic4,
- R.drawable.pic5,
- R.drawable.pic6,
- R.drawable.pic7
- };
- //声明 ImageAdapter
- public ImageAdapter(Context c){
- context = c;
- }
- @Override
- //获取图片的个数
- public int getCount() {
- return imageInteger.length;
- }
- @Override
- //获取图片在库中的位置
- public Object getItem(int position) {
- return position;
- }
- @Override
- //获取图片在库中的位置
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageView = new ImageView(context);
- //给ImageView设置资源
- imageView.setImageResource(imageInteger[position]);
- //设置比例类型
- imageView.setScaleType(ImageView.ScaleType.FIT_XY);
- //设置布局 图片128x192显示
- imageView.setLayoutParams(new Gallery.LayoutParams(128, 192));
- return imageView;
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <Gallery xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/gallery"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_vertical"
- android:background="?android:galleryItemBackground"
- />
效果图:
- Gallery和GridView浅析
- Gallery和GridView浅析
- Gallery和GridView浅析
- Android开发学习笔记:Gallery和GridView浅析
- Android开发学习笔记:Gallery和GridView浅析
- Android开发学习笔记:Gallery和GridView浅析
- Android开发学习笔记:Gallery和GridView浅析
- Android开发学习笔记:Gallery和GridView浅析
- Android开发学习笔记:Gallery和GridView浅析
- Android开发学习笔记:Gallery和GridView浅析
- Gallery和GridView 小结
- Gallery和GridView
- GridView和Gallery基本用法
- android之GridView和Gallery
- Android的GridView和Gallery结合Demo
- Android的GridView和Gallery结合Demo
- GridView Gallery Spinner AutoCompleteTextView和ExpandableListView
- Android:Gallery,ImageSwitcher 和GridView用法
- Linux下的motion detection(最简单的办公室监控系统)
- ubuntu_桌面快捷键
- 1、Android开发环境的搭建
- 做这样一个男人
- 设计模式
- Gallery和GridView浅析
- 【转载】多态性——vptr和vtable
- Android4.2中全屏或者取消标题栏
- 状态栏通知Notification、NotificationManager详解
- 设计模式之原则
- C++制作动态链接库dll
- 如何向内核中添加源码
- 黑马程序员_解决Servlet中文乱码
- Apache Ant运行时Unable to locate tools.jar解决方法