Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You obviously have a memory leak. Read how to <a href="http://developer.android.com/resources/articles/avoiding-memory-leaks.html" rel="nofollow noreferrer">avoid memory leaks</a> and how to <a href="https://stackoverflow.com/questions/1147172/what-android-tools-and-methods-work-best-to-find-memory-resource-leaks">find memory leaks</a>.</p> <p>Here is a your code refactored to use WeakReference:</p> <pre><code>public class MyMapView extends View { private int xPos = 0; private int yPos = 0; private int space = 0; private WeakReference&lt;Bitmap&gt; resizedBitmap; private WeakReference&lt;Bitmap&gt; position; private WeakReference&lt;Bitmap&gt; mapBitmap; public void setMapBitmap(Bitmap value) { this.mapBitmap = new WeakReference&lt;Bitmap&gt;(value); } public MyMapView(Context context) { super(context); } public MyMapView(Context context, AttributeSet attrs) { super(context, attrs); } public MyMapView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void init(Bitmap mapBitmap) { Paint paint = new Paint(); paint.setFilterBitmap(true); int width = getMeasuredWidth(); int height = getMeasuredHeight(); resizedBitmap = new WeakReference&lt;Bitmap&gt;(Bitmap.createScaledBitmap( mapBitmap, width, height, true)); position = new WeakReference(BitmapFactory.decodeResource( getContext().getResources(), R.drawable.position)); space = (width - resizedBitmap.get().getWidth()) / 2; } // public void destroy() { // resizedBitmap.recycle(); // resizedBitmap = null; // position.recycle(); // position = null; // mapBitmap.recycle(); // mapBitmap = null; // } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec)); } @Override protected void onDraw(Canvas canvas) { if (mapBitmap != null) { canvas.drawBitmap(resizedBitmap.get(), space, 0, null); } if (xPos != 0 &amp;&amp; yPos != 0) { canvas.translate(xPos + space - position.get().getWidth() / 2, yPos - position.get().getHeight() / 2); canvas.drawBitmap(position.get(), new Matrix(), null); } } public void updatePosition(int xpos, int ypos) { xPos = xpos; yPos = ypos; invalidate(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload