activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:gravity="bottom" >
<ImageSwitcher
android:id="@+id/myImageSwitcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" />
<Gallery
android:id="@+id/myGallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/> </LinearLayout>

grid_lyout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <ImageView
android:id="@+id/myImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
/> </LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
private Gallery myGallery=null;
private SimpleAdapter mySimpleAdapter=null;
private List<Map<String,Integer>> list=new ArrayList<Map<String,Integer>>();
private ImageSwitcher myImageSwitcher=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); this.myGallery=(Gallery)super.findViewById(R.id.myGallery);
this.myGallery.setOnItemClickListener(new OnItemClickListenerImpl());
this.initAdapter();
this.myGallery.setAdapter(this.mySimpleAdapter);
this.myImageSwitcher=(ImageSwitcher)super.findViewById(R.id.myImageSwitcher);
this.myImageSwitcher.setFactory(new ViewFactoryImpl());
} private void initAdapter(){
Field[] fields=R.drawable.class.getDeclaredFields();//取得全部属性
for (int i = 0; i < fields.length; i++) {
if(fields[i].getName().startsWith("png_")){ //以png_开头的文件
Map<String,Integer> map=new HashMap<String, Integer>();
try{
map.put("img", fields[i].getInt(R.drawable.class));
}catch(Exception e){ }
this.list.add(map);
}
}
this.mySimpleAdapter=new SimpleAdapter(
this,
this.list,
R.layout.grid_layout,
new String[] {"img"},
new int[]{R.id.myImg}
);
} private class OnItemClickListenerImpl implements OnItemClickListener{ @Override
public void onItemClick(AdapterView<?> parent, View view, int postion,
long id) {
// TODO Auto-generated method stub
//Toast.makeText(MainActivity.this, String.valueOf(postion), Toast.LENGTH_SHORT).show();
Map<String, Integer> map=(Map<String,Integer>) parent.getAdapter().getItem(postion);
MainActivity.this.myImageSwitcher.setImageResource(map.get("img"));
} } private class ViewFactoryImpl implements ViewFactory{ @Override
public View makeView() {
ImageView img=new ImageView(MainActivity.this);
img.setBackgroundColor(0xFFFFFFFF);
img.setScaleType(ImageView.ScaleType.CENTER);
img.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return img;
} }

Android学习笔记 Gallery图库组件的使用的更多相关文章

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

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

  2. Android学习笔记之Activity详解

    1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...

  3. 【转】 Pro Android学习笔记(八二):了解Package(1):包和进程

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在之前,我们已经学习了如何签发apk,见P ...

  4. 【转】 Pro Android学习笔记(七六):服务(1):local和remote

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ Android提供服务,服务是运行在后台的 ...

  5. 【转】Pro Android学习笔记(二三):用户界面和控制(11):其他控件

    目录(?)[-] Chronometer计时器控件 倒计时CountDownTimer Switch控件 Space控件 其他控件 Android提供了很多控件,基本上都是view的扩展. Chron ...

  6. 【转】Pro Android学习笔记(三):了解Android资源(上)

    在Android开发中,资源包括文件或者值,它们和执行应用捆绑,无需在源代码中写死,因此我们可以改变或替换他们,而无需对应用重新编译. 了解资源构成 参考阅读Android学习笔记(三八):资源res ...

  7. 【转】Pro Android学习笔记(二):开发环境:基础概念、连接真实设备、生命周期

    在Android学习笔记(二):安装环境中已经有相应的内容.看看何为新.这是在source网站上的Android架构图,和标准图没有区别,只是这张图颜色好看多了,录之.本笔记主要讲述Android开发 ...

  8. Android学习笔记:Home Screen Widgets(2):关于Widget

    通过widget定义,我们在widget列表中看到了我们的TestWidget.当我们拖拽widget到主页时,假设在appwidet-provider中定义了android:configure的ja ...

  9. Android学习笔记(一)

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

随机推荐

  1. 地下迷宫探索(dfs)

    地下迷宫探索(30 分) 地道战是在抗日战争时期,在华北平原上抗日军民利用地道打击日本侵略者的作战方式.地道网是房连房.街连街.村连村的地下工事,如下图所示. 我们在回顾前辈们艰苦卓绝的战争生活的同时 ...

  2. 洛谷 P1975 [国家集训队]排队 Lebal:块内排序+树状数组

    题目描述 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和. 红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的身高有所区别 ...

  3. mysql核心参数优化

    MySQL数据库服务器配置CPU的优化内存的优化IO的优化连接的优化数据一致性的优化 1.描述back_log参数的作用? back_log = 500 要求 MySQL 能有的连接数量.当主要MyS ...

  4. 第2章 深入分析java I/O的工作机制(上)

    java的I/O操作类在包java.io下,大致分成4组: 所有文件的存储都是字节(byte)的储存,在磁盘上保留的并不是文件的字符而是先把字符编码成字节,再存储这些字节到磁盘.在读取文件时,也是一个 ...

  5. Windows下搭建PHP开发环境-WEB服务器

    PHP集成开发环境有很多,如XAMPP.AppServ......只要一键安装就把PHP环境给搭建好了.但这种安装方式不够灵活,软件的自由组合不方便,同时也不利于学习.所以我还是喜欢手工搭建PHP开发 ...

  6. Deep Learning 学习笔记(3):Linear Regression 数据的预处理

    为了获得良好的收敛,在进行梯度下降前,我们可以对数据进行预处理. 目标是使得数据大小在同一个数据数量级上,均值为零. 一般将数据放缩到(-1,1)区间, 我们可以对数据进行如下操作: 其中u1是数据的 ...

  7. 微信公众平台PHP示例一

    <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015-12-18 * Time: 21:51 */ define ...

  8. FPGA和CPLD的比较

    1 FPGA的集成度比CPLD高,具有更复杂的布线结构和逻辑实现. 2 CPLD更适合触发器有限而乘积丰富的结构,更适合完成复杂的组合逻辑:FPGA更适合于触发器丰富的结构,适合完成时序逻辑. 3 c ...

  9. c++多线程编程(二)

    这是道面试题目:有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC… 见代码: #include <iostream> #include <Wind ...

  10. Newtonsoft 序列化和反序列化特殊处理

    1>序列化,时间格式化处理 JsonConvert.SerializeObject(Iar, new JsonSerializerSettings() { DateFormatString = ...