hello i know this is a dupicat tread but the answer did not seem to work for me my app crashes on second time running so if i run it once its ok but when i close and run again it crashes giving me this error in the log cat java.lang.OutOfMemoryError: bitmap size exceeds VM budget i tried using this code
@Override
protected void onDestroy(){
// TODO Auto-generated method stub
super.onDestroy();
unbindDrawables(sView);
System.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));
}
((ViewGroup) view).removeAllViews();
}
}
but no luck i am drawing my bitmaps on a canvas using the surfaceview class this is my code private SurfaceV sView;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
sView = new SurfaceV(this);
setContentView(sView);
}
and in the sView class
private SurfaceHolder holder;
private Thread thread = null;
private Canvas canvas;
private Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.sky);
private Bitmap bg = Bitmap.createScaledBitmap(background, w, h, false);
private Bitmap sky = BitmapFactory.decodeResource(getResources(), R.drawable.sky2);
private Bitmap s = Bitmap.createBitmap(sky, 210, 10, 230, 230);
private Bitmap c1 = Bitmap.createBitmap(sky, 10, 14, 182, 100);
private Bitmap c2 = Bitmap.createBitmap(sky, 10, 142, 182, 100);
private Bitmap c3 = Bitmap.createBitmap(sky, 10, 292, 182, 112);
private Bitmap sun = Bitmap.createScaledBitmap(s, sunSize, sunSize, false);
private Bitmap cloud1 = Bitmap.createScaledBitmap(c1, cloudWidth, cloudHeigth, false);
private Bitmap cloud2 = Bitmap.createScaledBitmap(c2, cloudWidth, cloudHeigth, false);
private Bitmap cloud3 = Bitmap.createScaledBitmap(c3, cloudWidth, cloudHeigth, false);
public SurfaceV(Context context){
super(context);
holder = getHolder();
}
@Override
public void run(){
while(running){
if(!holder.getSurface().isValid()){
continue;
}
startTime = System.currentTimeMillis();
canvas = holder.lockCanvas();
draw(canvas);
holder.unlockCanvasAndPost(canvas);
doFpsCheck(startTime);
}
@Override
public void draw(Canvas canvas){
super.draw(canvas);
canvas.drawBitmap(bg, 0, 0, null);
canvas.drawBitmap(sun, 20, 20, null);
canvas.drawBitmap(cloud1, c1x, c1y, null);
canvas.drawBitmap(cloud2, c2x, c2y, null);
canvas.drawBitmap(cloud3, c3x, c3y, null);
}
please any help will be very much appriciated thank you in advance