views:

26

answers:

0

Hi,

I'm trying to loop 1 layer which is in the middle of a stack of semi transparent png images.

The initial layerDrawable is created in XML, then the LayerDrawable is retrieved using findViewById().getDrawable().

I can change one of the layers using LayerDrawable.setDrawableByLayerId() if the command is in the same method. But when I use a Handler and Runnable to set up the frame animation of the layer, the layer image just doesn't change.

I'm passing the Activity to the Runnable and then the Runnble to Handler.postDelayed(), which then calls a method of the Activity which shows the next image in the loop.

While debugging, all the right lines of code are being run but the image just refuses to change. If I do a LayerDrawable.invalidateDrawable() in the loopNext() method with the drawable that just got changed in the loop, that layer disappears so I'm fairly certain I'm referencing the right object.

I hesitate to use any other method of animating one of the layers (like stacked ImageViews) because I hope to be able to rotate the LayerDrawable as well.

Thanks in advance.

Code below...

public class Radar extends Activity {
int loopIndex = 0;

Runnable loopRun;
Handler loopHandler;

LayerDrawable layers = null;

Drawable[] drawA = new Drawable[3];

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // loading the png's from Resources. final version loads them from the web
    Resources res = getResources();
    drawA[0] = res.getDrawable(R.drawable.loop1);
    drawA[1] = res.getDrawable(R.drawable.loop2);
    drawA[2] = res.getDrawable(R.drawable.loop3);

    loopHandler = new Handler();

    ImageView loopView = (ImageView)findViewById(R.id.radar_layers);

    layers = (LayerDrawable) loopView.getDrawable();

    // this line works and changes the image in the layer
    layers.setDrawableByLayerId(R.id.radar_loop, drawA[1]);

    loopRun = new LoopRun(this);
    Handler loopHandler = new Handler();
    loopHandler.postDelayed(loopRun, 1000);

}

public void loopNext() {
    layers.setDrawableByLayerId(R.id.radar_loop, da[loopIndex]);
    loopIndex++;        
}

}

public class LoopRun implements Runnable {
Radar act;
public LoopRun(Radar a) {
    act = a;
}
@Override
public void run() {
    act.loopNext();
}

}