android学习笔记15——Galley
Gallery==>画廊视图
Gallery和Spinnery父类相同——AbsSpinner,表明Garrey和Spinner都是一个列表框。
两者之间的区别是:Spinner显示的是一个垂直列表框,Gallery显示的是一个水平列表框;
Spinner的作用是供用户选择,而Gallery则允许用户通过拖动来查看上一个、下一个列表项。
Garrey常用XML属性:
| android:animationDuration | setAnimationDuration(int) | 设置列表项切换时的动画帧持续时间 |
| android:gravity | setGravity(int) | 设置对其方式 |
| android:spacing | setSpacing(int) | 设置Gallery内列表项之间的间距 |
| android: unselectedAlpha | setUnselectedAlpha(float) | 设置没有选中的列表项的透明度 |
注意:
Gallery用法类似Spinner,使用Adapter提供数据源,Adapter的getView()所返回的View将作为Gallery列表的列表项;
通过OnItemSelectedListener监听器监听选择项的改变。
实例一
布局文件==》
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <ImageSwitcher
android:id="@+id/switcher"
android:layout_width="320dp"
android:layout_height="320dp" /> <Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spacing="3pt"
android:unselectedAlpha="0.6" /> </LinearLayout> 代码实现==》
package com.example.mygrallery; import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory; @SuppressWarnings("deprecation")
@SuppressLint("InlinedApi")
public class MainActivity extends Activity
{
private int[] ImageIds = new int[]
{ R.drawable.one, R.drawable.tw, R.drawable.th, R.drawable.eight, R.drawable.ele,
R.drawable.five, R.drawable.four, R.drawable.nice, R.drawable.seven, R.drawable.six,
R.drawable.sl, R.drawable.ss, R.drawable.sw, R.drawable.ten, R.drawable.tw,
R.drawable.oneowne }; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final ImageSwitcher switcher = (ImageSwitcher) this.findViewById(R.id.switcher);
final Gallery gallery = (Gallery) this.findViewById(R.id.gallery); switcher.setFactory(new ViewFactory()
{
@Override
public View makeView()
{
ImageView img = new ImageView(MainActivity.this);
img.setBackgroundColor(0xff0000);
img.setScaleType(ImageView.ScaleType.FIT_CENTER);
img.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return img;
}
});
// 设置图片更换的动画效果
switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
// 创建DataAdapter对象,为gallery提供数据
BaseAdapter adapter = new BaseAdapter()
{
@Override
public int getCount()
{
// TODO Auto-generated method stub
return ImageIds.length;
} @Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
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 img = new ImageView(MainActivity.this);
img.setImageResource(ImageIds[position % ImageIds.length]);
// 设置ImageView的缩放类型
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setLayoutParams(new Gallery.LayoutParams(75, 110));
// TypedArray arr= obtainStyledAttributes(R.)
// img.setBackgroundResource(resid); return img;
}
}; gallery.setAdapter(adapter);
gallery.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// TODO Auto-generated method stub
switcher.setImageResource(ImageIds[position % ImageIds.length]);
} @Override
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
实现效果如下:

android学习笔记15——Galley的更多相关文章
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- Android学习笔记进阶17之LinearGradient
具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...
- Android学习笔记进阶16之BitmapShader
<1>简介 具体的看一下博文:Android学习笔记进阶15之Shader渲染 public BitmapShader(Bitmap bitmap,Shader.TileMode ti ...
- Android学习笔记(一)
目录 Android学习笔记(一) 一.JDK.Android SDK 二.部分项目结构 三.字符串引用 四.外层build.gradle详解 五.app->build.gradle详解 六.日 ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Ext.Net学习笔记15:Ext.Net GridPanel 汇总(Summary)用法
Ext.Net学习笔记15:Ext.Net GridPanel 汇总(Summary)用法 Summary的用法和Group一样简单,分为两步: 启用Summary功能 在Feature标签内,添加如 ...
- udacity android 学习笔记: lesson 4 part b
udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...
随机推荐
- 读metronic文档学到的几个知识点
1.RTL 同样的页面,它做了两套.为什么,因为在这个世界上,有些民族,有些语种,是从右向左来的. 2. google material design 同样的一套东西,又分别做了google mat ...
- 三分钟了解Activity工作流
一. 什么是工作流 以请假为例,现在大多数公司的请假流程是这样的 员工打电话(或网聊)向上级提出请假申请——上级口头同意——上级将请假记录下来——月底将请假记录上交公司——公司将请假录入电脑 采用工作 ...
- signtool对EXE进行签名
https://msdn.microsoft.com/zh-cn/library/9sh96ycy(VS.80).aspx .NET Framework 2.0 其他版本 文件签名工具使用 A ...
- SAP系统联机应用程序帮助
新安装好的SAP系统,联机帮助是不能用的. 通过菜单中的“帮助-应用程序帮助” 和“帮助-SAP库”都打不开任何帮助页面.这并不是因为SAPgui安装不完整,而是因为SAP的帮助系统本身就不包含在GU ...
- tools/version.c
/* * linux/version.c * * Copyright (C) 1992 Theodore Ts'o * * May be freely distributed as part ...
- mysql in和or查询效率
http://blog.chinaunix.net/uid-20639775-id-3416737.html 上面链接博主的文章分析结论: or在没有索引的情况下呈指数增长,in则是正常递增. or的 ...
- UVa 272 Tex Quotes --- 水题
题目大意:在TeX中,左引号是 ``,右引号是 ''.输入一篇包含双引号的文章,你的任务是把他转成TeX的格式 解题思路:水题,定义一个变量标记是左引号还是右引号即可 /* UVa 272 Tex Q ...
- CoderForces 280B(记忆化搜索)
题目大意:一个纸牌游戏,52张纸牌排成一列,每张纸牌有面值和花色两种属性.每次操作可以用最后一张纸牌将倒数第二张或者倒数第四张替换,但前提是两张牌的花色或者面值相同.问最终能否只剩一张牌. 题目分析: ...
- Java Web学习(1): 客户端请求、服务器响应及其HTTP状态码
一JSP客户端请求 当浏览器请求一个网页时,它会向网络服务器发送一系列不能被直接读取的信息,因为这些信息是作为HTTP信 息头的一部分来传送的.我们可以查阅HTTP协议来获得更多的信息. 下表列出了浏 ...
- python程序的调试方法
[转自:http://blog.csdn.net/luckeryin/article/details/4477233] 本文讨论在没有方便的IDE工具可用的情况下,使用pdb调试python程序 ...