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?
You get
OutOfMemoryError
because you load theBitmap
every time you rotateImageView
. You should consider reusing already loaded bitmap. Also callBitmap.recycle()
method when you do not need the bitmap any more.Instead of rotating the Bitmap, you could rotate the canvas you are drawing on.
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.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