我正在制作一个基本的益智应用游戏,我正在尝试显示关卡,以便玩家可以选择其中一个。
图片大约是1000x1000像素,我将它们设置为缩小到屏幕宽度平方的1/3左右的图像视图。大约有40个级别,这意味着要显示40个图像。这是我第一次使用android studio,所以当我将6张图像加载到屏幕上时,我没想到应用程序开始滞后。
我的应用程序停止运行,因为内存不足,所以我在线寻找降低图像质量的方法。我找到了google的答案Where they load the image into a bitmap, then load it again with the dimensions changed.因为我是初学者,所以我无法完全理解这些方法的作用,所以我只是将其复制并粘贴到我的代码中。
我设置了所有内容,就像教程告诉我的那样,但是当我运行我的应用程序时,图像停止显示。
我的代码:
public class MainMenuPuzzle {
//all puzzle images are equally sized squares so this int stores the sidelengths
private int width = Constants.SCREEN_WIDTH/3;
private ImageView image;
//stores the id of the image using R.id.filename
int id;
//changes the margins of the imageview
public ViewGroup.LayoutParams params;
//variables for margins on each side
private int l;
private int t;
private int r = 0;
private int b = 0;
public MainMenuPuzzle(ImageView image, Activity activity,int row, int column,int id){
this.image = image;
this.id = id;
ViewGroup.LayoutParams params = image.getLayoutParams();
final RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) image.getLayoutParams();
//there are 3 images displayed in every row, adjusts the margin depending on which column the image is in
if (column == 1){
l = 0;
}
else if(column == 2){
l = width;
}
else{
l = width*2;
}
//the vertical margin is based on which row of images the image is in
t = ((row-1)*width)+Constants.SCREEN_HEIGHT;
lp.width =width;
lp.height= width;
lp.setMargins(l,t,r,b);
//where I attempt to reduce the quality of the image so my app doesn't lag lmao
image.setImageBitmap(
decodeSampledBitmapFromResource(Constants.CURRENT_CONTEXT.getResources(), id, width, width));
image.setLayoutParams(params);
}
//from google
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
//from google
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
//random methods because I'm too lazy to make getters and setters for everything
public ImageView getImage(){
return image;
}
public void setHeight(int height){
params.height = height;
}
public void setWidth(int width){
params.width = width;
}
}