先给最终效果图:
当我们在最下边的gallery中切换图片时,上面的大图片会自动切换,切换时有动画效果哦,很简单的一个程序,有待完善更多的功能!
activity代码:
package cn.com.chenzheng_java;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
public class GalleryActivity extends Activity implements OnItemSelectedListener {
int[] imagesId = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a3,
R.drawable.a4
};
int index = R.drawable.a1;
ImageSwitcher imageSwitcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 设置窗口无标题栏,一定要在setContentView前进行设置哦
*/
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.gallery);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setOnItemSelectedListener(this);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
imageSwitcher.setVisibility(View.VISIBLE);
/***
* setInAnimation可以设置淡入动画
* setOutAnimation可以设置淡出动画
*/
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
getApplicationContext(), android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
getApplicationContext(), android.R.anim.fade_out));
imageSwitcher.setFactory(new ViewFactory() {
/**
* 创建一个新的图片放置到ImageSwitcher上,可以使用其设置背景哦。
* 一般只会在创建时调用一次。相当于为我们创建一个进行动画效果时的一个背景图。
* 此方法亦可以通过setView替代
*/
@Override
public View makeView() {
ImageView imageView = new ImageView(GalleryActivity.this);
// 设置截取模式
imageView.setScaleType(ScaleType.CENTER_INSIDE);
imageView.setBackgroundResource(R.drawable.ground);
Toast.makeText(getApplicationCont