How to quote background colors in Android / Java?

I have a list of hexadecimal colors with duration in milliseconds for each of them. I would like to fill the screen with each color for its duration, and then move on to the next color.

I tried iterating over colors to do the following:

 myView.setBackgroundColor(Color.parseColor( theColor ));
 SystemClock.sleep( theDuration );

 myView.setBackgroundColor(Color.parseColor( nextColor ));
 SystemClock.sleep( nextDuration );

 etc...

which seemed obvious to me, but does nothing to represent when it works, at least in my AVD. I study this because Android only works at predefined times. (I have not tried calling "Invalidate ()").

What is the best way to display all colors sequentially?

(I understand that I should not call sleep () either, so any suggestions for this will also be appreciated.)

Thanks.

+4
3

Runnable . ( "" ) Runnable run().

public void playOnClick(View v) {
    Runnable runnable = new Runnable() {
        public void run() {
            ...
        }

, .

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        Bundle b = msg.getData();
        String theColor = b.getString("color");

        myView = (View) findViewById(R.id.bigView);
        myView.setBackgroundColor(Color.parseColor(theColor));
    }
};

run() Runnable , , Bundle/Message:

b = new Bundle();
b.putString("color", theColor);
msg = new Message();
msg.setData(b);
handler.sendMessage(msg);
0
new Thread() {
    @Override
    public void run() {
        YourActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            myView.setBackgroundColor(Color.parseColor( theColor ));
        }
        Thread.sleep( theDuration);

        YourActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            myView.setBackgroundColor(Color.parseColor( nextColor ));
        }
        Thread.sleep( nextDuration );
    }
}.start();

.

+2

, . Handler Runnable. , , , :

  • Runnable

    private Runnable runnable = null;
    
  • onCreate(), ,

    final Handler handler = new Handler();
    
  • runnable run()

    runnable = new Runnable() {
        @Override
        public void run() {
            //change the color
            myView.setBackgroundColor(currentColor);
    
            //run it again in nextDuration miliseconds
            handler.postDelayed(toggle, nextDuration);
        }
    };
    
    //start runnable in theDuration miliseconds
    handler.postDelayed(toggle, theDuration);
    

. , myView .

EDIT:

, , :

/ , ...

, onCreate() , .

This blogpost from the adroid developers website uses a very similar design as suggested above.

The handler runs the update code as part of the main thread, avoiding the overhead of the second thread, and also simplifying access to the view hierarchy used for the user interface.

See here and here CommonsWare answered and here - Light Alnhem answered .

0
source

Source: https://habr.com/ru/post/1525332/


All Articles