GridView和ImageSwitcher的功能和用法









main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical"
    android:gravity="center_horizontal">
    
    <!-- 定义一个GridView组件 -->
    <GridView android:id="@+id/grid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:horizontalSpacing="1pt"
        android:verticalSpacing="2pt"
        android:numColumns="4"
        android:gravity="center"></GridView>


    <!-- 定义一个ImageSwitcher组件 -->
    <ImageSwitcher android:id="@+id/switcher"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"></ImageSwitcher>
    
</LinearLayout>


Main.java

package com.example.imageswitchertest;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView.LayoutParams;
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 {


//制定图片数组
int[] imageIds=new int[]{
R.drawable.aermeiliyan,R.drawable.asenna,R.drawable.gebenhagen,
R.drawable.hengda,R.drawable.kaierteren,R.drawable.liwonuo,R.drawable.mancheng,
R.drawable.manlian
};

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//创建一个List集合
List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();

for(int i=0;i<imageIds.length;i++){
Map<String,Object> oneListItem=new HashMap<String,Object>();
oneListItem.put("image", imageIds[i]);
listItems.add(oneListItem);
}

//获取显示图片的ImageSwitcher
final ImageSwitcher switcher=(ImageSwitcher) super.findViewById(R.id.switcher);

//设置图片更换时候的效果,设置渐隐渐现
switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
switcher.setOutAnimation(this, android.R.anim.fade_out);

/**
* ImageSwitcher 类必须设置一个ViewFactory,主要用来将显示的图片和父窗口区分开来
* 因此需要实现ViewSwitcher.ViewFactory接口,通过makeView()方法来显示图片
* 这里会返回一个ImageView 对象,而方法 setImageResource用来指定图片资源
*/

switcher.setFactory(new ViewFactory(){


public View makeView() {
ImageView imageView=new ImageView(MainActivity.this);
imageView.setBackgroundColor(0xff0000);
//ImageView.ScaleType把图片暗比例扩大/缩小到View的宽度,居中显示
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(120,120));
return imageView;
}


});


String[] from=new String[]{"image"};
int[] to=new int[]{R.id.imageView};

//创建一个SimpleAdapter
SimpleAdapter simpleAdapter=new SimpleAdapter(
this, 
listItems, 
//使用/res/layout/cell.xml文件作为界面布局
R.layout.cell, 
from, 
to);
GridView grid=(GridView) super.findViewById(R.id.grid);
//为grid设置Adapter
grid.setAdapter(simpleAdapter);

final Builder builder=new AlertDialog.Builder(this);


//添加列表项被选中的监听器
grid.setOnItemSelectedListener(new OnItemSelectedListener(){


/**
* parent 
* The AdapterView where the selection happened 
* view 
* The view within the AdapterView that was clicked 
* position 
* The position of the view in the adapter 
* id
* The row id of the item that is selected  
*/

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

}


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]);
}
});

    }
}


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/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="5dp"/>
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值