在ondraw中,我在android中遇到内存不足异常

在ondraw中,我在android中遇到内存不足异常,android,android-emulator,Android,Android Emulator,大家好,我在这里做一个应用程序,我需要实现绘图功能。所以我把myview作为自定义类,在绘图中我做了更多的分配。所以我得到了内存异常。所以我需要分配到哪里,你们有谁知道这一点,请建议我 MyView.class public class MyView extends View { private Canvas mCanvas; private Path mPath; private Paint

大家好,我在这里做一个应用程序,我需要实现绘图功能。所以我把myview作为自定义类,在绘图中我做了更多的分配。所以我得到了内存异常。所以我需要分配到哪里,你们有谁知道这一点,请建议我

MyView.class

    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);


              int leftx1=(int)(15*(screenWidth/1024));
                 int leftx2=(int)(1010*(screenWidth/1024));
                    int topy1=(int)(60*(screenHeight/600));
                    int topy2=(int)(530*(screenHeight/600));

                       canvas.drawLine(leftx1, topy1, leftx2, topy1, painto);   
                       canvas.drawLine(leftx1, topy1, leftx1, topy2, painto);
                       canvas.drawLine(15, topy2, leftx2, topy2, painto);
                       canvas.drawLine(leftx2, topy1, leftx2, topy2, painto);



            bitmapOrg = BitmapFactory.decodeResource(getResources(),
                        R.drawable.circles1_4);
            int leftorg=(int)(150*(screenWidth/1024));
            int toporg=(int)(110*(screenHeight/600));
              canvas.drawBitmap(bitmapOrg, leftorg, toporg, painto);

              bitmapOrg1 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.circles1_5);
              int leftorg1=(int)(430*(screenWidth/1024));
                int toporg1=(int)(130*(screenHeight/600));
               canvas.drawBitmap(bitmapOrg1, leftorg1,toporg1, painto);

       bitmapOrg2 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.circles1_6);
       int leftorg2=(int)(650*(screenWidth/1024));
               canvas.drawBitmap(bitmapOrg2, leftorg2,toporg, painto);

         bitmapOrg3 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.circles1_1);
         int leftorg3=(int)(170*(screenWidth/1024));
         int toporg3=(int)(350*(screenHeight/600));
               canvas.drawBitmap(bitmapOrg3, leftorg3,toporg3, painto);

              bitmapOrg4 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.circles1_3);
              int leftorg4=(int)(680*(screenWidth/1024));
               canvas.drawBitmap(bitmapOrg4, leftorg4,toporg3, painto);

            bitmapOrg5 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.circles1_2);
            int leftorg5=(int)(400*(screenWidth/1024));
             int toporg5=(int)(300*(screenHeight/600));
               canvas.drawBitmap(bitmapOrg5, leftorg5,toporg5, painto);


               Paint paint1 = new Paint();
                paint1.setAntiAlias(true);
                paint1.setColor(Color.BLACK);
                paint1.setStrokeWidth(3);
                paint1.setStyle(Paint.Style.FILL);
                paint1.setTextSize(13);
                canvas.drawText("Get  ready  to  write  place  your  pen  on  the  dot  and  follow  direction ", 120, 20, paint1);
                canvas.drawText("indicated  by  the  arrow . ", 120, 38, paint1);


                Paint p = new Paint();
                p.setAntiAlias(true);
                p.setTextSize(120);
                p.setColor(Color.LTGRAY);
                             Typeface font = Typeface.createFromAsset(getAssets(), "font/KINDTRG.TTF"); 




               canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

                canvas.drawPath(mPath, mPaint); 


     }  
     private float mX, mY;
   private  final 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;
   }
      }

Android给你的工作空间很小。位图非常重。你需要小心使用它们。确保在你不再需要它们的时候重新记录它们,并且考虑不要使用ARBBH88 88,除非你有需要透明度的alpha通道。 试着这样做: 释放附加到该特定活动的imageview

protected void onDestroy() {
    super.onDestroy();

    unbindDrawables(findViewById(R.id.rl_main));
    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();
    }
}
protectedvoid onDestroy(){
super.ondestory();
不可绑定的可提取文件(findViewById(R.id.rl_main));
gc();
}
私有void未绑定可提取项(视图){
if(view.getBackground()!=null){
view.getBackground().setCallback(null);
}
if(视图组的视图实例){
对于(int i=0;i<((视图组)视图)。getChildCount();i++){
未绑定的Drawables(((视图组)视图).getChildAt(i));
}
((视图组)视图);
}
}

在不再需要位图时释放它们日志?错误带来的红色东西?斯利西,你应该知道的。如果没有,谷歌可以帮助你。如果你读过一些关于java的问题,你应该知道。我相信我的建议会对你有所帮助,但我很好奇你为什么在这里使用位图?你想做什么?虚拟机不允许我们分配360000字节,位图大小超过了这个例外我的日志结果我用这种方法在暂停或销毁时释放这些位图。我会在你不使用它们时立即释放它们。。。肯定地说,至少每次调用onDraw时,等待onPause的时间肯定太长了。那么,我必须将这些位图设为null的位置,我会在
canvas.drawPath(mPath,mPaint)之后立即想象