如何在android中以编程方式增加应用程序的堆大小

时间:2012-11-08 13:47:00

标签: android android-emulator

我正在为使用显示指标的所有设备做一个应用程序..在2.2版本中,这里我使用自定义视图绘制function.some次它工作得很好。我学习了很多次,当我得到outof memory exception.in emulater我增加了堆大小那个时间它的工作都很好。如果在旧版本中增加堆大小,并且我在我的代码中做错了我不明白你有没有想法建议我.....

公共类A扩展了活动{

BitmapDrawable   sounddrawable,erasedrawable,backdrwable,fwddrwable,captiondrwable,strtdrwable,stpdrwable;
Bitmap  soundbitmap,eraseBitmap,backbitmap,fwdbitmap,captionbitmap,strtbitmap,stpbitmap;
  MyView myview;
  GlobalClass global;
  RelativeLayout.LayoutParams lp6;
  SampleView sampleview;
ImageView img;
MediaPlayer nextsound;
 boolean count=true;
   MediaPlayer mediay2;
  ImageView horn;
  String  user;
  int username;
 Bundle b;
RelativeLayout relativeLayout,relativeLayout1;
ImageView strtbtn,stpbtn,erase;
 float screenHeight,screenWidth,screendensity;


  public  boolean action=false;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
           requestWindowFeature(Window.FEATURE_NO_TITLE);

        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.alh);
    int topsampl=(int)(80*(screenHeight/600));
    global=(GlobalClass)this.getApplication();
    if(sampleview==null)
    {
         sampleview=new SampleView(this);
    }
         lp6 = new RelativeLayout.LayoutParams(
                 android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
             sampleview.setLayoutParams(lp6);
            lp6.setMargins(0,topsampl , 0, 0);

       relativeLayout=(RelativeLayout)findViewById(R.id.relative);

       RelativeLayout.LayoutParams layoutrel= (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();      
       layoutrel.height=(int)(600*(screenHeight/600));  

       ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
       int memoryClass = am.getMemoryClass();
       Log.v("onCreateAAAAAAAAAA", "memoryClass:" + Integer.toString(memoryClass));


        relativeLayout1=(RelativeLayout)findViewById(R.id.relative2);

       RelativeLayout.LayoutParams layoutre2= (RelativeLayout.LayoutParams)           relativeLayout1.getLayoutParams();       
       layoutre2.height=(int)(35*(screenHeight/600));  



       int left=(int)(15*(screenWidth/1024));
       int top=(int)(50*(screenHeight/600));

       int widthhh=(int)(80*(screenWidth/1024));
       int hifhtttt=(int)(50*(screenHeight/600));

       ImageView I2=new ImageView(this);

       if(captiondrwable!= null) {
            captionbitmap.recycle();
               captiondrwable= null;
                  }

       if(captiondrwable== null) {
          captionbitmap=BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.practise));
       }
          captiondrwable = new BitmapDrawable(captionbitmap); 
             I2.setBackgroundDrawable(captiondrwable);
       RelativeLayout.LayoutParams rlp=new RelativeLayout.LayoutParams(widthhh,hifhtttt);
       rlp.setMargins(left, top, 0, 0);
       relativeLayout.addView(I2,rlp);


       b =getIntent().getExtras();
           user=b.getString("k1");
          username=Integer.parseInt(user);

        Log.i("username..........",""+username);
        Log.i("user..........",""+user);






       ImageView next=(ImageView)findViewById(R.id.imv1a);
       if(fwddrwable!= null) {
           fwdbitmap.recycle();
               fwddrwable= null;
                    }

       if(fwddrwable== null) {
          fwdbitmap=BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.next_5011));
       }    
          fwddrwable = new BitmapDrawable(fwdbitmap); 
              next.setBackgroundDrawable(fwddrwable);
       RelativeLayout.LayoutParams layoutnext= (RelativeLayout.LayoutParams) next.getLayoutParams();       
       layoutnext.height=(int)(35*(screenHeight/600));  
       layoutnext.width=(int)(50*(screenWidth/1024));

       next.setOnClickListener(new OnClickListener() {


        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Log.i("next","..........");
            Intent i =new Intent(A.this,Smallletera.class);
            i.putExtra("k1", user);
            startActivity(i);

            if(nextsound==null)
            {
                nextsound =          MediaPlayer.create(getApplicationContext(),R.raw.chime_up);
            }
            nextsound.start();

         if(nextsound!=null)
            {
            }
        }
    });

       ImageView back=(ImageView)findViewById(R.id.back);
       if(backdrwable!= null) {
           backbitmap.recycle();
           backdrwable= null;
               }
       if(backdrwable== null) {
           backbitmap=BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.back1_50));
       }
        backdrwable = new BitmapDrawable(backbitmap); 
          back.setBackgroundDrawable(backdrwable);
       RelativeLayout.LayoutParams layoutback= (RelativeLayout.LayoutParams) back.getLayoutParams();       
       layoutback.height=(int)(35*(screenHeight/600));  
       layoutback.width=(int)(50*(screenWidth/1024));
     back.setOnClickListener(new OnClickListener() {


        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Log.i("next","..........");

            Intent i =new Intent(A.this,HWindex.class);
            startActivity(i);
        }
    });

     if(myview==null)
     {

       myview = new MyView(this);
     }
       myview.setBackgroundColor(Color.WHITE);

    myview.setId(004);
    lp6 = new RelativeLayout.LayoutParams(
         android.view.ViewGroup.LayoutParams.WRAP_CONTENT,   android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
myview.setLayoutParams(lp6);
lp6.setMargins(0,top , 0, 0);
relativeLayout.addView(myview,lp6);

 horn=new ImageView(this);
     if(sounddrawable!= null) {
      soundbitmap.recycle();
         sounddrawable= null;
     }
       if(sounddrawable== null) {
           soundbitmap=BitmapFactory.decodeStream(getResources().openRawResource(   R.drawable.horn));
       }
         sounddrawable = new BitmapDrawable(soundbitmap); 
         horn.setBackgroundDrawable(sounddrawable);

      int left1=(int)(830*(screenWidth/1024));
               int top1=(int)(50*(screenHeight/600));

          int widthhh1=(int)(60*(screenWidth/1024));
           int hifhtttt1=(int)(60*(screenHeight/600));
              RelativeLayout.LayoutParams hn=new RelativeLayout.LayoutParams(widthhh1,hifhtttt1);
    hn.setMargins(left1,top1,0,0);
     relativeLayout.addView(horn,hn);

  horn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(mediay2==null)
            {
             mediay2 = MediaPlayer.create(A.this, R.raw.uppercaseinstructions);
            }

               mediay2.start();
               if(mediay2!=null)
               {

               }
        }
    });






       strtbtn = new  ImageView(this);

      if(strtdrwable!= null) {
         strtbitmap.recycle();
          strtdrwable= null;
     }
     if(strtdrwable== null) {
                strtbitmap=BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.play_50));
  }
  strtdrwable = new BitmapDrawable(strtbitmap); 
   strtbtn.setBackgroundDrawable(strtdrwable);
         int left2=(int)(520*(screenWidth/1024));
    int top2=(int)(60*(screenHeight/600));

 int widthhh2=(int)(40*(screenWidth/1024));
    int hifhtttt2=(int)(40*(screenHeight/600));
   strtbtn.setId(1);
    RelativeLayout.LayoutParams rlp1=new        RelativeLayout.LayoutParams(widthhh2,hifhtttt2);
      rlp1.setMargins(left2, top2, 0, 0);

   relativeLayout.addView(strtbtn,rlp1);

   strtbtn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(count==true)
        {
         Log.i("---------dfgdf-----","--------");
      relativeLayout.addView(sampleview,lp6);
          count=false;
        }



    }
});

  stpbtn=new ImageView(this);

  if(stpdrwable!= null) {
     stpbitmap.recycle();
    stpdrwable= null;
       }
  if(stpdrwable== null) {
         stpbitmap=BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.stop_50));
   }
 stpdrwable = new BitmapDrawable(stpbitmap); 
  stpbtn.setBackgroundDrawable(stpdrwable);
        int left3=(int)(570*(screenWidth/1024));
        int top3=(int)(60*(screenHeight/600));



        stpbtn.setId(2);
        RelativeLayout.LayoutParams rlp2=new RelativeLayout.LayoutParams(widthhh2,hifhtttt2);
      rlp2.setMargins(left3, top3,0,0);
      relativeLayout.addView(stpbtn,rlp2);

      stpbtn.setOnClickListener(new OnClickListener() {


    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(count==false)
        {
         Log.i("------stop--------","--");
      relativeLayout.removeView(sampleview);
         count=true;
        }

    }
});

     erase=new ImageView(this);


     if(erasedrawable!= null) {
         eraseBitmap.recycle();
        erasedrawable= null;
          }
     if(erasedrawable== null) {
     eraseBitmap=BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.eraser_50));
     }
     erasedrawable = new BitmapDrawable(eraseBitmap); 
    erase.setBackgroundDrawable(erasedrawable);
     int left4=(int)(620*(screenWidth/1024));
     int top4=(int)(50*(screenHeight/600));

     int widthhh4=(int)(60*(screenWidth/1024));
     int hifhtttt4=(int)(60*(screenHeight/600));
     RelativeLayout.LayoutParams rlp4=new RelativeLayout.LayoutParams(widthhh4,hifhtttt4);
     rlp4.setMargins(left4, top4, 0, 0);
     relativeLayout.addView(erase,rlp4);

     erase.setOnClickListener(new OnClickListener() {


    public void onClick(View v) {
        // TODO Auto-generated method stub
         try{

                mBitmap.eraseColor(android.graphics.Color.TRANSPARENT);
                Canvas Canvas=new Canvas(mBitmap);

                action=true;
            myview.onDraw(Canvas);

                }catch(IllegalStateException ie){
                    ie.printStackTrace();
                }
    }
}) ;

   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 Paint       mPaint;

    public void colorChanged(int color) {
        mPaint.setColor(color);
    }
    private Bitmap  mBitmap;
    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;
                 }

              if(mBitmap==null)
              {
                mBitmap= Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
              }

                mCanvas = new Canvas(mBitmap);


         }

          @Override
            protected void onDraw(Canvas canvas) {


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


               Paint paint1 = new Paint();
                paint1.setAntiAlias(true);
                paint1.setColor(Color.BLACK);
                paint1.setStrokeWidth(3);
                paint1.setStyle(Paint.Style.FILL);
                paint1.setTextSize(15);
                canvas.drawText("Practice writing the letter by tracing it over and over again . ", 110, 40, paint1);

                 Paint p1=new Paint();
                    p1.setColor(getResources().getColor(R.color.bcolor));

                    if(screendensity==160)
                    {
                        p1.setTextSize(630);
                    }
                    if(screendensity==240)
                    {
                        p1.setTextSize(650);
                    }
                    p1.setAntiAlias(true);
                    Typeface font = Typeface.createFromAsset(getAssets(), "font/KINDTRG.TTF");             
                    p1.setTypeface(font);            




                    float lefta=(int)(270*(screenWidth/1024));
                     float lefta1=(int)(290*(screenWidth/1024));
                    float lefta2=(int)(320*(screenWidth/1024));
                    float lefta3=(int)(340*(screenWidth/1024));
                    float lefta4=(int)(350*(screenWidth/1024));
                    float lefta5=(int)(370*(screenWidth/1024));
                    float lefta6=(int)(335*(screenWidth/1024));
                    float lefta7=(int)(300*(screenWidth/1024));
                    float lefta8=(int)(265*(screenWidth/1024));



                    float topa=(int)(505*(screenHeight/600));
                    float topa1=(int)(520*(screenHeight/600));
                    float topa2=(int)(510*(screenHeight/600));
                    float topa3=(int)(500*(screenHeight/600));
                    float topa4=(int)(495*(screenHeight/600));
                    float topa5=(int)(490*(screenHeight/600));



                    if(username==1)
                      {
                         canvas.drawText("A", lefta, topa, p1);
                      }
                        if(username==2)
                          {
                             canvas.drawText("B", lefta, topa, p1);

                          }
                        if(username==3)
                        {
                            canvas.drawText("C", lefta, topa, p1);
                        }
                        if(username==4)
                        {
                            canvas.drawText("D", lefta, topa1, p1);
                        }
                        if(username==5)
                        {
                            canvas.drawText("E", lefta3, topa1, p1);
                        }


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

                canvas.drawPath(mPath, mPaint); 

    if(action)
     {
 invalidate();
     }

     }  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;
}
    }
    public void clearAllResources() {

        // Set related variables null

        System.gc();
        Runtime.getRuntime().gc();
       }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();


    }

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

        unbindDrawables(findViewById(R.id.relative));
        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();
        }
    }





    protected void onstop() {
          clearAllResources();


          super.onStop();
      }


       class SampleView extends View {
           java.io.InputStream is;


            private Movie mMovie;
            private long mMovieStart;

            private  byte[] streamToBytes(InputStream is) {
                ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
                byte[] buffer = new byte[1024];
                int len;
                try {
                    while ((len = is.read(buffer)) >= 0) {
                        os.write(buffer, 0, len);
                    }
                } catch (java.io.IOException e) {
                }
                return os.toByteArray();
            }

            public SampleView(Context context) {
                super(context);
                setFocusable(true);








                if(global.bugcount==1)
                {

                is = context.getResources().openRawResource(R.drawable.a);
                }

                if(global.bugcount==2)
                    {

                          is = context.getResources().openRawResource(R.drawable.b);

                    }
                    if(global.bugcount==3)
                    {

                          is = context.getResources().openRawResource(R.drawable.c);

                    }
                    if(global.bugcount==4)
                    {

                          is =  context.getResources().openRawResource(R.drawable.d);

                    }
                    if(global.bugcount==5)
                    {

                          is =   context.getResources().openRawResource(R.drawable.e);

                    }


                if (true) {
                    mMovie = Movie.decodeStream(is);
                } else {
                    byte[] array = streamToBytes(is);
                    mMovie = Movie.decodeByteArray(array, 0, array.length);
                }
            }

            @Override protected void onDraw(Canvas canvas) {


                Paint p = new Paint();
                p.setAntiAlias(true);



                long now = android.os.SystemClock.uptimeMillis();
                if (mMovieStart == 0) {   // first time
                    mMovieStart = now;
                }
                if (mMovie != null) {
                    int dur = mMovie.duration();
                    if (dur == 0) {
                        dur = 1000;
                    }
                    int relTime = (int)((now - mMovieStart) % dur);
                    mMovie.setTime(relTime);
                    mMovie.draw(canvas, getWidth() - mMovie.width(),
                                getHeight() - mMovie.height());
                    invalidate();
                }
            }
        }



      @Override
        protected void onPause() {

            if (mediay2 != null){
                mediay2.stop();
                mediay2.release();
                mediay2=null;
          }


            if(nextsound != null) {
                 if (nextsound.isPlaying()) {

                     nextsound.stop();

                 }
                 nextsound.release();
                 nextsound = null;

            }

            clearAllResources();

            super.onPause();

        }

      }

    i am getting exception in inside myview on sizechnage()....

1 个答案:

答案 0 :(得分:0)

尝试加载配置和使用BitmapsOptions的图像 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html