I was looking at this answer over here, however it didn't really fix my problem.
For some reason the manifest.xml
doesn't overwrite my changes, even though I deleted my build
folder.
I am trying to load up 5 images on the screen, however even after optimizing them; they still run out of memory.
I was watching a video that teaches image scaling. It did solved a small portion of my problem. I am able to load my background image, however I wasn't able to load my other 4 images.
How can I optimize more efficiently? I was watching this video regards Bitmap optimization by Android, however I didn't understand much from it.
Here is my source code
MainActivity.java
package com.example.cliente.myapplication;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private static final float BYTES_PER_PX = 4.0f;
ImageView backgroundImage, upImage, downImage, leftImage, alertImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
backgroundImage = findViewById(R.id.background_image);
loadImage(backgroundImage, R.drawable.background);
upImage = findViewById(R.id.up_arrow_image);
loadImage(upImage, R.drawable.up);
downImage = findViewById(R.id.down_arrow_image);
loadImage(downImage, R.drawable.down);
leftImage = findViewById(R.id.left_arrow_image);
loadImage(leftImage, R.drawable.left);
alertImage = findViewById(R.id.alert_image);
loadImage(alertImage, R.drawable.alert);
}
private void loadImage(ImageView image, int imgSrc) {
if (readBitmapInfo() > MemUtils.megabytesFree()) {
subSampleImage(image, 32);
} else {
image.setImageResource(imgSrc);
}
}
private float readBitmapInfo() {
final Resources res = this.getResources();
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, R.drawable.background, options);
final float imageHeight = options.outHeight;
final float imageWidth = options.outWidth;
return imageWidth * imageHeight * BYTES_PER_PX / MemUtils.BYTES_IN_MB;
}
private void subSampleImage(ImageView image, int powerOf2) {
if (powerOf2 < 1 || powerOf2 > 2) {
return;
}
final Resources res = this.getResources();
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = powerOf2;
final Bitmap map = BitmapFactory.decodeResource(res, R.drawable.background, options);
image.setImageBitmap(map);
}
}
MemUtils.java
package com.example.cliente.myapplication;
public class MemUtils {
public static final float BYTES_IN_MB = 1024.0f * 1024.0f;
public static float megabytesFree() {
final Runtime rt = Runtime.getRuntime();
final float bytesUsed = rt.totalMemory();
final float mbUsed = bytesUsed / BYTES_IN_MB;
final float mbFree = megabytesAvailable() - mbUsed;
return mbFree;
}
public static float megabytesAvailable() {
final Runtime rt = Runtime.getRuntime();
final float bytesAvailable = rt.maxMemory();
return bytesAvailable / BYTES_IN_MB;
}
}