Android学习笔记 Gallery图库组件的使用
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图库组件的使用的更多相关文章
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- Android学习笔记之Activity详解
1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...
- 【转】 Pro Android学习笔记(八二):了解Package(1):包和进程
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在之前,我们已经学习了如何签发apk,见P ...
- 【转】 Pro Android学习笔记(七六):服务(1):local和remote
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ Android提供服务,服务是运行在后台的 ...
- 【转】Pro Android学习笔记(二三):用户界面和控制(11):其他控件
目录(?)[-] Chronometer计时器控件 倒计时CountDownTimer Switch控件 Space控件 其他控件 Android提供了很多控件,基本上都是view的扩展. Chron ...
- 【转】Pro Android学习笔记(三):了解Android资源(上)
在Android开发中,资源包括文件或者值,它们和执行应用捆绑,无需在源代码中写死,因此我们可以改变或替换他们,而无需对应用重新编译. 了解资源构成 参考阅读Android学习笔记(三八):资源res ...
- 【转】Pro Android学习笔记(二):开发环境:基础概念、连接真实设备、生命周期
在Android学习笔记(二):安装环境中已经有相应的内容.看看何为新.这是在source网站上的Android架构图,和标准图没有区别,只是这张图颜色好看多了,录之.本笔记主要讲述Android开发 ...
- Android学习笔记:Home Screen Widgets(2):关于Widget
通过widget定义,我们在widget列表中看到了我们的TestWidget.当我们拖拽widget到主页时,假设在appwidet-provider中定义了android:configure的ja ...
- Android学习笔记(一)
目录 Android学习笔记(一) 一.JDK.Android SDK 二.部分项目结构 三.字符串引用 四.外层build.gradle详解 五.app->build.gradle详解 六.日 ...
随机推荐
- 字符编码py2,py3操作,SecureCRT的会话编码的设置
对之前的字符串类型和二进制类型(bytes类型),可以这样关联记忆,把字符串类型当作是Unicode,把bytes类型当作是GBK或者UTF-8或者是日文编码.这样字符串要转成二进制,那么就需要编码e ...
- xunsearch搜索使用
目录 如何开始搜索? 典型处理 快捷操作 搜索中的串接操作 构建搜索语句 如何开始搜索? <?php // 引入 require_once './sdk/xs/lib/XS.php'; // 创 ...
- 第三章 深入分析Java Web中的中文编码问题
3.1 几种常见的编码格式 3.1.1 为什么要编码 一个字节 byte只能表示0~255个符号,要表示更多的字符,需要编码. 3.1.2 如何翻译 ASCII码:有128个,用一个字节的低7位表示. ...
- highcharts钻取例子
<!doctype html> <html lang="en"> <head> <script type="text/javas ...
- javascript好文分享
JavaScript精华 http://www.cnblogs.com/jesse2013/p/the-part-of-javascript-you-must-know.html JavaScript ...
- 一个简单的Python多线程
首先对一段普通的代码进行测试 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import time def RunThrea ...
- java成神之——安全和密码
安全和密码 加密算法 公钥和私钥加密解密 生成私钥和公钥 加密数据 解密数据 公钥私钥生成的不同算法 密钥签名 生成加密随机数 基本用法 指定算法 加密对象 SealedObject Signatur ...
- leetcode582
public class Solution { public IList<int> KillProcess(IList<int> pid, IList<int> p ...
- 【转】VS 安全开发生命周期(SDL)检查
前面在学习使用google的protobuf时在VS2012中一直无法编译编译通过,经过查找一些资料原来发现,并不是protobuf的问题,而是自己在使用VS2012时,没有完全了解VS2012的强大 ...
- 可变、不可变数据类型和hash
一.可变和不可变数据类型 在python中,我们对数据类型除了分为数字类型.字符串类型.列表类型.元组类型.字典类型和集合类型外, 还有另外一种分类方式,我们给数据类型分为可变数据类型和不可变数据类型 ...