2013-02-27 39 views
1

我需要顯示在畫布上的圖像內存異常的我嘗試使用下面的代碼及其工作好一些times.but如果播放相同的頁面一次又一次我得到位圖的大小超過了內存exception.i衝浪在網絡和我設置樣本大小,那個時候也正在得到例外,請任何人幫助我這麼多天在我遇到這個問題的病房。出在我的應用程序的Android

Lines.class 

public class Lines extends Activity 
    { 

    BitmapDrawable sounddrawable,erasedrawable,backdrwable,fwddrwable,captiondrwable,img1drwable; 
Bitmap img1bitmap,soundbitmap,eraseBitmap,backbitmap,fwdbitmap,captionbitmap; 
Bitmap bitmapOrg1,bitmapOrg2,bitmapOrg3,bitmapOrg4,bitmapOrg5,bitmapOrg6,bitmapOrg; 

MyView myview; 
    View content; 
float screenHeight,screenWidth,screendensity; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setNoTitle(); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    screenHeight = displaymetrics.heightPixels; 
    screenWidth = displaymetrics.widthPixels; 
    screendensity = displaymetrics.densityDpi; 
    Log.i("screenHeight",""+screenHeight); 
    Log.i("screenWidth",""+screenWidth); 
    Log.i("screendensity",""+screendensity); 

     setContentView(R.layout.line); 

     widtho=(int)(91*(screenWidth/1024)); 
    highto=(int)(175*(screenHeight/600)); 
    bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, widtho, highto, true); 
    leftorg=(int)(120*(screenWidth/1024)); 
    toporg=(int)(130*(screenHeight/600)); 


     bitmapOrg1 = BitmapFactory.decodeResource(getResources(), 
       R.drawable.line1_2); 

     width1=(int)(90*(screenWidth/1024)); 
     hight1=(int)(175*(screenHeight/600)); 
     bitmapOrg1 = Bitmap.createScaledBitmap(bitmapOrg1, width1, hight1, true); 
     leftorg1=(int)(300*(screenWidth/1024)); 
     toporg1=(int)(130*(screenHeight/600)); 


    bitmapOrg2 = BitmapFactory.decodeResource(getResources(), 
       R.drawable.line1_3); 


    bitmapOrg2 = Bitmap.createScaledBitmap(bitmapOrg2, widtho, highto, true); 

    leftorg2=(int)(470*(screenWidth/1024)); 
    toporg2=(int)(130*(screenHeight/600)); 


      bitmapOrg3 = BitmapFactory.decodeResource(getResources(), 
       R.drawable.line1_4); 
    bitmapOrg3 = Bitmap.createScaledBitmap(bitmapOrg3, width1, hight1, true); 
       leftorg3=(int)(640*(screenWidth/1024)); 
    toporg3=(int)(130*(screenHeight/600)); 


      bitmapOrg4 = BitmapFactory.decodeResource(getResources(), 
       R.drawable.line1_5); 




      bitmapOrg4 = Bitmap.createScaledBitmap(bitmapOrg4, width1, hight1, true); 
       leftorg4=(int)(810*(screenWidth/1024)); 
     toporg4=(int)(130*(screenHeight/600)); 



    bitmapOrg5 = BitmapFactory.decodeResource(getResources(), 
       R.drawable.line1_6); 


     width6=(int)(237*(screenWidth/1024)); 
     hight6=(int)(139*(screenHeight/600)); 
      bitmapOrg5 = Bitmap.createScaledBitmap(bitmapOrg5, width6, hight6, true); 
