我想在循环中使用随机颜色作为背景,平滑过渡效果

时间:2016-04-21 18:54:44

标签: android android-layout android-fragments android-studio

我的问题不是在背景上获得随机颜色而是我只是将2种颜色与动画效果作为背景。当我重新启动应用程序2颜色更改。 我的目标是必须使用随机颜色作为背景,并使用平滑的颜色变化动画 这是我的代码

public class MainActivity extends AppCompatActivity {
int color1,color2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final RelativeLayout targetView = (RelativeLayout) findViewById(R.id.new2);

            BackgroundPainter backgroundPainter = new BackgroundPainter();


            color1=getRandColor();
            color2=getRandColor();
            backgroundPainter.animate(targetView, color1, color2);





   // int color1 = ContextCompat.getColor(this, R.color.colorAccent);
    //int color2 = ContextCompat.getColor(this, R.color.colorPrimary);


}

    public int getRandColor(){
        Random rand = new Random();
        int r = rand.nextInt(255);
        int g = rand.nextInt(255);
        int b = rand.nextInt(255);
        int rando = Color.rgb(r, g, b);
        return rando;
    }

public class BackgroundPainter {

    private static final int MIN = 1800;
    private static final int MAX = 2300;

    private final Random random;

    public BackgroundPainter() {
        random = new Random();

    }

    public void animate(@NonNull final View target, @ColorInt final int color1,
                        @ColorInt final int color2) {


        final ValueAnimator valueAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), color1, color2);

        valueAnimator.setDuration(randInt(MIN, MAX));

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override public void onAnimationUpdate(ValueAnimator animation) {
                target.setBackgroundColor((int) animation.getAnimatedValue());
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override public void onAnimationEnd(Animator animation) {
                //reverse animation
                animate(target, color2, color1);
            }
        });

        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.start();
    }

    private int randInt(int min, int max) {
        return random.nextInt((max - min) + 1) + min;
    }
}
}

这是我的主要xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="weathercheck.bt4u.com.myapplication.MainActivity"
android:id="@+id/screen"
android:orientation="horizontal">


</LinearLayout>

1 个答案:

答案 0 :(得分:2)

只需更改

animate(target, color2, color1);

animate(target, color2, getRandColor());