开发者

view.invalidate() not working to redraw imageview

开发者 https://www.devze.com 2023-02-21 19:11 出处:网络
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 te

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开发者_JAVA百科 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);

   }


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.


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.


I was having the same problem, i used:

runOnUiThread(new Runnable() {
    public void run() {
    myImageView.setImageBitmap(image);
    imageView.invalidate();
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消