0

i try to rotate 3 imageViews (or better the Bitmaps behind them) every 10-100ms. i do the rotation like this:

ImageView ivLoad;
Bitmap bMapLoad;

....

Matrix mat=new Matrix();
mat.reset();
mat.postScale(1.55f, 1.55f);
mat.postRotate((float)currentLoadDegree+(float)LoadDegree);
bMapLoad = Bitmap.createBitmap(bMapLoadgr, 0, 0, bMapLoadgr.getWidth(), bMapLoadgr.getHeight(), mat, true);
ivLoad.setImageBitmap(bMapLoad);
ivLoad.setScaleType(ScaleType.CENTER);

....

the first time i start the app everthing works fine. second time also works but the 3rd time i start the app it crashs with the following error:

03-27 10:01:09.234: E/AndroidRuntime(3603): java.lang.OutOfMemoryError
03-27 10:01:09.234: E/AndroidRuntime(3603):     at android.graphics.Bitmap.nativeCreate(Native Method)    
03-27 10:01:09.234: E/AndroidRuntime(3603):     at android.graphics.Bitmap.createBitmap(Bitmap.java:605)  
03-27 10:01:09.234: E/AndroidRuntime(3603):     at android.graphics.Bitmap.createBitmap(Bitmap.java:551)

after trying around a long time i found out that when i call System.exit(0) in the onDestroy methode everthing works. now i don't know if there would be a better way because on google a lot of peaople mean that System.exit(0) is unsafe.

so will i get problems with this?

3 Answers 3

2

Instead of rotating the Bitmap, you could rotate the canvas you are drawing on.

canvas.save();
canvas.translate(-canvasWidth/2, -canvasHeight/2);
canvas.rotate(degrees)

canvas.drawBitmap( ... )

canvas.translate(-canvasWidth/2, -canvasHeight/2);
canvas.restore();

Now you only get a new bitmap, when the image itself is updated, even though you can rotate it as frequent as you like. But if you get a new Bitmap, you still need to call Bitmap.recycle() on the old one.

Sign up to request clarification or add additional context in comments.

Comments

1

You shouldn't recreate the bitmap on every step of the rotation, instead you should just try to draw it rotated. That's also possible with a Matrix (what you already use) and will avoid the excessive memory usage.

Android: How to rotate a bitmap on a center point

2 Comments

the problem is that the degrees i rotate depend on data i get over bluetooth. so the degrees change every time. and to rotate the bitmap i have to create a new one.
and what is the problem with that ? :-? Just save the angle as a member of your class, draw the image rotated each time based on that member, and just update that member whenever you want.
0

You get OutOfMemoryError because you load the Bitmap every time you rotate ImageView. You should consider reusing already loaded bitmap. Also call Bitmap.recycle() method when you do not need the bitmap any more.

5 Comments

well the rotation degrees are different every time. i tryed to recycle the bitmap before creating a new one but that crashes the app because it tryes to draw a recycle bitmap
Look at @twall's answer - it does make sense. By the way, using Animation is an option.
how that link helped you?! I would say that can be temporal work-around but you still have memory leaking in your app..
well the memory is just leaking bekause the process is running in background. wenn i kill the process all the memory is released. whenn looking on logcat i can see the heap growing without it every time i restart the app. but with the process kill the heap is the same size every time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.