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?

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
5w4rley
  • 353
  • 3
  • 15

3 Answers3

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.

devsnd
  • 7,382
  • 3
  • 42
  • 50
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

Community
  • 1
  • 1
zrgiu
  • 6,200
  • 1
  • 33
  • 35
  • 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. – 5w4rley Mar 27 '12 at 08:36
  • 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. – zrgiu Mar 27 '12 at 09:12
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.

a.ch.
  • 8,285
  • 5
  • 40
  • 53
  • 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 – 5w4rley Mar 27 '12 at 08:31
  • Look at @twall's answer - it does make sense. By the way, using `Animation` is an option. – a.ch. Mar 27 '12 at 08:55
  • http://stackoverflow.com/questions/2092951/how-to-close-android-application helped =) – 5w4rley Mar 27 '12 at 09:02
  • how that link helped you?! I would say that can be temporal work-around but you still have memory leaking in your app.. – Ewoks Mar 27 '12 at 10:46
  • 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. – 5w4rley Mar 27 '12 at 14:15