带预览图片的浏览器

  使用ImageSwitcher时往往需要为它设置一个ImageSwitcher.ViewFactory,实现ImageSwitcher.ViewFactory时需要实现一个makeView()方法,该方法通常会返回一个ImageView,而ImageSwitcher则负责显示这个ImageView。

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="center_horizontal"
    android:orientation="vertical" >

    <!--
     定义一个GridView组件
        android:horizontalSpacing="1pt"——设置各元素的水平间距
        android:verticalSpacing="2pt"——设置各元素的垂直间距
        android:numColumns="4"——设置列数
    -->

    <GridView
        android:id="@+id/grid01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:horizontalSpacing="1pt"
        android:numColumns="4"
        android:verticalSpacing="2pt" />
    <!-- 定义一个ImageSwitcher组件 -->

    <ImageSwitcher
        android:id="@+id/switcher"
        android:layout_width="320dp"
        android:layout_height="320dp"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

cell.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="center_horizontal"
    android:orientation="horizontal"
    android:padding="5pt" >

    <ImageView
        android:id="@+id/image1"
        android:layout_width="50dp"
        android:layout_height="50dp" />

</LinearLayout>

 

package org.crazyit.gridview;

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.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery.LayoutParams;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.ViewSwitcher.ViewFactory;

public class GridViewTest extends Activity {

    int[] imagesList = new int[] { R.drawable.bomb5, R.drawable.bomb6, R.drawable.bomb7, R.drawable.bomb8,
            R.drawable.bomb9, R.drawable.bomb10, R.drawable.bomb11, R.drawable.bomb12, R.drawable.bomb13,
            R.drawable.bomb14, R.drawable.bomb15, R.drawable.bomb16 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // 数据源
        // 创建一个List对象,List对象的元素是Map
        // List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
        // 是定义一个List类型的变量,list里面存放的是一个Map,
        // 而Map的key是一个String类型,Map的value是Object类型
        List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imagesList.length; i++) {
            Map<String, Object> item = new HashMap<String, Object>();
            item.put("image", imagesList[i]);
            lists.add(item);
        }
        GridView gridView = (GridView) findViewById(R.id.grid01);
        // 创建一个SimpleAdapter,并使用/layout/cell.xml文件作为界面布局
        SimpleAdapter sAdapter = new SimpleAdapter(this, lists, R.layout.cell, new String[] { "image" },
                new int[] { R.id.image1 });
        // 为GridView设置Adapter
        gridView.setAdapter(sAdapter);
        // _______________在GridView已加载图片_________________________

        // 获取显示图片的ImageSwitcher
        final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        // 设置图片更换的动画效果
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        // 为ImageSwitcher设置图片切换的动画效果
        imageSwitcher.setFactory(new ViewFactory() {
            @Override
            public View makeView() {
                // TODO Auto-generated method stub
                ImageView imageView = new ImageView(GridViewTest.this);
                imageView.setBackgroundColor(0xff0000);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setLayoutParams(
                        new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                return imageView;
            }
        });

        // __________________________ImageSwitcher相关的设置_________________________________________

        // 添加列表项被选中的监听器
        gridView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // 显示当前被选中的图片
                imageSwitcher.setImageResource(imagesList[position % imagesList.length]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        // 添加列表项被单击的监听器
        gridView.setOnItemClickListener(new OnItemClickListener() {
            // 显示被单击的图片的图片
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                imageSwitcher.setImageResource(imagesList[position % imagesList.length]);
            }
        });

        // __________________________监听事件_________________________________________

    }
}

posted @ 2016-04-12 11:29  沉默的羊癫疯  阅读(213)  评论(0编辑  收藏  举报