4

Ok guys, this may sound dumb, but I have been banging my head against the keyboard for some time now trying to figure out why this will not refresh. the basics: I have a little sample app that i am testing to see if i can rotate an image around a point a X amount of degrees, and show it one degree at a time to make a smooth animation. So I have a great sample i found that works great with a slider bar, basically setting the images rotation to a point on the slider bar, great! but.... when i try and create a for loop with a random number and use my for variable updating the image along the way every degree... it does nothing... and all i get is the updated image at the end... but when i drag my finger on he slider bar the graphic is updated instant as me spinning it... I cant figure out what i am doing wrong here... here is the code with the slider... i don't have my piece that creates the random number and draws it but essentially i did it behind a button click

essentially if you look at this piece i did the same behind a button again but it doesnt do it "real time". i called view.invalidate() and view.postinvalidate() to try to force it but no go...

 @Override
   public void onProgressChanged(SeekBar seekBar, int progress,
     boolean fromUser) {
    // TODO Auto-generated method stub
    curRotate = (float)progress;
    drawMatrix();
   }

private void drawMatrix(){

    Matrix matrix = new Matrix();
       matrix.postScale(curScale, curScale);
       matrix.postRotate(curRotate);

       Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
       myImageView.setImageBitmap(resizedBitmap);

   }
noobS41bot
  • 71
  • 1
  • 2
  • 5

3 Answers3

1

I think what you did was something like:

for (int degrees = 0 ; degrees < maxRotation ; i++) {
    // perform the rotation by matrix
    myImageView.invalidate();
}

This wouldn't work because invalidate() only schedules a redraw in the main thread event queue. This means that the redraw will be performed only when the current code has all been executed (in this case, the for cycle).

For a simple rotation a Tween Animation would be better suited. For more advanced stuff (like game animations) you might need to create a custom view or use SurfaceView.

bigstones
  • 15,087
  • 7
  • 65
  • 82
  • I looked at the tween animation, but I do not think I tried to set the "too rotation" to anything more then 360.... maybe i could try that and it might suit my needs... thank you! – noobS41bot Apr 04 '11 at 16:20
0

Sounds like you're blocking the UI thread with your code to rotate the image.

I don't have any code to show you right now (reply back and when I'm home tonight I can post something that should help), but yuo will probably get better results placing your rotate code in an AsyncTask, see the Painless Threading area of the dev site for more info.

RivieraKid
  • 5,923
  • 4
  • 38
  • 47
  • Thanks! I will check out threading... a common thing even in the world i am used to with c# however in that world I can still cause a force redraw of the main UI thread (its not optimal) but in small scales it would still work just fine... hmmm ill take a look and see if i can spin off a thread, and then update the UI via a thread safe activity. Thanks! – noobS41bot Apr 04 '11 at 16:22
  • Alternatively, you could use a `Runnable` in your UI thread which is easier to create and use and you can have it update your UI also. – RivieraKid Apr 04 '11 at 20:02
  • Awesome thank you. i am going to try spinning off a thread that will do the work and end itself. i will see if i can do that tonight. hopefully it can kill itself lol. – noobS41bot Apr 04 '11 at 20:52
  • Thanks again for all your help. so i figured out the asynctask, however either i am doing something wrong, or its just not meant for graphics syncing at the scale i am looking for, i get about a handful of screen redraws... that is about it... i think i am trying to update it too much (every degree) i could try a smaller amount, or move on to a true thread maybe... ugh, it should not be this hard hahahaha – noobS41bot Apr 05 '11 at 15:54
  • Actually i just thought about it, probably because i am doing the for loop in the background which can happen much faster then the re-draw and they are happening independently which is what we want really, but when i sends another re-draw the degree difference is much greater so it updates that... hmmmm i need a callback maybe or to slow it down i guess... – noobS41bot Apr 05 '11 at 16:06
  • In that case, you might want to look into [this](http://developer.android.com/guide/topics/graphics/index.html) page on the dev site helps you decide the best way of managing your drawing. A `SurfaceView` may be the best option - the LunarLander SDK example demonstrates its use. – RivieraKid Apr 05 '11 at 16:27
0

I was having the same problem, i used:

runOnUiThread(new Runnable() {
    public void run() {
    myImageView.setImageBitmap(image);
    imageView.invalidate();
    }
});
neteinstein
  • 17,529
  • 11
  • 93
  • 123