2012-02-07 83 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

相关问题