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. java代码switch语句求分数等级

    总结:从键盘输入分数----- 如果在0到100内,则输出等级 小于0或者是大于100都不能输出,这里用if-else条件判断. package com.c2; import java.util.Sc ...

  2. Error EBUSY: osd.0 is still up; must be down before removal的解决办法

    标签(空格分隔):ceph,ceph运维,osd故障 集群环境: [root@node3 ~]# cat /etc/redhat-release CentOS Linux release 7.3.16 ...

  3. vue-cli中的ESlint配置文件eslintrc.js详解

    本文讲解vue-cli脚手架根目录文件.eslintrc.js eslint简介 eslint是用来管理和检测js代码风格的工具,可以和编辑器搭配使用,如vscode的eslint插件 当有不符合配置 ...

  4. Mybites和hibernate的优缺点和区别

    Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀的O/R mapping框架.目前属于apache的 ...

  5. hadoop本地调试方法

    Mapreduce 是Hadoop上一个进行分布式数据运算和统计的框架,但是每次运行程序的时候都需要将程序打包并上传的集群环境中运行,这就会让程序的调试变得十分不方便.所以在这里写下这篇博客和大家交流 ...

  6. class function

    type   TTest = class    public       class function sayHello :String;       function saybye:String; ...

  7. WordPress,discuz 根据不同的入口url更换logo

    Discuz!中调用cookie的思路出来了:    设置cookie:dsetcookie('cookie名', 'cookie值', '有效时间'); 读取cookie有两种方法,第一种使用get ...

  8. easylogging++学习记录(一):接入

    easylogging++是一个非常轻量级并且非常高效的一个日志库,支持文件配置,支持线程安全,并且其自定义格式非常的方便,最关键的是,其所有代码都集中在一个.h头文件之中,完全不需要引用第三方库,接 ...

  9. dyld_decache&MesaSQLite

    [dyld_decache] Starting from iPhone OS 3.1, the individual libraries files supplied by the system ar ...

  10. jQuery+css模拟下拉框模糊搜索的实现

    html: @*输入框*@ <div> <input type="text" style="width: 85%; height: 34px;" ...