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的更多相关文章

  1. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  2. Android学习笔记进阶17之LinearGradient

    具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...

  3. Android学习笔记进阶16之BitmapShader

    <1>简介 具体的看一下博文:Android学习笔记进阶15之Shader渲染 public   BitmapShader(Bitmap bitmap,Shader.TileMode ti ...

  4. Android学习笔记(一)

    目录 Android学习笔记(一) 一.JDK.Android SDK 二.部分项目结构 三.字符串引用 四.外层build.gradle详解 五.app->build.gradle详解 六.日 ...

  5. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  6. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  7. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  8. Ext.Net学习笔记15:Ext.Net GridPanel 汇总(Summary)用法

    Ext.Net学习笔记15:Ext.Net GridPanel 汇总(Summary)用法 Summary的用法和Group一样简单,分为两步: 启用Summary功能 在Feature标签内,添加如 ...

  9. udacity android 学习笔记: lesson 4 part b

    udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

随机推荐

  1. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  2. html部分---表单、iframe、frameset及其他字符的用法(以及name、id、value的作用与区别);

    <form action="aa.html" method="post/get"> /action的作用是提交到..,methed是提交方法,用po ...

  3. ✡ leetcode 162. Find Peak Element --------- java

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  4. php 倒计时程序

    <html>  <head>  <meta http-equiv="Content-Type" content="text/html; ch ...

  5. URAL(timus) 1280 Topological Sorting(模拟)

    Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world champio ...

  6. dedecms 忘记后台密码

    找到admin表 dede_admin,把其pwd的值修改为 默认的 字符串:f297a57a5a743894a0e4, 之后,你的密码就被充值为 admin

  7. 内存使用空间之swap建置[转]

    http://www.cnblogs.com/ggjucheng/archive/2012/08/22/2651502.html 内存置换空间(swap)之建置 安装时一定需要的两个 partitio ...

  8. python [吐槽]关于nan类型时遇到的问题

    今天在用写一段求和的代码时候,发现最后返回的是nan的结果,这段循环求和代码依次调用了三个函数,于是依次打印这三个函数的返回值,发现其中一个函数的返回值为nan,原来是因为这段函数里面没有相似的用户, ...

  9. 在easyui datagrid中formatter数据后使用linkbutton

    http://ntzrj513.blog.163.com/blog/static/2794561220139245411997/ formatter:function(value,rowData,ro ...

  10. hibernate--HQL语法与详细解释

    HQL查询: Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此 Hi ...