leftorg5=(int)(150*(screenWidth/1024)); 
      toporg5=(int)(370*(screenHeight/600)); 


     bitmapOrg6 = BitmapFactory.decodeResource(getResources(), 
       R.drawable.line1_7); 

      bitmapOrg6 = Bitmap.createScaledBitmap(bitmapOrg6, width6, hight6, true); 
     leftorg6=(int)(600*(screenWidth/1024)); 
      toporg6=(int)(370*(screenHeight/600)); 

    } 
     mPaint = new Paint(); 

    mPaint.setAntiAlias(true); 
    mPaint.setDither(true); 

    mPaint.setColor(Color.BLACK); 
    mPaint.setStyle(Paint.Style.STROKE); 
    mPaint.setStrokeJoin(Paint.Join.ROUND); 
    mPaint.setStrokeCap(Paint.Cap.ROUND); 
    mPaint.setStrokeWidth(6); 

    } 

    private void setNoTitle() { 
    // TODO Auto-generated method stub 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
} 
private Paint  mPaint; 
    private Bitmap mBitmap; 

    public void colorChanged(int color) { 
     mPaint.setColor(color); 
    } 

    public class MyView extends View 
    { 

      private Canvas mCanvas; 
      private Path mPath; 
      private Paint mBitmapPaint; 

     public MyView(Context context) { 
      super(context); 
      // TODO Auto-generated constructor stub 

      mPath = new Path(); 



       mBitmapPaint = new Paint(Paint.DITHER_FLAG); 

     } 
     @Override 
      protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
       super.onSizeChanged(w, h, oldw, oldh); 

       if (mBitmap != null) { 
        mBitmap.recycle(); 
        mBitmap=null; 
       } 

       mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 


       mCanvas = new Canvas(mBitmap); 

       mCanvas.setBitmap(mBitmap); 

      } 

     @Override 
      protected void onDraw(Canvas canvas) { 

       if(action) 
       { 
        invalidate(); 
       } 


       Paint painto = new Paint(); 
       painto.setAntiAlias(true); 
       painto.setColor(getResources().getColor(R.color.magnata)); 

       painto.setStrokeWidth(3); 
       painto.setStyle(Paint.Style.FILL); 


         bitmapOrg.recycle(); 
       canvas.drawBitmap(bitmapOrg, leftorg, toporg, null); 
       bitmapOrg1.recycle(); 
      canvas.drawBitmap(bitmapOrg1, leftorg1,toporg1, null); 

       bitmapOrg2.recycle(); 

      canvas.drawBitmap(bitmapOrg2, leftorg2,toporg2, null); 
      bitmapOrg3.recycle(); 
       canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
       canvas.drawBitmap(bitmapOrg3, leftorg3,toporg3, null); 

       bitmapOrg4.recycle(); 
       canvas.drawBitmap(bitmapOrg4, leftorg4,toporg4, null); 

       bitmapOrg6.recycle(); 

       canvas.drawBitmap(bitmapOrg6, leftorg6,toporg6, null); 

       bitmapOrg5.recycle(); 
       canvas.drawBitmap(bitmapOrg5, leftorg5,toporg5, null); 

       canvas.drawPath(mPath, mPaint); 
    } 
     private float mX, mY; 
     private float TOUCH_TOLERANCE = 2; 
     private void touch_start(float x, float y) { 
       mPath.reset(); 
        mPath.moveTo(x, y); 
         mX = x; 
      mY = y; 
       } 
    private void touch_move(float x, float y) { 
     float dx = Math.abs(x - mX); 
     float dy = Math.abs(y - mY); 
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
    mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
    mX = x; 
    mY = y; 
    } 
    } 
private void touch_up() { 
    mPath.lineTo(mX, mY); 

    mCanvas.drawPath(mPath, mPaint); 

    mPath.reset(); 
    } 

@Override 
     public boolean onTouchEvent(MotionEvent event) { 
     float x = event.getX(); 
      float y = event.getY(); 

    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
     touch_start(x, y); 
     invalidate(); 
     break; 
    case MotionEvent.ACTION_MOVE: 
     touch_move(x, y); 
     invalidate(); 
     break; 
    case MotionEvent.ACTION_UP: 
     touch_up(); 
     invalidate(); 
     break; 
     } 
     return true; 
} 
} 
+0

你應該避免在初始化'的onDraw()'方法的對象, – 2013-02-27 06:49:24

+0

你確信回收所有的位圖上收的活動呢? – 2013-02-27 06:49:51

+0

yes.i暫停回收,onresume無用。我在繪製我沒有intialized在我的代碼任何東西與繪製對象的權利? – radha 2013-02-27 07:01:49

回答

-1

我不知道爲什麼你會得到一個OutOfMemory錯誤(因爲你沒有張貼堆棧跟蹤,我猜這不是你所得到的實際誤差)。

您使用Bitmap.recycle()是最有可能的問題。它僅用於丟棄位圖時使用,並且不會再次引用它。在任何情況下調用它實際上並不是必要的,它只是導致早期清理本地內存,當Bitmap完成時它將被釋放。

文檔說:

釋放與此相關的位圖的原生對象,並清除 參照像素數據。這不會同步釋放像素數據 ;它只是允許它被垃圾收集,如果 沒有其他參考。位圖被標記爲「死」的,這意味着它 會拋出一個異常的getPixels()或的setPixels()被調用, 會畫什麼。該操作不能顛倒,因此只有在確認 位圖沒有其他用途時才應調用 。這是一個高級調用,通常不需要調用, ,因爲當沒有對此位圖的更多引用時,正常的GC進程將釋放此內存。

所以,你絕對不應該的onDraw調用它,絕對不是你嘗試將位圖繪製到畫布右側前。我的建議是刪除所有對recycle()的調用。

+0

我要添加這些位圖上繪製出()來canvas.so。我在哪裏可以intialize那些 – radha 2013-02-27 07:13:14

+0

我不建議你不要初始化它們,我建議你停止調用Bitmap.recyle() 。這是沒有必要的,而且最有可能導致你的問題。只要刪除這些陳述。 – GreyBeardedGeek 2013-02-27 07:19:17

+0

我刪除了那麼也同樣的問題 – radha 2013-02-27 12:51:00

相關問題