您的位置:首页 > 其它

自定义View实现换灯片

2016-01-14 11:30 302 查看
前面两篇文章都是关于幻灯片是怎么实现的,但是效果都不是非常理想,结合前两次的知识我自定义了一个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);
}
}
代码非常简单也有注释,希望能够帮到大家.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: