最近我已经开始在这个应用程序上工作它的作用是在屏幕上显示一个小操纵杆,当你按下箭头到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;
}
}
答案 0 :(得分:0)
它可能根本不是内存问题,请尝试在调试模式下连接手机并检查日志。它将为您提供真实问题的清晰画面。