0

嗨,我在这里做一个应用程序,我需要实现绘图功能。所以我把 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;
   }
      }
4

2 回答 2

2

尝试这种方式 :: 释放附加到该特定活动的图像视图..

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();
    }
}
于 2012-10-30T10:01:44.380 回答
0

Android 给您的工作空间非常小。位图非常重。你需要小心你如何使用它们。确保在您不再需要它们时立即回收()它们,并考​​虑不要使用 ARGB_8888,除非您有需要透明度的 Alpha 通道。

于 2012-10-30T09:58:27.957 回答