自定义View实现换灯片

前面两篇文章都是关于幻灯片是怎么实现的,但是效果都不是非常理想,结合前两次的知识我自定义了一个View把功能封装了一下更方便代码的使用与修改.代码如下:

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class SlideView extends RelativeLayout {
	private int[] urls;
	private ImageView imageTop;
	private ImageView imageDown;
	private int index;
	private boolean isFrist = true;
	private Context context;
	public SlideView(Context context) {
		super(context);
	}

	public SlideView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	public void setData(int[] urls, int index) {
		this.urls = urls;
		this.index = index;
		setView();
	}

	private void setView() {
		imageTop = new ImageView(context);
		imageDown = new ImageView(context);
		addView(imageDown);
		addView(imageTop);
	}
	//开始幻灯片
	public void nextAnimation() {
		imageTop.clearAnimation();

		if (isFrist) {
			imageTop.setBackgroundResource(urls[index]);
			isFrist = false;
		}

		ScaleAnimation scale = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		scale.setDuration(3000);
		scale.setFillAfter(true);

		AnimationSet animationSet = new AnimationSet(true);
		AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
		alpha.setDuration(3000);
		alpha.setFillAfter(true);
		alpha.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				imageDown
						.setBackgroundResource(urls[index + 1 >= urls.length ? 0
								: index + 1]);
				if (index + 1 >= urls.length) {
					index = -1;
				}
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				imageDown.bringToFront();
				setImageDownAnimation();
			}
		});
		animationSet.addAnimation(scale);
		animationSet.addAnimation(alpha);
		imageTop.startAnimation(animationSet);
	}

	private void setImageDownAnimation() {
		imageDown.clearAnimation();

		ScaleAnimation scale = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		scale.setDuration(3000);
		scale.setFillAfter(true);

		AnimationSet animationSet = new AnimationSet(true);
		AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
		alpha.setDuration(3000);
		alpha.setFillAfter(true);
		alpha.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				index = index + 2 < urls.length ? index + 2 : 0;
				imageTop.setBackgroundResource(urls[index]);

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				imageTop.bringToFront();
				nextAnimation();
			}
		});
		animationSet.addAnimation(scale);
		animationSet.addAnimation(alpha);
		imageDown.startAnimation(animationSet);
	}
	//获取换灯片的运行到那个位置
	public int getIndex() {
		return index;
	}
	//结束幻灯片
	public void stopAnim() {
		isFrist = true;
		imageDown.clearAnimation();
		imageTop.clearAnimation();
		removeAllViews();
		setVisibility(View.GONE);
	}
}
代码非常简单也有注释,希望能够帮到大家.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值