作者:刘昊昱
博客:http://blog.csdn.net/liuhaoyutz
如果我们要实现类似Windows的照片查看器切换上一张下一张照片的效果,可以使用图片切换器ImageSwitcher,下面我们来看一个例子,其运行效果如下所示:
先来看主布局文件main.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:gravity="center" android:orientation="vertical" > <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="300px" android:layout_height="300px"/>" <LinearLayout android:orientation="horizontal" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="上一张" android:id="@+id/button1" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:text="下一张" android:id="@+id/button2" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
下面来看主Activity文件,其内容如下:
package com.liuhaoyu; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { private int[] imageId = new int[] { R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08}; private int index = 0; private ImageSwitcher imageSwitcher; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); imageSwitcher.setFactory(new ViewFactory() { @Override public View makeView() { ImageView imageView = new ImageView(MainActivity.this); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return imageView; } }); imageSwitcher.setImageResource(imageId[index]); Button previous = (Button) findViewById(R.id.button1); Button next = (Button) findViewById(R.id.button2); previous.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (index > 0) { index--; } else { index = imageId.length - 1; } imageSwitcher.setImageResource(imageId[index]); } }); next.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (index < imageId.length - 1) { index++; } else { index = 0; } imageSwitcher.setImageResource(imageId[index]); } }); } }