Java 如何减少我的android应用程序使用的内存量
最近我开始开发这个应用程序,它的作用是在屏幕上显示一个小操纵杆,当你按下箭头时,android图标会移动到它按下的位置 这在模拟器中运行良好,但只要我在手机上尝试,它就会立即崩溃 我猜我的程序正在使用内存,那么我该如何限制它呢? 谢谢Java 如何减少我的android应用程序使用的内存量,java,android,debugging,Java,Android,Debugging,最近我开始开发这个应用程序,它的作用是在屏幕上显示一个小操纵杆,当你按下箭头时,android图标会移动到它按下的位置 这在模拟器中运行良好,但只要我在手机上尝试,它就会立即崩溃 我猜我的程序正在使用内存,那么我该如何限制它呢? 谢谢 package net.jegard.software.zombie.main; 导入java.util.Timer; 导入java.util.TimerTask; 导入android.content.Context; 导入android.graphics.Bit
package net.jegard.software.zombie.main;
导入java.util.Timer;
导入java.util.TimerTask;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.view.MotionEvent;
导入android.view.SurfaceHolder;
导入android.view.SurfaceView;
公共类zombView扩展了SurfaceView{
私人位图,草,操纵杆;
私人土地持有人;
定时器t=新定时器();
浮动x=0,y=0;
布尔向前、左、下、右;
公共zombView(上下文){
超级(上下文);
holder=getHolder();
holder.addCallback(新的SurfaceHolder.Callback(){
@凌驾
公共空间表面覆盖(表面覆盖物持有人){
}
@凌驾
已处理的公共空隙表面(最终表面层持有人){
t、 scheduleAtFixedRate(新TimerTask(){
公开募捐{
Canvas c=holder.lockCanvas(空);
昂德劳(c);
支架。解锁帆布吊杆(c);
onTouchEvent(空);
}
},200,100);
}
@凌驾
公共无效表面更改(表面文件夹持有者,整数格式,
整数宽度,整数高度){
}
});
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.ic_启动器);
grass=BitmapFactory.decodeResource(getResources(),R.drawable.grades);
操纵杆=位图工厂.decodeResource(getResources(),R.drawable.joystic);
}
@凌驾
受保护的void onDraw(画布){
画布。drawColor(颜色。黑色);
drawBitmap(grass,getWidth()-getWidth(),getHeight()-getHeight(),null);
drawBitmap(操纵杆,getWidth()-getWidth(),操纵杆,getHeight(),null);
drawBitmap(bmp,x,y,null);
如果(右){
x=x+10;
}
如果(转发){
y=y-10;
}如果(左){
x=x-10;
}如果(向下){
y=y+10;
}
}
@凌驾
公共布尔onTouchEvent(运动事件){
开关(event.getAction()&MotionEvent.ACTION\u掩码){
case MotionEvent.ACTION\u DOWN:
float tx=event.getX();
float ty=event.getY();
如果(tx>(操纵杆.getWidth()/3)*2和&tx<(操纵杆.getWidth()/3)*3和&ty>(getHeight()-操纵杆.getHeight())+(操纵杆.getHeight()/3)和&ty<(getHeight()-操纵杆.getHeight())+(操纵杆.getHeight()/3)*2){
右触屏(tx、ty);
使无效();
}如果(tx>(操纵杆.getWidth()/3)和&tx<(操纵杆.getWidth()/3)*2和&ty>(操纵杆高度()-操纵杆高度())和&ty<(操纵杆高度()-操纵杆高度())+(操纵杆高度()/3)){
向前触摸(tx、ty);
使无效();
}如果(tx>(操纵杆.getWidth()-操纵杆.getWidth())和&tx<(操纵杆.getWidth()/3)和&ty>(操纵杆高度()-操纵杆高度())+(操纵杆高度()/3)和&ty<(操纵杆高度()-操纵杆高度())+(操纵杆高度()/3)*2){
触摸左(tx,ty);
使无效();
}如果(tx>(goodage.getWidth()/3)和&tx<(goodage.getWidth()/3)*2和&ty>((getHeight()-goodage.getHeight())+(goodage.getHeight()/3)*2)和&ty<(getHeight()-goodage.getHeight())+(goodle.getHeight()){
触地(tx、ty);
使无效();
}
打破
case MotionEvent.ACTION\u移动:
触摸移动();
使无效();
打破
case MotionEvent.ACTION\u UP:
润色;
使无效();
打破
}
返回true;
}
私有无效触摸屏右(浮动x2,浮动y2){
右=真;
}
私有无效触摸左(浮动x2,浮动y2){
左=真;
}
私人空间触地(浮动x2、浮动y2){
向下=真;
}
专用无效触摸屏向前(浮动x2,浮动y2){
向前=真;
}
私人空位触摸移动(){
润色;
}
私人空间修补(){
正向=假;
右=假;
向下=假;
左=假;
}
}
这可能根本不是内存问题,请尝试在调试模式下连接手机并检查日志。它会让你清楚地了解真正的问题。一如既往,任何时候当你在Android系统中遇到问题时,都可以从LogCat发布堆栈跟踪。在你的手机中,转到“设置->应用程序->开发->USB调试”启用它,然后在eclipse中转到DDMS视图并选择你的手机。
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;
}
}