我创建了一个小应用程序(游戏),它可以多次重复使用同一组图像。所以我认为我应该创建一个负责加载所有不同图像的类,然后我可以从其他类静态访问它们。但是,我相信这可能会导致垃圾收集器出现问题,这会导致我的应用在GC运行时滞后。这里是什么ImgLoader类的样子:加载位图并将它们存储为静态替代?
public class ImgLoader extends View {
public static Bitmap tree1;
public ImgLoader(Context context) {
super(context);
loadImgs();
}
public void loadImgs() {
System.gc(); // Manually Call GC
// TREES
tree1 = BitmapFactory.decodeResource(getResources(), R.drawable.tree);
tree1 = getResizedBitmap(tree1, MainActivity.height/2,
MainActivity.width/10);
}
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
public static Bitmap RotateBitmap(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
source.getHeight(), matrix, true);
}
}
注意,我只用一个位图的问题。从本质上讲,当我想在我的应用程序中使用的图像,我说:
object.image = ImgLoader.tree1;
什么是做这个的替代,事实并非如此内存密集型?
谢谢!
我会这样做,但我需要每次调整它们以适合屏幕。 – TastyLemons
我仍然不明白为什么调整位图大小需要静态引用它? –
我只是说,所以每次我加载它,我将不得不调用重新调整大小的方法。这不是没有效率吗? – TastyLemons