如何改变android的背景每???秒?

如何改变android的背景每???秒?,android,animation,Android,Animation,我想用淡入/淡出动画每10秒更改一次相对布局的背景。 所以我发现 //Transitiondrawable TransitionDrawable transition = (TransitionDrawable) viewObj.getBackground(); transition.startTransition(transitionTime); 但它只支持2个可绘制的,我想添加更多 有什么方法可以做到这一点吗?您可以创建自己的循环,例如: int delayBetweenAnimatio

我想用淡入/淡出动画每10秒更改一次相对布局的背景。 所以我发现

//Transitiondrawable
TransitionDrawable transition = (TransitionDrawable) viewObj.getBackground();
transition.startTransition(transitionTime);
但它只支持2个可绘制的,我想添加更多
有什么方法可以做到这一点吗?

您可以创建自己的循环,例如:

 int delayBetweenAnimations = 10000;

    for (int i = 0; i < yourImagesArray.length ; i++) {

        int delay = i * delayBetweenAnimations;

        yourImageview.postDelayed(new Runnable() {
            @Override
            public void run() {
                //set your image and animation here
            }
        }, delay);

    }

您可以创建自己的循环,例如:

 int delayBetweenAnimations = 10000;

    for (int i = 0; i < yourImagesArray.length ; i++) {

        int delay = i * delayBetweenAnimations;

        yourImageview.postDelayed(new Runnable() {
            @Override
            public void run() {
                //set your image and animation here
            }
        }, delay);

    }

首先实现MyAnim.java类,如下所示:

public class MyAnim extends Animation {


    private final RelativeLayout view;
    private int targetBackGround;


    public MyAnim(RelativeLayout view, int tagetBackGroundColor) {
        this.view = view;
        this.targetBackGround = tagetBackGroundColor;
    }
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        view.setBackgroundColor(targetBackGround);
    }


    public void setColor(int color) {
        this.targetBackGround = color;
    }
}
然后将以下代码添加到活动中,并在任何需要的地方调用animateBackground()方法:

    private MyAnim backgroundAnim;
    private int i;

    private void animateBackground(){
        final RelativeLayout animLay = (RelativeLayout) findViewById(R.id.animLay);
        final int colors[] = new int[]{Color.RED, Color.CYAN, Color.DKGRAY, Color.GREEN, Color.MAGENTA};
        backgroundAnim = new MyAnim(animLay, colors[i]);
        backgroundAnim.setDuration(1000);
        animLay.startAnimation(backgroundAnim);

        backgroundAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                if (i == colors.length - 1) {
                    i = 0;
                } else {
                    i++;
                }
                backgroundAnim.setColor(colors[i]);
                animLay.startAnimation(backgroundAnim);


            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

首先实现MyAnim.java类,如下所示:

public class MyAnim extends Animation {


    private final RelativeLayout view;
    private int targetBackGround;


    public MyAnim(RelativeLayout view, int tagetBackGroundColor) {
        this.view = view;
        this.targetBackGround = tagetBackGroundColor;
    }
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        view.setBackgroundColor(targetBackGround);
    }


    public void setColor(int color) {
        this.targetBackGround = color;
    }
}
然后将以下代码添加到活动中,并在任何需要的地方调用animateBackground()方法:

    private MyAnim backgroundAnim;
    private int i;

    private void animateBackground(){
        final RelativeLayout animLay = (RelativeLayout) findViewById(R.id.animLay);
        final int colors[] = new int[]{Color.RED, Color.CYAN, Color.DKGRAY, Color.GREEN, Color.MAGENTA};
        backgroundAnim = new MyAnim(animLay, colors[i]);
        backgroundAnim.setDuration(1000);
        animLay.startAnimation(backgroundAnim);

        backgroundAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                if (i == colors.length - 1) {
                    i = 0;
                } else {
                    i++;
                }
                backgroundAnim.setColor(colors[i]);
                animLay.startAnimation(backgroundAnim);


            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

在设置背景时,如何为背景设置数组?int[]imageList=new int[]{R.drawable.image1,R.drawable.image2…};我知道这一点,但我问如何设置背景数组非常感谢我的朋友有什么区别?背景不是图像吗?是的,用数组设置视图背景的方法是什么?如view.setBackground(R.drawable.ic);??在设置背景时,如何为背景设置数组?int[]imageList=new int[]{R.drawable.image1,R.drawable.image2…};我知道这一点,但我问如何设置背景数组非常感谢我的朋友有什么区别?背景不是图像吗?是的,用数组设置视图背景的方法是什么?如view.setBackground(R.drawable.ic);??谢谢,这绝对是我现在想要的。我如何使用淡入动画来更改背景颜色?谢谢,这绝对是我现在想要的。我如何使用淡入动画来更改背景颜色