Android学习之图片画廊(SimpleAdapter)
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/MyLayout"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="bottom"><ImageSwitcherandroid:id="@+id/myImageSwitcher"android:layout_width="fill_parent"android:layout_height="wrap_content"/><Galleryandroid:id="@+id/myGallery"android:gravity="center_vertical"android:spacing="3px"android:layout_width="fill_parent"android:layout_height="wrap_content"/> </LinearLayout>
grid_layout.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal" android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#FFFFFF"><ImageViewandroid:id="@+id/img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:scaleType="center"/> </LinearLayout>
MyGalleryDemo.java:
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.SimpleAdapter;
import android.widget.ViewSwitcher.ViewFactory;public class MyGalleryDemo extends Activity {private Gallery gallery = null; // 图片浏览private List<Map<String,Integer>> list = new ArrayList<Map<String,Integer>>() ;private SimpleAdapter simpleAdapter = null; // 适配器private ImageSwitcher myImageSwitcher = null ; // 图片切换@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.main); // 调用布局文件this.initAdapter() ; // 初始化适配器this.gallery = (Gallery) super.findViewById(R.id.myGallery) ;// 取得组件this.myImageSwitcher = (ImageSwitcher) super.findViewById(R.id.myImageSwitcher); // 取得组件this.myImageSwitcher.setFactory(new ViewFactoryImpl()) ;// 设置图片工厂this.gallery.setAdapter(this.simpleAdapter); // 设置图片集this.gallery.setOnItemClickListener(new OnItemClickListenerImpl()) ;// 设置单击事件}private class OnItemClickListenerImpl implements OnItemClickListener {@SuppressWarnings("unchecked")@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) { Map<String, Integer> map = (Map<String, Integer>) MyGalleryDemo.this.simpleAdapter.getItem(position); // 取出MapMyGalleryDemo.this.myImageSwitcher.setImageResource(map.get("img")); // 设置显示图片}}public void initAdapter(){ // 初始化适配器Field[] fields = R.drawable.class.getDeclaredFields(); for (int x = 0; x < fields.length; x++) { if (fields[x].getName().startsWith("ispic_")){ // 所有ispic_*命名的图片Map<String,Integer> map = new HashMap<String,Integer>() ; // 定义Maptry {map.put("img", fields[x].getInt(R.drawable.class)) ;} catch (Exception e) { // 设置图片资源}this.list.add(map) ; // 保存Map}}this.simpleAdapter = new SimpleAdapter(this, // 实例化SimpleAdapterthis.list, // 要包装的数据集合R.layout.grid_layout, // 要使用的显示模板new String[] { "img" }, // 定义要显示的Map的Keynew int[] {R.id.img }); // 与模板中的组件匹配}private class ViewFactoryImpl implements ViewFactory { // 定义视图工厂类@Overridepublic View makeView() {ImageView img = new ImageView(MyGalleryDemo.this); // 实例化图片显示img.setBackgroundColor(0xFFFFFFFF); // 设置背景颜色img.setScaleType(ImageView.ScaleType.CENTER); // 居中显示img.setLayoutParams(new ImageSwitcher.LayoutParams( // 自适应图片大小LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));// 定义组件return img;}}}
Android学习之图片画廊(SimpleAdapter)相关推荐
- 【Android学习】图片
1,ImageView(图片视图) 1)概念 ImageView继承自View组件,它的主要功能是用于显示图片,且任何Drawable对象都可使用ImageView来显示. Android中不仅可以将 ...
- Android学习之图片切换
main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:and ...
- Android学习之图片压缩,压缩程度高且失真度小
曾经在做手机上传图片的时候.直接获取相机拍摄的原图上传,原图大小一般1~2M.因此上传一张都比較浪费资源,有些场景还须要图片多张上传,所以近期查看了好多前辈写的关于图片处理的资料.然后试着改了一个图片 ...
- Android学习笔记26:图片切换控件ImageSwitcher的使用
在Windows操作系统中,要查看多张图片,可以通过使用"Windows照片查看器"在"上一张"和"下一张"之间切换,进行多张图片的浏览. ...
- Android学习小Demo(10)ToDoList的改进版之ViewPager显示多个图片
在TodoList增强版的增加界面上,为了显示图片,我是挖了两块地方,放了两个ImageButton,来显示图片,而且限制了最多只能放两张图片.当两个View都放置图片之后,我就会把"Gal ...
- 【Android-Kotlin-Volley】图片画廊学习笔记
下载地址: APP下载地址: https://wws.lanzous.com/b01tsdd7c 密码:dp1f Github: https://github.com/kirikaTowa/Galle ...
- Android学习笔记 2.5.3 实例——使用SimpleAdapter创建ListView 2.5.4 自动完成文本框(AutoCompleteTextView)的功能与用法
Android学习笔记 疯狂Android讲义 文章目录 Android学习笔记 疯狂Android讲义 第2章 Android 应用的界面编程 2.5 第4组 UI组件:AdapterView及其子 ...
- Android学习笔记之在图片特效
1.涂鸦(能清屏) HandWritingActivity.java [java] view plaincopy package xiaosi.handWriting; import android. ...
- Android学习之优化美女图片浏览器
接上一篇文章:Android学习之打造美女图片浏览器 上一篇博文其实我给大家留了好多坑,那么这篇文章我就一一把坑都埋了,好吧:)有一种自己挖坑自己埋的感觉,不过埋的过程我们也是可以学到不少的:而且我比 ...
最新文章
- MySQL 数据库的备份和恢复
- Spring RESTful Web服务中的异常处理
- 【渝粤教育】国家开放大学2018年秋季 2302T供应链管理 参考试题
- zabbix内网安装部署_zabbix2.4.8升级3.0.31操作部署记录
- @ModelAttribute使用详解
- cgo 调用dll Failed to load xxxxx.dll: The specified module could not be found.
- Java MyBatis 别名
- 解决安装XAMPP 浏览器输入localhost 出现404错误的问题
- easyui-treegrid的案例
- python高阶函数心得体会_Python高阶函数总结
- 数字电视加密技术工作原理(EMM ECM)
- 如何制作一个简单的APP应用软件?
- (TVS)简介(瞬态抑制二极管)参数
- 如何通过两步验证来保护您的Apple ID
- 加一度解析百度搜索困局,小程序将开启搜索流量新机遇
- Python——类和实例
- random.RandomState()用处
- 新一代VTL实现“新”的数据保护
- ImageJ Nikon_尼康“小世界”竞赛的获胜者揭示了微小的彩色世界|尼康|小世界|nikon|显微镜...
- 防火墙iptables转发规则