Android开发中画廊视图Gallery的两种使用方法分析

所属分类: 软件编程 / Android 阅读数: 29
收藏 0 赞 0 分享

本文实例讲述了Android开发中画廊视图Gallery的两种使用方法。分享给大家供大家参考,具体如下:

第一种方法:

第一步:设计xml布局文件

代码如下:main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <Gallery
    android:id="@+id/myGallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:spacing="3px"
    android:text="@string/hello" />
</LinearLayout>

第二步:自定义一个适配器,这个适配器继承BaseAdapter这个类

代码如下:

package net.loonggg.gallery;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class ImageGalleryAdapter extends BaseAdapter {
private Context context;
// 里面所有的方法表示的是可以根据指定的显示图片的数量,进行每个图片的处理
private int[] image = 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 context) {
this.context = context;
}
public int getCount() { // 取得要显示内容的数量
return image.length;
}
public Object getItem(int position) { // 每个资源的位置
return image[position];
}
public long getItemId(int position) { // 取得每个项的ID
return image[position];
}
// 将资源设置到一个组件之中,很明显这个组件是ImageView
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(context);
iv.setBackgroundColor(0xFFFFFFFF);
iv.setImageResource(image[position]);// 给ImageView设置资源
iv.setScaleType(ImageView.ScaleType.CENTER);// 设置对齐方式
iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return iv;
}
}

第三步:主方法:

package net.loonggg.gallery;
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.Toast;
public class GalleryActivity extends Activity {
private Gallery myGallery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myGallery = (Gallery) findViewById(R.id.myGallery);
myGallery.setAdapter(new ImageGalleryAdapter(this));
myGallery.setOnItemClickListener(new OnItemClickListenerImpl());
}
private class OnItemClickListenerImpl implements OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(GalleryActivity.this, String.valueOf(position),
Toast.LENGTH_SHORT).show();
}
}
}

第二种方法:

第一步:设计xml布局文件

代码如下:main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="bottom"
  android:orientation="vertical" >
  <ImageSwitcher
    android:id="@+id/is"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
  </ImageSwitcher>
  <Gallery
    android:id="@+id/myGallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:spacing="3px" />
</LinearLayout>

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

第二步:MainActivity

代码如下:

package net.loonggg.gallery2;
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.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.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
private ImageSwitcher is;
private Gallery gallery;
private SimpleAdapter adapter;
private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
is = (ImageSwitcher) findViewById(R.id.is);
is.setFactory(new ViewFactoryImpl());
initAdapter();
gallery = (Gallery) findViewById(R.id.myGallery);
gallery.setAdapter(adapter); // 为gallery设置合适的适配器
gallery.setOnItemClickListener(new OnItemClickListenerImpl());
}
public class OnItemClickListenerImpl implements OnItemClickListener { // gallery的点击事件
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Map<String, Integer> map = (Map<String, Integer>) parent
.getAdapter().getItem(position);
is.setImageResource(map.get("image"));
}
}
public void initAdapter() { // 这个方法的功能是:从R.java文件中获取图片资源的id,如果资源图片数量比较多,用数组的方法一一定义,就不太合适,这种方法最好了。
Field[] fields = R.drawable.class.getDeclaredFields();
for (int x = 0; x < fields.length; x++) {
if (fields[x].getName().startsWith("ispic_")) { // 根据图片的名称取出想要的图片
Map<String, Integer> map = new HashMap<String, Integer>();
try {
map.put("image", fields[x].getInt(R.drawable.class));
} catch (Exception e) {
e.printStackTrace();
}
list.add(map);
}
}
adapter = new SimpleAdapter(MainActivity.this, list,
R.layout.grid_item, new String[] { "image" },
new int[] { R.id.iv });
}
public class ViewFactoryImpl implements ViewFactory {
@Override
public View makeView() {
ImageView iv = new ImageView(MainActivity.this);
iv.setBackgroundColor(0xFFFFFFFF);
iv.setScaleType(ImageView.ScaleType.CENTER);
iv.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
return iv;
}
}
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多