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};
@Override
public 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() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
is.setBackgroundResource(res[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public 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{
@Override
public int getCount() {
// TODO Auto-generated method stub
return res.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return res[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public 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 是定义的一个内部类,使用内部类的好处在于有些数据调用十分方便,如果有需要的话,可以留言,我可以把它分离出来。