配置文件

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="bottom">
    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <Gallery
        android:id="@+id/gallery"
        android:gravity="center_horizontal"
        android:spacing="3px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

img_data.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <ImageView 
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="center"/>
</LinearLayout>

程序文件

MainActivity.java

package com.example.simpleadaptergalleryproject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {
private Gallery gallery=null;
private List<Map<String,Integer>> list=new ArrayList<Map<String,Integer>>();
private SimpleAdapter simpleAdapter=null;
private ImageSwitcher imageSwitcher=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.gallery=(Gallery)super.findViewById(R.id.gallery);//取得资源ID
this.imageSwitcher=(ImageSwitcher)super.findViewById(R.id.imageSwitcher);//取得资源ID
this.imageSwitcher.setFactory(new ViewFactorylmpl());//设置转换工厂
this.initAdapter();//初始化适配器
this.gallery.setAdapter(this.simpleAdapter);   //设置图片集
this.gallery.setOnItemClickListener(new OnItemClickListenerlmpl());//设置事件
}

private class ViewFactorylmpl implements ViewFactory{
@Override
public View makeView() {
ImageView image=new ImageView(MainActivity.this);
image.setBackgroundColor(0xFFFFFFFF);
image.setScaleType(ImageView.ScaleType.CENTER);
image.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return image;
}
}

private class OnItemClickListenerlmpl implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//Toast.makeText(MainActivity.this,"一直都很帅,从未被超越",Toast.LENGTH_LONG).show();
Map<String,Integer> map=(Map<String,Integer>)MainActivity.this.simpleAdapter.getItem(arg2);
MainActivity.this.imageSwitcher.setImageResource(map.get("img"));
}
}

private void initAdapter(){//初始化适配器
Field[] field=R.drawable.class.getDeclaredFields();//取得全部属性
for (int i = 0; i < field.length; i++) {
if(field[i].getName().startsWith("ispic_")){   //找到所有以ispic_命名的图片
Map<String,Integer> map=new HashMap<String,Integer>();   
try{
map.put("img", field[i].getInt(R.drawable.class));  //设置图片资源
}catch(Exception e){
}
this.list.add(map);   //保存图片资源
}
}
this.simpleAdapter=new SimpleAdapter(MainActivity.this,
MainActivity.this.list,//要包装的数据集合
R.layout.img_data, //要使用的资源模板
new String[]{"img"},//要显示map中的Key
new int[]{R.id.img});//与模板中的组建向匹配
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

android手机中图片的拖拉及浏览功能的更多相关文章

  1. android开发中图片优化步骤

    android开发中图片优化方法 1.图片加载方法,方便用户加载图片 /*** * 加载本地图片 * @param context:主运行函数实例 * @param bitAdress:图片地址,一般 ...

  2. 将文件从已Root Android手机中copy出来的几个cmd窗口命令

    将文件从已Root Android手机中copy出来的几个cmd窗口命令: 以shell身份登录adbadb shell进入adb后切换至root用户su更改文件的所属chown shell *更改文 ...

  3. 从Android手机中取出已安装的app包,导出apk

    从Android手机中取出已安装的app包,导出apk TAG:Android,提取,apk,adb,pm,root,导出apk 假设有这样一个场景,A君看到你手机上一个实用APP,想要安装到自己手机 ...

  4. 命令行从Android手机中导出已安装APK的方法调研

    一.背景 二.步骤 一.背景 很多时候,APK文件只存在于应用市场,在PC上无法直接下载.用手机下载下来后就直接安装了,也不能保存原始的APK文件. APK安装到手机后,Android系统会保存一份和 ...

  5. 关于Android应用中图片占用内存浅谈

    从事过移动端应用开发的童鞋应该都清楚,内存是非常宝贵的资源.如果能很好的利用有限的内存,对应用性能的提升会有很大的帮助.在实际应用开发中图片内存占整个应用非常大的比重,我们只有了解图片是如何加载到内存 ...

  6. 用FileExplorer查看android手机中的数据库

    想查看一下手机中的通讯录数据库,google之后找到了办法. 参考: http://stackoverflow.com/questions/4867379/android-eclipse-ddms-c ...

  7. Android手机中UID、PID作用及区别

    PID 指进程ID. PID是进程的身份标识,程序一旦运行,就会给应用分配一个独一无二的PID(ps:一个应用可能包含多个进程,每个进程有唯一的一个PID) 进程终止后PID会被系统收回,再次打开应用 ...

  8. ionic ng-src 在网页显示,但是导出apk在android手机中运行不显示图片

    解决方法参照: http://stackoverflow.com/questions/29896158/load-image-using-ng-src-in-android-ionic-aplicat ...

  9. Android手机中获取手机号码和运营商信息

    代码如下: package com.pei.activity; import android.app.Activity; import android.os.Bundle; import androi ...

随机推荐

  1. Netty笔记

    1 基本介绍 Bootstrap Netty应用程序通过设置 bootstrap(引导)类开始,该类提供了一个用于应用程序网络层配置的容器.Bootstrap有两种类型,一种是用于客户端的Bootst ...

  2. Java学习笔记(二):String

    String 在Java中String是作为引用对象存在的一种数据类型,用来保存字符串. 实例化和赋值 //直接声明 String s1 = "Hello world!"; //通 ...

  3. MSSQL索引优化

    转自:http://blog.itpub.net/16436858/viewspace-589275/ http://www.cnblogs.com/jams742003/archive/2011/1 ...

  4. mysql CASE WHEN的基础和多种用法

    CASE计算条件列表并返回多个可能结果表达式之一. CASE 具有两种格式: 简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果. CASE 搜索函数计算一组布尔表达式以确定结果. 两 ...

  5. TChromeTabs 使用日记

    1.如何让 Tab 在拖放时,拖放图形中带有 TabControl 的内容. 增加 ChromeTabs 的 NeedDragImageControl 事件,并在代码中设置 DragControl 为 ...

  6. JQuery Basic Features Quick Walkthrough

    1. Basic Selectors $('p')—Accesses all the paragraph elements in the HTML file $('div')—Accesses all ...

  7. Codeforces gym 100685 E. Epic Fail of a Genie 贪心

    E. Epic Fail of a GenieTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685 ...

  8. 从零开始学习Hadoop--前言

    Hadoop是最著名使用最广泛的分布式大数据处理框架,它是用Java开发的. 这本书有一个明确的目标:只要有一台能上网的计算机,就可以让读者在最短的时间内,学会Hadoop的初级开发.所以,这本书只讲 ...

  9. GLSL 基础量定义

    GLSL语法跟C语言非常相似: 1.数据类型: GLSL包含下面几种简单的数据类型 float bool :false or ture int 向量: vec   {2,3,4}     长度为2, ...

  10. 判断IE中iframe完美加载完毕的方法

    转: var iframe = document.createElement("iframe"); iframe.src = "http://www.planabc.ne ...