簡體   English   中英

Java:使用數組中的十六進制值延遲更改背景顏色

[英]Java: Change background color with a delay using hex values in an array

我需要使用存儲在String數組transmitArray中的十六進制顏色代碼值來更改空白ImageView的顏色,並在TransmitFreq中指定延遲。 但是,當我運行代碼時,只顯示第一種顏色(對應於第一個數組值)。

我嘗試了三種方法,即(thread.sleep),倒數計時器和post.delayed但沒有成功。 如果有人能指出我做錯了什么,我將不勝感激。

public class Main2Activity extends AppCompatActivity  {

    String [] transmitArray;
    long transmitFreq;
    public static int i;
    public static View colourView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main2);
        final String transmitArray [] = getIntent().getStringArrayExtra("COLOUR_DATA");
        transmitFreq = getIntent().getLongExtra("FREQ_VALUE", 0);
        int arrayLength = transmitArray.length;

        colourView = findViewById(R.id.colourBox);

       /*
       //Method 1: Using Countdown timer

        new CountDownTimer(transmitFreq*(transmitArray.length), transmitFreq) {

            public void onTick(long millisUntilFinished) {
                colourView.setBackgroundColor(Color.parseColor(transmitArray[i]));
                i++;
            }

            public void onFinish() {
                i=0;
            }
        }.start();

        //Method 2: Using post.delayed

        Handler handler = new Handler();
        for (i = 0; i < arrayLength ; i++) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    String transmitColour = transmitArray[i];
                    colourView.setBackgroundColor(Color.parseColor(transmitColour));
                }
            }, transmitFreq);
        }*/

        //Method 3: Using thread.sleep

        for (i = 0; i < arrayLength ; i++) {
            String transmitColour = transmitArray[i];
            colourView.setBackgroundColor(Color.parseColor(transmitColour));
            try {
                Thread.sleep(transmitFreq);                
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }

    }
}

您最初可以在onCreate方法中使用默認顏色,然后在OnCreate方法之外嘗試3種方法。 試試這個代碼

public class Main2Activity extends AppCompatActivity  {

String [] transmitArray;
long transmitFreq;
public static int i;
public static View colourView;


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

    colourView = findViewById(R.id.colourBox);
}

final String transmitArray [] = getIntent().getStringArrayExtra("COLOUR_DATA");
    transmitFreq = getIntent().getLongExtra("FREQ_VALUE", 0);
    int arrayLength = transmitArray.length;


 /*
   //Method 1: Using Countdown timer

    new CountDownTimer(transmitFreq*(transmitArray.length), transmitFreq) {

        public void onTick(long millisUntilFinished) {
            colourView.setBackgroundColor(Color.parseColor(transmitArray[i]));
            i++;
        }

        public void onFinish() {
            i=0;
        }
    }.start();

    //Method 2: Using post.delayed

    Handler handler = new Handler();
    for (i = 0; i < arrayLength ; i++) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                String transmitColour = transmitArray[i];
                colourView.setBackgroundColor(Color.parseColor(transmitColour));
            }
        }, transmitFreq);
    }*/

    //Method 3: Using thread.sleep

    for (i = 0; i < arrayLength ; i++) {
        String transmitColour = transmitArray[i];
        colourView.setBackgroundColor(Color.parseColor(transmitColour));
        try {
            Thread.sleep(transmitFreq);                
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM