如何在Android上修复OutOfMemory

时间:2019-06-19 08:07:07

标签: android out-of-memory imagemap

我正在尝试解决Android中的ImageMap问题。因此,我编写了一个小类,将触摸坐标与先前定义的区域匹配。 这是课程:

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (observer != null) {
        if(checkClickArea(event)){
            observer.OnAreaClick(v,event, matchedArea);
        }

    }
    return false;
}

boolean checkClickArea(MotionEvent event){

    Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int displayWidth = size.x;
    int displayHeight = imageView.getHeight();

    int touchImageX = Math.round((imageWidth / displayWidth) * event.getX());
    int touchImageY = Math.round((imageHeight / displayHeight) * event.getY());


    if (arealist != null) {
        for (ImageArea area : arealist) {
            if (area.rectF.contains(touchImageX, touchImageY)) {
                matchedArea = area;
                return true;
            }
        }
    }
    return false;
}

到目前为止,这将起作用。为了将单击的区域标记为“单击”,我将不同的图像放入LayerDrawable中,并将其提供给“图像视图”。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = findViewById(R.id.imageView);
    imageView.setImageDrawable(getDrawable(R.drawable.kopf));
    drawablesIDs.add(R.drawable.kopf);

    ImageClickRecognizer imageClickRecognizer = new ImageClickRecognizer(getApplicationContext(), ((BitmapDrawable) getDrawable(R.drawable.kopf)));
    imageClickRecognizer.setImageView(imageView);
    imageClickRecognizer.setOnAreaClickListener(this);


}


@Override
public void OnAreaClick(View v, MotionEvent event, ImageClickRecognizer.ImageArea area) {



    final int resourceId = getResources().getIdentifier(area.shape.name, "drawable",getPackageName());


    if (drawablesIDs.contains(resourceId)){

        drawablesIDs.remove(Integer.valueOf(resourceId));
        updateImageView();

    }else{
        drawablesIDs.add(Integer.valueOf(resourceId));
        updateImageView();

    }
}


private void updateImageView(){
    Drawable[] layers = new Drawable[drawablesIDs.size()];

    for (int i=0; i<drawablesIDs.size(); i++){
        Drawable draw = getDrawable(drawablesIDs.get(i));
        draw.setAlpha(150);
        layers[i] = draw;
    }

    LayerDrawable layerDrawable = new LayerDrawable(layers);
    imageView.setImageDrawable(layerDrawable);

}

除了很小的延迟外,它也可以正常工作,唯一的问题是,一旦第十二个图像加载到LayerDrawable中,我会收到一个OOM错误。虽然图片是10 kBit png。

为什么会这样?

2 个答案:

答案 0 :(得分:0)

如果您会看到Android Loading Large images efficiently

在图像较大的情况下,这很常见。

在放入inSampleSize之前,您应该使用Drawable[]缩放图像

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 SQL to Microservices: Integrating AWS Lambda with Relational Databases中选择

calculateInSampleSize方法。

答案 1 :(得分:0)

尝试在清单文件中添加以下内容:

<application
    ...
    android:hardwareAccelerated="false"
    android:largeHeap="true">

    <activity>
        ...
    </activity>

</application>