实例效果
main.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.example.gallerytest"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 定义一个ImageSwitcher组件 -->
<ImageSwitcher android:id="@+id/switcher"
android:layout_width="320dp"
android:layout_height="320dp"/>
<!-- 定义一个Gallery组件 -->
<Gallery android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:spacing="3pt"
/>
</LinearLayout>
main.java
package com.example.gallerytest;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.TypedArray;
import android.view.Menu;
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 MainActivity extends Activity {
int[] imageIds=new int[]{
R.drawable.gongniu,
R.drawable.guowang,
R.drawable.huojian,
R.drawable.huren,
R.drawable.jueshi
};
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Gallery gallery=(Gallery) super.findViewById(R.id.gallery);
final ImageSwitcher switcher=(ImageSwitcher) super.findViewById(R.id.switcher);
//为ImageSwitcher对象设置ViewFactory对象
switcher.setFactory(new ViewFactory(){
@Override
public View makeView() {
ImageView imageView=new ImageView(MainActivity.this);
imageView.setBackgroundColor(0xff0000);
//把图片扩大或者缩小到view的宽度或者高度,居中显示
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
return imageView;
}
});
//设置图片更换的动画效果
switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
//创建一个BaseAdapter对象,该对象负责提供Gallery所显示的每张图片
BaseAdapter adapter=new BaseAdapter() {
public View getView(int position, View convertView, ViewGroup parent) {
//创建一个ImageView
ImageView imageView=new ImageView(MainActivity.this);
imageView.setImageResource(imageIds[position]);
//设置ImageView的缩放类型,不按比例缩放图片,填满整个view
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(75, 100));
return imageView;
}
@Override
public long getItemId(int position) {
return position;
}
public Object getItem(int position) {
return position;
}
public int getCount() {
return imageIds.length;
}
};
gallery.setAdapter(adapter);
gallery.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
switcher.setImageResource(imageIds[position]);
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
}