最终效果图:
当我们在最下边的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;
- /**
- * @description 对图片的一个小小展示
- * @author chenzheng_java
- * @since 2011/3/17
- *
- */
- 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(getApplicationContext(), "执行了一次",
- Toast.LENGTH_SHORT).show();
- return imageView;
- }
- });
- gallery.setVisibility(View.VISIBLE);
- gallery.setAdapter(new ImageAdapter(this));
- }
- class ImageAdapter extends BaseAdapter {
- private Context context2;
- public ImageAdapter(Context context) {
- context2 = context;
- }
- @Override
- public int getCount() {
- return imagesId.length;
- }
- @Override
- public Object getItem(int position) {
- return imagesId[position];
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View image2, ViewGroup parent) {
- ImageView image = new ImageView(context2);
- image.setBackgroundResource(imagesId[position]);
- /**
- * setScaleType()可以设置当图片大小和容器大小不匹配时的剪辑模式.
- * ScaleType.CENTER_INSIDE的意思是,按图片的原比例缩小或者扩大,直到长或者宽中的任何一个顶到容器为止
- */
- image.setScaleType(ScaleType.CENTER_INSIDE);
- return image;
- }
- }
- /**
- * 当gallery的图像发生变化时,同步ImageSwitcher的变化
- */
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position,
- long id) {
- imageSwitcher.setImageResource(imagesId[position]);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- }
gallery.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:gravity="center_horizontal"
- >
- <ImageSwitcher
- android:id="@+id/imageSwitcher"
- android:layout_height="350dp"
- android:layout_width="fill_parent"
- ></ImageSwitcher>
- <Gallery android:id="@+id/gallery1"
- android:gravity="center"
- android:layout_width="70dp"
- android:layout_height="100dp"></Gallery>
- </LinearLayout>