如何在300毫秒内进行背景更改

时间:2016-03-20 12:21:41

标签: java android-studio

如何在300毫秒内进行背景更改? 用这个颜色代码?

.event

3 个答案:

答案 0 :(得分:1)

将数组转换为int数组,并将#替换为0xff

然后,您应该可以使用Value Animator来更改颜色

final static int[] codes = new int[] {
        0xfff0f8ff, 0xffFAEBD7, 0xff00ffff, 0xff7FFFD4,
        0xffF0FFFF, 0xffF5F5DC, 0xffffe4c4,
        0xff000000, 0xffffebcd, 0xff0000ff, 0xff8a2be2,
        0xffA52A2A, 0xffDEB887, 0xff5f9ea0, 0xff7FFF00,
        0xffD2691E, 0xffff7f50, 0xff6495ED, 0xfffff8dc,
        0xffDC143C, 0xff00FFFF, 0xff00008b, 0xff008b8b,
        0xffb8860b, 0xffa9a9a9, 0xffA9A9A9, 0xff006400,
        0xffbdb76b, 0xff8b008b, 0xff556b2f, 0xffff8c00,
        0xff9932cc, 0xff8B0000, 0xffE9967A, 0xff8FBC8F,
        0xff483D8B, 0xff2F4F4F, 0xff2F4F4F, 0xff00CED1,
        0xff9400D3, 0xffFF1493, 0xff00BFFF, 0xff696969,
        0xff696969, 0xff1E90FF, 0xffB22222, 0xffFFFAF0,
        0xff228B22, 0xffFF00FF, 0xffDCDCDC, 0xffF8F8FF,
        0xffFFD700, 0xffDAA520 };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final RelativeLayout colourSurface = (RelativeLayout)findViewById(R.id.main_rl_root);

    ValueAnimator colourAnimator = ValueAnimator.ofInt(codes);
    colourAnimator.setTarget(colourSurface);
    colourAnimator.setDuration(300); //Time in milli seconds from start to finish animation
    colourAnimator.setRepeatCount(-1); //-1 = repeat forever
    colourAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            if (colourSurface != null)
                colourSurface.setBackgroundColor((int) animation.getAnimatedValue());

        }
    });
    colourAnimator.start();

}

答案 1 :(得分:1)

使用集合 timertask

实施例

         String randomColor;
        final String[] codes = { "‪#‎F0F8FF‬", "‪#‎FAEBD7‬", "‪#‎00FFFF‬", "‪#‎7FFFD4‬", "‪#‎F0FFFF‬", "‪#‎F5F5DC‬", "‪#‎FFE4C4‬",
                "#000000", "‪#‎FFEBCD‬", "‪#‎0000FF‬", "‪#‎8A2BE2‬", "‪#‎A52A2A‬", "‪#‎DEB887‬", "‪#‎5F9EA0‬", "‪#‎7FFF00‬", "‪#‎D2691E‬",
                "‪#‎FF7F50‬", "‪#‎6495ED‬", "‪#‎FFF8DC‬", "‪#‎DC143C‬", "#00FFFF", "‪#‎00008B‬", "‪#‎008B8B‬", "‪#‎B8860B‬", "‪#‎A9A9A9‬",
                "#A9A9A9", "#006400", "‪#‎BDB76B‬", "‪#‎8B008B‬", "‪#‎556B2F‬", "‪#‎FF8C00‬", "‪#‎9932CC‬", "#8B0000", "#E9967A", "#8FBC8F",
                "#483D8B", "#2F4F4F", "#2F4F4F", "#00CED1", "#9400D3", "#FF1493", "#00BFFF", "#696969", "#696969", "#1E90FF", "#B22222",
                "#FFFAF0", "#228B22", "#FF00FF", "#DCDCDC", "#F8F8FF", "#FFD700", "#DAA520" };
        //      create a list from the array
        final List<String> l = new ArrayList<String>(Arrays.asList(codes));

        Collections.shuffle(l);
            randomColor = l.remove(0);
        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                Collections.shuffle(l);     //you shuffle the list
                randomColor = l.remove(0);  //get the element at an arbitrary position, zero in this case
                 //  your logic here
            }
        }, 0, 300);//put here time 300 milliseconds=0,3 second
    }

答案 2 :(得分:0)

为此目的使用Handler的postDelayed函数。它将在主UI线程上以指定的延迟运行您的代码,因此您将能够更新UI控件。 注意:onStop / onDestroy删除runnable 在color.xml中设置颜色

&lt; color name =“_ 1”&gt;#609EEB&lt; / color&gt;
&lt; color name =“_ 2”&gt;#FFEBCD&lt; / color&gt;
&lt; color name =“_ 3”&gt;#3F51B5&lt; / color&gt;
&lt; color name =“_ 4”&gt;#FF4081&lt; / color&gt;

Handler handler;
// On create handler need to be set
handler=new Handler();
runnable.run();

@Override
protected void onStop() {
    super.onStop();
    handler.removeCallbacks(runnable);
}

private int getColorCode(){
    int[] color = {
            R.color._1, R.color._2, R.color._3,R.color._4
    };
    int min = 0,max = color.length;
    Random r = new Random();
    int i= r.nextInt(max - min);
    return color[i];
}

private Runnable runnable = new Runnable()
{
    public void run()
    {
        try {
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.lineLinearLayout);
            linearLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),getColorCode()));

            handler.postDelayed(runnable, 300);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
};