ImageSwitcher 和 Gallery 的使用

来源:互联网 发布:淘宝一择数码这个店铺 编辑:程序博客网 时间:2024/04/28 08:04

ImageSwitcher 是一个用于图片浏览的控件,使用这个空间有一个好处就是可以设置图片的进入和退出的动画效果总体来说看着还是很不错的。而gallery即画廊是平时开发中常用到的图片浏览空间,其默认是第一张图在中间显示,若需要其左对齐 则需要对该空间进行重写,网上有很多这样的代码。现在直接上代码,注释都有的。


package com.igallerybate.activity;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;public class IGallleryBateActivity extends Activity implements ViewFactory {private Gallery gl = null;private ImageSwitcher  is = null;private int[] res = new int[]{R.drawable.t1,R.drawable.t2,R.drawable.t3,R.drawable.t4,R.drawable.t5,R.drawable.t6,R.drawable.t7};@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);is = (ImageSwitcher) this.findViewById(R.id.is_image);is.setFactory(this);//需要实现 makeView()方法is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//ImageSwitcher退出动画is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//ImageSwitcher进入动画。。这两个动画可以自己定义的gl = (Gallery) this.findViewById(R.id.gl);gl.setAdapter(new GalleryAdapter());gl.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {is.setBackgroundResource(res[arg2]);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});}@Overridepublic View makeView() {//该方法有用来实现ViewFactory 方法。ImageView iv = new  ImageView(getApplicationContext());//注意用的是ImageSwitcher.LayoutParams  iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));return iv;}    class GalleryAdapter extends BaseAdapter{@Overridepublic int getCount() {// TODO Auto-generated method stubreturn res.length;}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn res[arg0];}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn arg0;}@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {ImageView iv = new  ImageView(getApplicationContext());iv.setLayoutParams(new Gallery.LayoutParams(120,120));//设置Imageview控件的的大小iv.setBackgroundResource(res[arg0]);//设置图片显示iv.setAlpha(150);//设置imageview控件半透明的return iv;}        }}


布局文件


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ImageSwitcher         android:id="@+id/is_image"        android:layout_width="fill_parent"        android:layout_height="fill_parent"/>        <RelativeLayout         android:layout_width="fill_parent"        android:layout_height="100dip"        android:layout_alignParentBottom="true"        >        <Gallery             android:id="@+id/gl"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:spacing="5dip"            />    </RelativeLayout></RelativeLayout>


实现效果




这里说明一下,为什么程序用的程序使用的是adpater 是定义的一个内部类,使用内部类的好处在于有些数据调用十分方便,如果有需要的话,可以留言,我可以把它分离出来。

源码地址:http://download.csdn.net/detail/luinsist/4790672

原创粉丝点击