GridView and   ImageSwitcher

<ids.xml>

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="grid01"></item>
    <item type="id" name="switcher"></item>
     <item type="id" name="image1"></item>
</resources>

 

<cell.xml>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView 
        android:id="@id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

 

<main.xml>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!-- 定义一个GridView组件 -->
    <GridView
        android:id="@id/grid01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:horizontalSpacing="pt"
        android:verticalSpacing="2pt"
        android:numColumns="4"
        android:gravity="center"
        />
    <!-- 定义一个ImageSwitcher组件 -->
 <ImageSwitcher
     android:id="@id/switcher" 
     android:layout_width="320dp"
        android:layout_height="320dp"
        android:layout_gravity="center_horizontal"
     />

</LinearLayout>

<MainActivitiy.java>

package com.example.gridvieworimageswitcher;

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

public class MainActivity extends Activity {
 private static final String Tag="==CrazyIt.org==";
 int[]imageids=new int[]{
   R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,
   R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h,
   R.drawable.b,R.drawable.g,R.drawable.c,R.drawable.d,
   R.drawable.e,R.drawable.a,R.drawable.g,R.drawable.d
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  //创建一个list集合 List集合的元素q是Map
  List <Map<String ,Object>> listItems=new ArrayList<Map<String , Object>>();
  for(int i=0;i<imageids.length;i++){
   Map<String,Object> listItem=new HashMap<String, Object>();
   listItem.put("image", imageids[i]);
   listItems.add(listItem);
  }
  //获取显示图片的ImageSwitcher
  final ImageSwitcher switcher=(ImageSwitcher) findViewById(R.id.switcher);
  //设置图片更换的动画效果
  switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
  switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
  switcher.setFactory(new ViewFactory(){
   @Override
   public View makeView() {
    ImageView imageView=new ImageView(MainActivity.this);
    imageView.setBackgroundColor(0xff000);
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    return imageView;
   }
  });
  //创建一个SimpleAdapter
  SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItems,R.layout.cell,new String[]{"image"},new int[]{R.id.image1});

  GridView grid=(GridView) findViewById(R.id.grid01);
  grid.setAdapter(simpleAdapter);
  //添加监听
  grid.setOnItemSelectedListener(new OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView<?> parent, View view,
     int position, long id) {
    //显示当前被选中的图片
    switcher.setImageResource(imageids[position %imageids.length]);

   }

   @Override
   public void onNothingSelected(AdapterView<?> parent) {

   }

  });
  //添加列表项被单击的监听器
  grid.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position,
     long id) {
    //显示被单击的图片
    switcher.setImageResource(imageids[position %imageids.length]);
   }

  });
 }

}

 

你可能感兴趣的:(GridView,ImageSwitcher,and, )