0

I want to give the sensation that a button was pressed by the system. The two images are used when the user presses the button and I want to create the same effect using a thread sleeping between the display of both images.

But the system doesn't update the button src as expected. Resuming I want to change the src, wait 600 ms and update again. Can someone give me a direction on it?

switch (color) {
case 1: //green
    // first image
    Main.imageButtonGreen.setImageResource(R.drawable.light_green);

    //this is just to make it sleep to give the sensation that the button was pressed 
    try {
        Blink blink = new Blink();
        Thread t = new Thread(blink);
        t.run();
        t=null;
    }
    catch (Exception e){if (Main.debug) Log.d("Blink.blink Switch color= ", e.getMessage());}
    //scond image
    Main.imageButtonGreen.setImageResource(R.drawable.dark_green);
    break;
}
honk
  • 9,137
  • 11
  • 75
  • 83
Succar
  • 1
  • 1
  • 1
    possible duplicate of [How to pause / sleep thread or process in Android?](http://stackoverflow.com/questions/1520887/how-to-pause-sleep-thread-or-process-in-android) – Henry Aug 03 '14 at 07:02
  • This piece for code is certainly insufficient. How the color variable is updated? What Blink runnable does? ... – cyanide Aug 03 '14 at 07:20
  • color is just a int that represents the button. I have 4 buttons and each calls the function sending a int as identifier. But this is not important at all. – Succar Aug 05 '14 at 00:56

1 Answers1

0

I recommend you to use Handler or AsyncTask. This is how I would make:

Create Handler in UI Thread:

Handler UIHandler = new Handler(this);

Now just call

UIhandler.postDelayed(new Runnable(){
                  @Override
                  public void run() {
                    button.setBackgroundColor(Color.GREEN);
               }}, 600);

or even better to make changing color smoother

final long startTime = SystemClock.uptimeMillis();
final long duration = 600;

//here specify what interpolator you like
final Interpolator interpolator = new LinearInterpolator();

//here you can pass whatever you want
final int startColor = Color.GREEN;
final int endColor = Color.CYAN;

//take start color components
final int startRed = Color.red(startColor);
final int startGreen = Color.green(startColor);
final int startBlue = Color.blue(startColor);

//take delta color components
final int deltaRed = Color.red(endColor)-startRed;
final int deltaGreen = Color.green(endColor)-startGreen;
final int deltaBlue = Color.blue(endColor)-startBlue;

UIhandler.post(new Runnable() {
    @Override
    public void run() {
        long elapsed = SystemClock.uptimeMillis() - startTime;
        float t = interpolator.getInterpolation((float) elapsed / duration);

        if (t < 1.0) {
            button.setBackgroundColor(Color.argb(255, (int)(startRed+t*deltaRed), (int)(startGreen+t*deltaGreen), (int)(startBlue+t*deltaBlue)));
            // Post again 16ms later.
            UIhandler.postDelayed(this, 16);
        } else{
            //to restore default values
            button.setBackgroundColor(endColor);
        }
    }
});

Also you can change alpha parameter by passing value from 1 to 255 in Color.argb() as first parameter

Roman
  • 166
  • 1
  • 7