tags:

views:

42

answers:

1

I have an array of drawables, that I would like to change by my own timer to a imageview. (i tried the other option with xml and setBackgroundResource, but does not work for me as I have hundret of pics and always got memory problem as it looks android assign already the whole memory for all pics at once. (just in this demo i shorted it to 4 images)

Ok, so first i make my array

private static int[] draws = {
     R.drawable.frankiearmevor_0001, 
     R.drawable.frankiearmevor_0002, 
     R.drawable.frankiearmevor_0003, 
     R.drawable.frankiearmevor_0002 
 };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    imgView = (ImageView) findViewById(R.id.fredi);

    // create timer        
    Timer updateProgressTimer = new Timer();
    updateProgressTimer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            myloop();
        }
    }, 0, 150);
}

int mycounter;
public void myloop()
{
    mycounter++;
    if (mycounter > 4) mycounter = 1;
    imgView.setImageResource(draws[mycounter-1]);
    String hallo; hallo = "now: "+mycounter;
    Log.d("1",hallo);

}

when I assign only a fixed image: imgView.setImageResource(draws[2]); it shows that fine and I see also my thread is logged fine, but when I exchange the fixed resource draws[2] into a dynamic draws[mycounter-1] .. i just get a black screen, no error, nothing.

what to do, so i will show the images :)

thx chris

EDIT: 22. August:

I tried now with the comment I got, it compiles fine, but somehow there is an error i guess.. it crash:

    imgView = (ImageView) findViewById(R.id.fredi);

    Timer updateProgressTimer = new Timer();

    updateProgressTimer.scheduleAtFixedRate(new TimerTask() 
    {
        @Override
        public void run() 
        {
            //myloop();
            imgView.post (new Runnable() 
            {

                public void run() 
                {
                    mycounter++;
                    if (mycounter > 10) mycounter = 1;
                    imgView.setImageResource(draws[1]);
                    //imgView.invalidate();
                    String hallo; hallo = "now: "+mycounter;
                    Log.d("1",hallo);
                 }

            });
        }
    }, 0, 150);
A: 

The ImageView update should happen on the UI Thread...

Refer this answer. The TimerTask runs on a different thread, so you've to use the imgView.post() to update the UI Component.

Are you sure you need to update the Component every 150ms? That's very expensive.

Good Luck.

st0le
Thanks ... i will try... its an animation ... so yes i need to update often to have it smooth
christian Muller