android自动切换图片,android-自动切换背景图片

因为好用,所以值得收藏。

a4c26d1e5885305701be709a3d33442f.png 》》》》 a4c26d1e5885305701be709a3d33442f.png 》》》》》 a4c26d1e5885305701be709a3d33442f.png

一、创建切换背景布局类

public class LaunchAppAnimatedBgRelativeLayout extends

RelativeLayout {

private Random random;

private final float MAX_ALPHA = 1F;

private final float MIN_ALPHA = 0F;

private final int ANIMATION_DURATION =

800;

private final int DISPLAY_DURATION = (2000 +

ANIMATION_DURATION);

private Handler handler;

private ImageView animationImageView;

private ImageView staticImageView;

private List imageIdList;

private int currentImageIndex = -1;

private boolean isContinueLoop = true;

public

LaunchAppAnimatedBgRelativeLayout(Context context,

AttributeSet

attrs, int defStyle) {

super(context, attrs,

defStyle);

init();

}

public

LaunchAppAnimatedBgRelativeLayout(Context context, AttributeSet

attrs) {

super(context, attrs);

init();

}

public

LaunchAppAnimatedBgRelativeLayout(Context context) {

super(context);

init();

}

private void init() {

random = new Random();

handler = new Handler();

staticImageView =

makeNewImageView();

animationImageView =

makeNewImageView();

this.setBackgroundColor(Color.BLACK);

}

private ImageView makeNewImageView() {

ImageView imageView = new

ImageView(getContext());

imageView.setScaleType(ScaleType.CENTER_CROP);

this.addView(imageView, new

LayoutParams(LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT));

return imageView;

}

private int calNextImageIdIndex() {

currentImageIndex++;

if (currentImageIndex >

imageIdList.size() - 1) {

currentImageIndex

= 0;

}

return currentImageIndex;

}

private void

setAlphaAnimatedImageViewFadeOut() {

calNextImageIdIndex();

float fromAlpha =

MAX_ALPHA;

float toAlpha =

MIN_ALPHA;

AlphaAnimation alphaAnimation =

new AlphaAnimation(fromAlpha, toAlpha);

alphaAnimation.setDuration(ANIMATION_DURATION);

alphaAnimation.setAnimationListener(new

AnimationListener() {

@Override

public void

onAnimationStart(Animation animation) {

staticImageView.setImageResource(imageIdList

.get(currentImageIndex));

}

@Override

public void

onAnimationRepeat(Animation animation) {

}

@Override

public void

onAnimationEnd(Animation animation) {

animationImageView.setImageResource(imageIdList

.get(currentImageIndex));

if

(isContinueLoop) {

handler.postDelayed(new

Runnable() {

@Override

public

void run() {

setAlphaAnimatedImageViewFadeOut();

}

},

DISPLAY_DURATION);

}

}

});

animationImageView.startAnimation(alphaAnimation);

}

public boolean isContinueLoop() {

return isContinueLoop;

}

public void setContinueLoop(boolean

isContinueLoop) {

if ((!(this.isContinueLoop))

&& isContinueLoop) {

this.isContinueLoop

= isContinueLoop;

setAlphaAnimatedImageViewFadeOut();

} else {

this.isContinueLoop

= isContinueLoop;

}

}

public void setImageIdList(List imageIdList)

{

this.imageIdList =

imageIdList;

currentImageIndex = 0;

isContinueLoop = true;

animationImageView.setImageResource(imageIdList.get(currentImageIndex));

handler.postDelayed(new

Runnable() {

@Override

public void

run() {

setAlphaAnimatedImageViewFadeOut();

}

}, DISPLAY_DURATION);

}

}

二、在mainActivity内加载该布局,并向该布局传递需要进行切换的图片list即可,可根据项目需求修改布局参数,以实现自己需要的功能。

@Override

protected void onCreate(Bundle

savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

final LinearLayout layout =

(LinearLayout) findViewById(R.id.mlinear);

launch = new

LaunchAppAnimatedBgRelativeLayout(this);

layout.addView(launch);

List imageIdList = new

ArrayList();

imageIdList.add(R.drawable.car2);

imageIdList.add(R.drawable.car3);

imageIdList.add(R.drawable.car4);

imageIdList.add(R.drawable.car7);

imageIdList.add(R.drawable.car8);

imageIdList.add(R.drawable.car9);

launch.setImageIdList(imageIdList);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值