Gallery组件用于拖拽浏览图片,以下我们就来看一下怎样实现。

一、实现Gallery

1.布局文件非常easy:

<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MyLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" > <Gallery
android:id="@+id/myGallery"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout>

2.自己定义适配器类,能够直接覆写BaseAdapter类中的几个方法。

package org.yayun.demo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams; public class ImageGalleryAdapter extends BaseAdapter {
private Context context;
private int imgRes[] = new int[] { R.drawable.ispic_a, R.drawable.ispic_b,
R.drawable.ispic_c, R.drawable.ispic_d, R.drawable.ispic_e, }; public ImageGalleryAdapter(Context c) {//构造方法,用于获取上下文对象
this.context = c;
} public int getCount() { return imgRes.length;
} public Object getItem(int position) {
return imgRes[position];
} public long getItemId(int position) {
return imgRes[position];
} public View getView(int position, View convertView, ViewGroup parent) {
ImageView img = new ImageView(this.context);
img.setBackgroundColor(0xFFFFFFFF);
img.setImageResource(this.imgRes[position]);//设置资源
img.setScaleType(ImageView.ScaleType.CENTER);//居中显示
img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return img;
} }

3.MainActivity.java:

package org.yayun.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery; public class MainActivity extends Activity {
private Gallery gallery; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.main); // 设置要使用的布局管理器
gallery = (Gallery) findViewById(R.id.myGallery);
gallery.setAdapter(new ImageGalleryAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this,
"您选择了第" + String.valueOf(position + 1) + "张图片",
Toast.LENGTH_SHORT).show(); }
});
}
}

4.执行实比例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWF5dW4wNTE2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

二、Gallery和ImageSwitcher结合

这时的Gallery我们用SimpleAdapter类完毕。

1.布局文件:

<?

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MyLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="bottom"
android:orientation="vertical" > <ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ImageSwitcher> <Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:spacing="5dp" /> </LinearLayout>

2.定义显示模板:

<?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:background="#FFFFFF"
android:orientation="horizontal" > <ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center" /> </LinearLayout>

3.MainActivity.java程序:

package org.yayun.demo;

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.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery.LayoutParams;
import android.widget.Gallery;
import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity {
private Gallery gallery;
private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
private SimpleAdapter simpleAdapter;
private ImageSwitcher imageSwitcher; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.main); // 设置要使用的布局管理器
initAdapter();
gallery = (Gallery) findViewById(R.id.gallery);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
imageSwitcher.setFactory(new ViewFactory() { public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setBackgroundColor(0xFFFFFFFF);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return imageView;
}
});
gallery.setAdapter(simpleAdapter);
gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Map<String, Integer> map = (Map<String, Integer>) MainActivity.this.simpleAdapter
.getItem(position);// 取出map
MainActivity.this.imageSwitcher.setImageResource(map.get("img"));// 设置显示图片 }
});
} private void initAdapter() {
Field[] fields = R.drawable.class.getDeclaredFields();// Java反射机制获取全部资源图片
for (int i = 0; i < fields.length; i++) {
if (fields[i].getName().startsWith("ispic_")) {// 推断开头
Map<String, Integer> map = new HashMap<String, Integer>();
try {
map.put("img", fields[i].getInt(R.drawable.class));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.list.add(map);
}
}
simpleAdapter = new SimpleAdapter(this, this.list,
R.layout.grid_layout, new String[] { "img" },
new int[] { R.id.img }); }
}

4.执行实例:

Android实战简易教程-第十枪(画廊组件Gallery有用研究)的更多相关文章

  1. Android实战简易教程-第二十八枪(Uri转String型实例)

    接上一篇文章.我们能够轻易的获取所选图片的uri,那么我们考虑怎样将获取的uri转换成String型的地址呢? 接下来我们通过实例来研究.布局文件和上篇(二十七枪)一致,我们就不再列出,直接看Main ...

  2. Android实战简易教程-第二十五枪(基于Baas的数据表查询下拉刷新和上拉载入实现!)

    上一节我们实现了数据表的载入,可是,当数据表数据非常多时.我们就要考虑数据的分页.这里我们选用了PullToRefreshListView控件,先看一下该控件的说明: 效果图:            ...

  3. Android实战简易教程-第二十八枪(基于Bmob实现头像图片设置和网络上传功能!)

    上一篇我们介绍了怎样由uri转换成String ,本文就用到了上篇文章的方法.以下我们介绍一下怎样设置头像后将头像图片上传到云端的方法,本文基于Bmob提供的服务. 看一下代码:(布局文件和前两篇文章 ...

  4. Android实战简易教程-第十五枪(实现ListView中Button点击事件监听)

    1.main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  5. Android实战简易教程-第二十六枪(基于ViewPager实现微信页面切换效果)

    1.头部布局文件top.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  6. Android实战简易教程-第十三枪(五大布局研究)

    我们知道Android系统应用程序通常是由多个Activity组成,而这些Activity以视图的形式展如今我们面前, 视图都是由一个一个的组件构成的. 组件就是我们常见的Button.TextEdi ...

  7. Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)

    接上一篇,加入验证用户名是否已经注冊功能! 仅仅须要改动MainActivity.java: package com.example.logintest; import java.util.List; ...

  8. Android实战简易教程-第二十三枪(基于Baas的用户注冊和登录模块实现!)

    接着上两篇文章.我们基于Bmob提供的API实现用户登录功能.总体看一下代码. 1.注冊页面xml: <RelativeLayout xmlns:android="http://sch ...

  9. Android实战简易教程-第二十四枪(基于Baas的用户表查询功能实现!)

    接着上一篇,我们注冊了几个用户,用户表例如以下: 以下我们用ListView将表中数据显示出来吧. 首先看一下main.xml: <RelativeLayout xmlns:android=&q ...

随机推荐

  1. [Leetcode Week6]Linked List Cycle II

    Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...

  2. python--fnmatch

    import fnmatch ''' 这个库专门是用来做文件名匹配的,可以使用通配符如下 * 匹配任何数量的任意字符 ? 匹配单个数量的任意字符 [seq] 匹配seq中的任意字符 [!seq] 匹配 ...

  3. [ Python - 3 ] python3.5中不同的读写模式

    r 只能读.r+可读可写,不会创建不存在的文件.如果直接写文件,则从顶部开始写,覆盖之前此位置的内容,如果先读后写,则会在文件最后追加内容.w+ 可读可写 如果文件存在 则覆盖整个文件不存在则创建w ...

  4. css左右箭头

    .record-left{ content: ""; width: 0; height: 0; float: left; border-top: 10px solid transp ...

  5. 《Java编程思想》笔记 第三章 操作符

    1.操作符种类: 运算顺序1-7 一元操作符(单目操作符)  - 负号, + 正号,--递减,++递增 算术操作符 + - *  /  % 移位操作符  <<左移(低位补0),>&g ...

  6. k8s的网络学习

    1.Kubernetes 网络模型 Kubernetes 采用的是基于扁平地址空间的网络模型,集群中的每个 Pod 都有自己的 IP 地址,Pod 之间不需要配置 NAT 就能直接通信.另外,同一个 ...

  7. EasyUI中combobox的代码实例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. CF 1006C Three Parts of the Array【双指针/前缀和/后缀和/二分】

    You are given an array d1,d2,-,dn consisting of n integer numbers. Your task is to split this array ...

  9. linux中MySQL本地可以连接,远程连接不上问题

    1.网络或防火墙问题 (1)检查网络直接ping你的远程服务器,ping 182.61.22.107,可以ping通说明网络没问题 (2)看端口号3306是不是被防火墙挡住了,telnet 182.6 ...

  10. hdu2825(AC 自动机)

    hdu2825 题意 给出一些字符串,要求构造一个长度为 \(n\) 的字符串至少包括其中的 \(k\) 个,问有多少种字符串满足条件. 分析 AC自动机 构造状态转移,然后 状态压缩DP 即可. \ ...