Android初级之组件基础11-ImageSwitcher

ImageSwitcher,类似于TextSwitcher,只不过对应的View是ImageView。通过ImageSwitcher,我们可以切换显示的图片,做出简单的幻灯片效果。我们经常将ImageSwitcher与Gallery结合使用,做出相册的效果

1.常用属性与方法

2.实例

布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  	xmlns:android="http://schemas.android.com/apk/res/android"
  	android:orientation="vertical"
  	android:layout_width="fill_parent"
  	android:layout_height="fill_parent">


	<ImageSwitcher android:id="@id/isBase"
		android:layout_width="fill_parent" android:layout_height="wrap_content"/>
	<Button android:id="@id/btnBaseImageSwitcher" 
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:text="点击变幻图片" android:hint="以动画形式变换图片"/>	
</LinearLayout>

代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
import cn.youtous.R;


public class BaseImageSwitcher extends Activity {
	private ImageSwitcher isBase;
	private Button btnBaseImageSwitcher;
	private int mIndex = 0;
	
	private int[] mImages = {
			R.drawable.carlogo_52design_01, R.drawable.carlogo_52design_06, R.drawable.carlogo_52design_09,
			R.drawable.carlogo_52design_12, R.drawable.carlogo_52design_19, R.drawable.carlogo_52design_21,
			R.drawable.carlogo_52design_27, R.drawable.carlogo_52design_29, R.drawable.carlogo_52design_31
	};


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.base_imageswitcher);
		isBase = (ImageSwitcher)findViewById(R.id.isBase);
		isBase.setFactory(new IsBaseFactory());
		isBase.setImageResource(mImages[mIndex]);
		isBase.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left));
		isBase.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right));
		
		btnBaseImageSwitcher = (Button)findViewById(R.id.btnBaseImageSwitcher);
		btnBaseImageSwitcher.setOnClickListener(new IsBaseClickListener());
	}
	
	class IsBaseFactory implements ViewFactory {
		@Override
		public View makeView() {			
			ImageView iv = new ImageView(BaseImageSwitcher.this);
			//...可在此处对ImageView进行样式定义
			return iv;
		}		
	}


	class IsBaseClickListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			if(mIndex<mImages.length-1){
				mIndex++;
			}else{
				mIndex = 0;
			}
			//设置ImageSwitcher显示的图片
			isBase.setImageResource(mImages[mIndex]);				
		}		
	}
}


阅读更多
个人分类: Android初级
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