Android UI控件详解-ImageSwitcher(图片切换器)

package com.example.imageswitcher;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

/**
 * 
 * @author TXF
 * 
 * ImageSwitcher(图片切换器,无限循环。类似Windows的图片查看器)
 */
public class MainActivity extends Activity implements ViewFactory {
	private ImageSwitcher mis;
	private Integer[] imageIds = { R.drawable.a, R.drawable.b, R.drawable.c,
			R.drawable.d, R.drawable.e };
	private int position = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mis = (ImageSwitcher) findViewById(R.id.is);
		// 设置工厂
		mis.setFactory(this);
		// 设置图片资源 默认显示的是数组中第0张图片
		mis.setImageResource(imageIds[position]);
	}

	// 复写制造图片方法,返回一个新的图片
	@Override
	public View makeView() {
		return new ImageView(this);
	}

	public void click(View view) {
		switch (view.getId()) {
		case R.id.next:
			position++;
			if (position == imageIds.length) {
				position = 0;
			}
			mis.setImageResource(imageIds[position]);
			break;
		case R.id.up:
			position--;
			if (position < 0) {
				position = imageIds.length - 1;

			}
			mis.setImageResource(imageIds[position]);
			break;
		}
	}
}

xml布局



    

    

        
效果图

Android UI控件详解-ImageSwitcher(图片切换器)_第1张图片


你可能感兴趣的:(Android)