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详解 六.日 ...
随机推荐
- java排序。。简单的冒泡排序
总结:一种简单的交换顺序,从数左边开始扫描待排序的元素,在扫描过程中依次对相邻元素进行比较,将较大值后移,每经过一轮排序后,值最大的元素将移到末尾, 此时记下该元素的位置,下一轮排序只需比较到此位置即 ...
- mysql函数之一:INSTR、LOCATE、POSITION VS LIKE
LOCATE.POSITION和INSTR函数相似功能实例 使用LOCATE.POSITION和INSTR函数查找字符串中指定子字符串的开始位置.SQL语句如下: mysql>SELECT LO ...
- PHP生成唯一固定长度邀请码
function create_invite_code() { $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $rand = $code[rand(0,25)] .str ...
- jQuery笔记——事件
常用的事件有:click.dblclick. mousedown.mouseup.mousemove.mouseover.mouseout.change.select.submit.keydown. ...
- Unity3D版本之我见
关心Unity版本的变化以及了解未来版本的内容是专业做Unity的同学必备的功课,下面我来说一下我对4.0以后版本的一些见解. v4.0: 这个版本比3.5有较大的跳跃,首先最大卖点是新的动作系统Me ...
- Winform Datagridview 单元格html格式化支持富文本
Winform Datagridview 单元格html格式化支持富文本 示例: 源码:https://github.com/OceanAirdrop/DataGridViewHTMLCell 参考: ...
- jquery中选中复选框1.8之前与1.8之后的区别
在jquery 1.8.x中的版本,我们对于checkbox的选中与不选中操作如下: 判断是否选中 $('#checkbox').prop('checked') 设置选中与不选中状态: $('#che ...
- 浅谈PL/SQL语言基础
在前面的学习中,我们大部分接触的都是SQL语言,但是,在实现复杂操作的时候,SQL语言就无能为力了,这时候就需要引入新的语言,PL/SQL语言就是对SQL语言的扩展,可以实现存储过程,函数等的创建.下 ...
- node.js开发指南读书笔记(1)
3.1 开始使用Node.js编程 3.1.1 Hello World 将以下源代码保存到helloworld.js文件中 console.log('Hello World!'); console.l ...
- Areas in ASP.NET MVC 4
Download source - 2.7 MB Introduction to Areas In this article, we will learn the concept of Areas a ...