http://mzh3344258.blog.51cto.com/1823534/804237
private final static floatTARGET_HEAP_UTILIZATION = 0.75f;
VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);
private final static int CWJ_HEAP_SIZE = 6* 1024* 1024 ;
//设置最小heap内存为6MB大小
VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);--------------------------------------------
Bitmap temp = BitmapFactory.decodeResource(context.getResources(), resource, opts); Bitmap bmp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp.getHeight(), flip, true); temp.recycle();
bmp = getBitmapFromRessourceID(R.drawable.menu); ratio = (float)this.height/(float)bmp.getScaledHeight(canvas); temp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth()*ratio), (int) (bmp.getHeight()*ratio-10), false); bmp.recycle(); bmp = temp;
---------------------- 撤销清理
protected void onDestroy() {
super.onDestroy();
unbindDrawables(findViewById(R.id.top_layout));
Runtime.getRuntime().gc();
}
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
try {
((ViewGroup) view).removeAllViews();
} catch (UnsupportedOperationException e) {
}
}
}
private void unbindDrawables(View view) { if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup && !(view instanceof AdapterView)) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); } }
---------------------- get resource
tmpBitmap.recycle(); tmpBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image1); myView1.setImageBitmap(tmpBitmap);
-----------------------sample
public class MyMapView extends View { private int xPos = 0; private int yPos = 0; private int space = 0; private WeakReference<Bitmap> resizedBitmap; private WeakReference<Bitmap> position; private WeakReference<Bitmap> mapBitmap; public void setMapBitmap(Bitmap value) { this.mapBitmap = new WeakReference<Bitmap>(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<Bitmap>(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 && 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(); } }