当用户选中一张图片之后,可以直接将其显示在正中央。

要实现显示图片的切换,那么就要依靠ImageSwitchar类完成。

在main.xml中

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

<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/myImageSwitcher"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

<Gallery

android: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">

<ImageView

android:id="@+id/img"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:scaleType="center"/>

</LinearLayout>

在MyGalleryDemo.java程序中

package org.lxh.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.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 MyGalleryDemo extends Activity {

private Gallery gallery = null;

private SimpleAdapter simpleAdapter = null ;

private List<Map<String,Integer>> list = new ArrayList<Map<String,Integer>>() ;

private ImageSwitcher myImageSwitcher = null ;

@Override

public 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.gallery.setAdapter(this.simpleAdapter) ;

this.myImageSwitcher.setFactory(new ViewFactoryImpl()) ;

this.gallery.setOnItemClickListener(new OnItemClickListenerImpl()) ;

}

private void initAdapter(){

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("img", fields[x].getInt(R.drawable.class)) ; // 必须定义好名称是img

} catch(Exception e){

}

this.list.add(map) ;

}

}

this.simpleAdapter = new SimpleAdapter(this, this.list,

R.layout.grid_layout, new String[] { "img" },

new int[] { R.id.img });

}

private class OnItemClickListenerImpl implements OnItemClickListener {

public void onItemClick(AdapterView<?> parent, View view, int position,

long id) {

Map<String, Integer> map = (Map<String, Integer>) parent

.getAdapter().getItem(position);

MyGalleryDemo.this.myImageSwitcher.setImageResource(map.get("img")) ;

}

}

private class ViewFactoryImpl implements ViewFactory{

public 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;

}

}

}

使用SimpleAdapter一定需要一个布局文件。



更多相关文章

  1. Android:WebView加载url网页显示不完整解决办法
  2. Android中Gallery 点击图片放大
  3. Android大分辨率适应
  4. Android(安卓)7.1.2(Android(安卓)N) Multi-window-mode--多窗口
  5. android PopupWindow模拟Windows开始菜单显示消失效果
  6. Android仿微信图片选择器(二)
  7. Android锁屏(四)
  8. android listview(每个item包含图片及文字)
  9. Android(安卓)MTK平台修改开关机动画和开机logo【转】

随机推荐

  1. 自定手机App消息开发说明
  2. Android(安卓)2.3离线下载安装
  3. 如何用同一份代码产生不同包名得APK包
  4. JNI综合实验一:LED点亮+IO电平读取
  5. Android(安卓)创建AlertDislog时要小心哦
  6. listview的item中含有checkbox的一些问题
  7. arcgis for android访问arcgis server上
  8. android aidl 传递parcelable
  9. ActionBar的简单使用(修改背景颜色,文字居
  10. 手机cpu厂商和手机cpu架构