本文主要是介绍ImageSwitcher小例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
效果图如下,点击就会出现下一张:
MainActivity.java中的代码如下:
package com.bzu.imageswitcher.activity;import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity {// 设置当前图片的索引private int index;// 定义图片数组private int[] images = new int[] { R.drawable.image1, R.drawable.image2,R.drawable.image3, R.drawable.image4, R.drawable.image5,R.drawable.image6 };@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 得到ImageSwitcher对象final ImageSwitcher switcher = (ImageSwitcher) this.findViewById(R.id.image);// 实现并设置工厂内部接口的makeView方法,用来显示视图switcher.setFactory(new ViewFactory() {@Overridepublic View makeView() {return new ImageView(MainActivity.this);}});// 设置图片来源switcher.setImageResource(images[index]);// 设置点击事件switcher.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {index++;if (index >= images.length) {index = 0;}switcher.setImageResource(images[index]);}});// 设置切入动画switcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left));// 设置切出动画switcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right));}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}
activity_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:orientation="vertical"><ImageSwitcher android:id="@+id/image"android:layout_width="wrap_content"android:layout_height="wrap_content"></ImageSwitcher></LinearLayout>
这篇关于ImageSwitcher小例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!