简体   繁体   中英

Android App, Changing Button Colors from Pattern?

I am trying to make an app similar to Simon Says. I want each button to light up and then darken one after another when the sequence is played. Right now, it will lighten and then darken a button, only if it is the last one in the sequence. When a button is clicked that is not last in the sequence it will just stay with an Alpha of 1.

This GIF is me playing the first couple turns.

Here is my code:

public void playSequence() {
        for (int i = 0; i < sequence.size(); ++i) {
            spot = 0;
            cbutton = (Button) sequenceb.get(sequence.get(i));
            cbutton.setAlpha((float) 1.0);

            final Handler handler1 = new Handler();
            handler1.postDelayed(new Runnable() {
                @Override
                public void run() {
                    cbutton.setAlpha((float) 0.35);
                }
            }, 500);
        }
    }

I dont know if you guys need to see anything else from my code, since I think its just a logic issue. The variable 'sequence' is an ArrayList with ints that refer to which tile is in the sequence, and 'sequenceb' refers to an ArrayList with references to the buttons from the board in it.

Also, I am pretty new to android programming so this code might be redundant, or really bad haha.

I ended up getting it working, although this is probably a horrible way to get it working. Here it is for reference.

public void playSequence() {
        System.out.println("Sequence Size: " + sequence.size() + ", Spot: " + spot);
        spot = 0;
        cbutton = (Button) sequenceb.get(sequence.get(spot));

        LightRunnable lRun = new LightRunnable(10);
        Thread lt = new Thread(lRun);
        lt.start();

        toggleButtons(true);
    }

public class DarkRunnable implements Runnable {
    private int var;

    public DarkRunnable(int var) {
        this.var = var;
    }

    @Override
    public void run() {
        try {
            ++spot;
            Thread.sleep(250);
            cbutton.setAlpha((float) 0.35);
            System.out.println("Darkening Tile: " + cbutton.getTag());
            Thread.sleep(10);
            fixAlphas((float) 0.35);
            if(spot < sequence.size()) {
                cbutton = (Button) sequenceb.get(sequence.get(spot));
                LightRunnable lRun = new LightRunnable(10);
                Thread lt = new Thread(lRun);
                lt.start();
            }
        } catch(Exception e) {}
    }
}

public class LightRunnable implements Runnable {
    private int var;

    public LightRunnable(int var) {
        this.var = var;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(250);
            cbutton.setAlpha((float) 1.0);
            System.out.println("Lightening Tile: " + cbutton.getTag());
            DarkRunnable dRun = new DarkRunnable(10);
            Thread dt = new Thread(dRun);
            dt.start();
        } catch (Exception e) {}
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM