user4398570
user4398570

Reputation:

How to alternate two imageview? Android

Now the image is changed as soon as I click on.

I would like to make this process AUTOMATIC (ex. every second)

this is my actually code

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.imageView:
            foto.setVisibility(View.INVISIBLE);
            foto1.setVisibility(View.VISIBLE);
            break;
        case R.id.imageView2:
            foto1.setVisibility(View.INVISIBLE);
            foto.setVisibility(View.VISIBLE);
    }
}

Upvotes: 1

Views: 122

Answers (5)

user3514956
user3514956

Reputation:

Have a look at ViewFlipper

http://developer.android.com/reference/android/widget/ViewFlipper.html

You can set the delay using setFlipInterval(int) or in the XML using android:flipInterval

Upvotes: 2

danips
danips

Reputation: 26

Yo can have a periodic task to do it:

ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(1);

scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
  public void run() {
    runOnUiThread(new Runnable() {
      public void run() {
        if (foto.getVisibility == View.VISIBLE) {
            foto.setVisibility(View.INVISIBLE);
            foto1.setVisibility(View.VISIBLE);
        } else {
            foto.setVisibility(View.VISIBLE);
            foto1.setVisibility(View.INVISIBLE);
        }
      }
    });
  }
}, 0, 1, TimeUnit.SECONDS);

Upvotes: 0

snachmsm
snachmsm

Reputation: 19223

maybe use ViewSwitcher (in your casae ImageSwitcher should work) and switch Views using Handler?

private static final int delay=1000; //ms
Handler h = new Handler();
Runnable r = new Runnable(){
    public run(){
        //viewSwitcher.showNext();
        //exampole of swtiching
        h.postDelayed(r, delay);
    }
}
h.postDelayed(r, delay);

to stop this loop use h.removeCallbacks(r)

you may also use viewSwitcher.postDelayed

Upvotes: 0

GIGAMOLE
GIGAMOLE

Reputation: 1266

Try this:

boolean isFirstVisible;
long millis;        

while(true) {
    millis = System.currentTimeMillis();

    if (isFirstVisible) {
        foto1.setVisibility(View.INVISIBLE);
        foto.setVisibility(View.VISIBLE);
        isFirstVisible = false;
    } else {
        foto.setVisibility(View.INVISIBLE);
        foto1.setVisibility(View.VISIBLE);
        isFirstVisible = true;
    }

    Thread.sleep(1000 - millis % 1000);
}   

Upvotes: 0

Evripidis Drakos
Evripidis Drakos

Reputation: 870

You should use a Handler with `postDelayed'. See this answer for some code.

Also, you can try using 'ImageSwitcher' instead of two 'ImageView's

Upvotes: 0

Related Questions