2012-02-07 79 views
-1

最近我已經開始在這個程序上工作它是在屏幕上顯示一個小搖桿,當你按下箭頭到android圖標移動到它按下的位置如何減少我的Android應用程序使用的內存量

這在模擬器中正常工作,但只要我嘗試在我的手機上立即崩潰 即時猜測我的程序正在使用內存,所以我怎麼能限制這個? 感謝

package net.jegard.software.zombie.main; 

import java.util.Timer; 
import java.util.TimerTask; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

    public class zombView extends SurfaceView{ 
     private Bitmap bmp, grass, joystick; 
     private SurfaceHolder holder; 
     Timer t = new Timer(); 
     float x = 0, y = 0; 
     boolean forward, left, down, right; 
     public zombView(Context context) { 
       super(context); 
       holder = getHolder(); 
       holder.addCallback(new SurfaceHolder.Callback() { 

        @Override 
        public void surfaceDestroyed(SurfaceHolder holder) { 
        } 

        @Override 
        public void surfaceCreated(final SurfaceHolder holder) { 


         t.scheduleAtFixedRate(new TimerTask(){ 
          public void run(){ 
          Canvas c = holder.lockCanvas(null); 
          onDraw(c); 
          holder.unlockCanvasAndPost(c); 
          onTouchEvent(null); 
         } 
        },200,100); 
        } 
        @Override 
        public void surfaceChanged(SurfaceHolder holder, int format, 
            int width, int height) { 
        } 
       }); 
       bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
       grass = BitmapFactory.decodeResource(getResources(), R.drawable.grassland); 
       joystick = BitmapFactory.decodeResource(getResources(), R.drawable.joystic); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
       canvas.drawColor(Color.BLACK); 
       canvas.drawBitmap(grass, getWidth() - getWidth(), getHeight() - getHeight(), null); 
       canvas.drawBitmap(joystick, getWidth() - getWidth(),joystick.getHeight(), null); 
       canvas.drawBitmap(bmp, x, y, null); 
       if(right){ 
        x = x + 10; 
       } 
       if(forward){ 
       y = y - 10; 
       }if(left){ 
       x = x - 10; 
       }if(down){ 
       y = y + 10; 
       } 
     } 
     @Override 
     public boolean onTouchEvent(MotionEvent event) { 
      switch (event.getAction() & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_DOWN: 
       float tx = event.getX(); 
       float ty = event.getY(); 
       if(tx > (joystick.getWidth()/3)*2 && tx < (joystick.getWidth()/3)*3 && ty > (getHeight() - joystick.getHeight()) + (joystick.getHeight()/3) && ty < (getHeight() - joystick.getHeight()) + (joystick.getHeight()/3)*2){ 
         touch_right(tx, ty); 
         invalidate(); 
       }if(tx > (joystick.getWidth()/3) && tx < (joystick.getWidth()/3)*2 && ty > (getHeight() - joystick.getHeight()) && ty < (getHeight() - joystick.getHeight()) + (joystick.getHeight()/3)){ 
         touch_forward(tx, ty); 
         invalidate(); 
       }if(tx > (joystick.getWidth() - joystick.getWidth()) && tx < (joystick.getWidth()/3) && ty > (getHeight() - joystick.getHeight()) + (joystick.getHeight()/3) && ty < (getHeight() - joystick.getHeight()) + (joystick.getHeight()/3)*2){ 
        touch_left(tx, ty); 
        invalidate(); 
       }if(tx > (joystick.getWidth()/3) && tx < (joystick.getWidth()/3)*2 && ty > ((getHeight() - joystick.getHeight()) + (joystick.getHeight()/3)*2) && ty < (getHeight() - joystick.getHeight()) + (joystick.getHeight())){ 
        touch_down(tx, ty); 
        invalidate(); 
       } 
       break; 
      case MotionEvent.ACTION_MOVE: 
       touch_move(); 
       invalidate(); 
       break; 
      case MotionEvent.ACTION_UP: 
       touch_up(); 
       invalidate(); 
       break; 
      } 
      return true; 
     } 

     private void touch_right(float x2, float y2) { 
      right = true; 

     } 
     private void touch_left(float x2, float y2) { 
      left = true; 

     } 
     private void touch_down(float x2, float y2) { 
      down = true; 

     } 
     private void touch_forward(float x2, float y2) { 
      forward = true; 

     } 
     private void touch_move() { 
      touch_up(); 

     } 

     private void touch_up() { 
      forward = false; 
      right = false; 
      down = false; 
      left = false; 


     } 
    } 
+3

與往常一樣,如果您在Android中遇到了崩潰問題,請在LogCat中發佈堆棧跟蹤。 – kabuko 2012-02-07 21:56:28

回答

0

它可能不是內存問題在所有,嘗試連接你的手機在調試模式,並檢查日誌。它會給你一個真實問題的清晰畫面。

+0

我該怎麼做? – user1068310 2012-02-07 22:06:40

+0

在您的手機中,進入「設置 - >應用程序 - >開發 - > USB調試」啓用它,並在Eclipse中進入DDMS視圖並選擇您的手機。 – 2012-02-07 23:24:35

相關問題