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"
android:orientation="vertical"
tools:context="com.example.android.gallery.MainActivity" > <Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageSwitcher
android:id="@+id/is"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
自定义适配器:
package com.example.android.gallery; import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType; public class MyAdapter extends BaseAdapter {
private int[] res;
private Context context; public MyAdapter(int[] res, Context context) {
this.res = res;
this.context = context;
} @Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;//设置数量为整型的最大值,使图片循环播放
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return res[position];
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(context);
image.setBackgroundResource(res[position%res.length]);//设置背景图片无限循环
image.setLayoutParams(new Gallery.LayoutParams(400, 300));//设置缩略图的大小
image.setScaleType(ScaleType.FIT_XY);//设置按比例缩放
return image;
} }
MainActivity类:
package com.example.android.gallery; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
/**
* ImageSwitcher:和ImageView的功能类似,都适用与显示图片,
* 区别:ImageSwitcher的效果更炫,它可以指定图片切换的动画效果
*
* ImageSwticher可以粗略理解为ImageView的选择器,需要设置ViewFactory,
* 一般来说,会把ViewFactory的makeView()方法,返回ImageView。
* @author Administrator
*
*/
public class MainActivity extends Activity implements ViewFactory,OnItemSelectedListener{
//准备数据源
private int[] res = {R.drawable.item1,R.drawable.item2,R.drawable.item3,R.drawable.item4,
R.drawable.item5,R.drawable.item6,R.drawable.item7,R.drawable.item8,
R.drawable.item9,R.drawable.item10,R.drawable.item11,R.drawable.item12};
private Gallery gallery;
private ImageSwitcher is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (Gallery) findViewById(R.id.gallery);
//自定义适配器
MyAdapter adapter = new MyAdapter(res,this);
//为gallery设置适配器
gallery.setAdapter(adapter);
//为gallery设置监听器,用来监听Gallery选中的图片
gallery.setOnItemSelectedListener(this); is = (ImageSwitcher) findViewById(R.id.is);
is.setFactory(this);//为imageSwitcher设置视图工厂
//为imageSwitcher设置动画效果
is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
}
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView image = new ImageView(this);
image.setScaleType(ScaleType.FIT_CENTER);//使图片按比例缩放并居中
return image;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
is.setBackgroundResource(res[position%res.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub }
}