Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的android应用程序这么慢?_Java_Android_Performance_Lag - Fatal编程技术网

Java 为什么我的android应用程序这么慢?

Java 为什么我的android应用程序这么慢?,java,android,performance,lag,Java,Android,Performance,Lag,我一直在学习教程。我一直在玩弄代码,去掉我不需要的东西,添加东西等等,但是当我在模拟器中运行它时,块会以非常不连贯的方式移动。就好像我的FPS很低。在运行应用程序的第一秒钟左右,它会平稳运行,然后在关闭前重新编译它时,它会再次平稳运行大约一秒钟。你知道如何让它一直平稳运行吗 MainActivity.java package com.example.mobilecoursework; import android.app.Activity; import android.os.Bundle;

我一直在学习教程。我一直在玩弄代码,去掉我不需要的东西,添加东西等等,但是当我在模拟器中运行它时,块会以非常不连贯的方式移动。就好像我的FPS很低。在运行应用程序的第一秒钟左右,它会平稳运行,然后在关闭前重新编译它时,它会再次平稳运行大约一秒钟。你知道如何让它一直平稳运行吗

MainActivity.java

package com.example.mobilecoursework;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new Blocks(this));
    }
}
blocks.java

package com.example.mobilecoursework;

import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
 import android.view.SurfaceView;


 class Blocks extends SurfaceView implements SurfaceHolder.Callback {
    //
    private BlockThread _thread;
    //declare an array of the block sprites 
    private ArrayList<GraphicObject> _graphics = new ArrayList<GraphicObject>();


    public Blocks(Context context) {
        super(context);
        getHolder().addCallback(this);
        _thread = new BlockThread(getHolder(), this);
        setFocusable(true);
    }



    public void DrawBlocks(){
        for (int i=0; i < 20; i++)
        {
            //create new random x and y position values
            Random randX = new Random();
            int i1=randX.nextInt(getWidth()-0) + 0;
            Random randY = new Random();
            int i2=randY.nextInt(getHeight()-0) + 0;
            GraphicObject graphic = new GraphicObject(BitmapFactory.decodeResource(getResources(), R.drawable.block));
            graphic.getCoordinates().setX((int) i1 - graphic.getGraphic().getWidth() / 2);
            graphic.getCoordinates().setY((int) i2 - graphic.getGraphic().getWidth() / 2);
            _graphics.add(graphic);
        }
    }

    public void updatePhysics() {
        GraphicObject.Coordinates coord;
        for (GraphicObject graphic : _graphics) {
            //move blocks down
            coord = graphic.getCoordinates();
            coord.setY(coord.getY() + 5 );                
            // reset block
            if (coord.getY() + graphic.getGraphic().getHeight() > getHeight()+10) {
                coord.setY(-10);
            }
        }
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        Bitmap bitmap;
        GraphicObject.Coordinates coords;
        for (GraphicObject graphic : _graphics) {
            bitmap = graphic.getGraphic();
            coords = graphic.getCoordinates();
            canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        _thread.setRunning(true);
        _thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // simply copied from sample application LunarLander:
        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        _thread.setRunning(false);
        while (retry) {
            try {
                _thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }
}

class BlockThread extends Thread {
    private SurfaceHolder _surfaceHolder;
    private Blocks _Blocks;
    private boolean _run = false;

    public BlockThread(SurfaceHolder surfaceHolder, Blocks blocks) {
        _surfaceHolder = surfaceHolder;
        _Blocks = blocks;
    }

    public void setRunning(boolean run) {
        _run = run;
    }

    public SurfaceHolder getSurfaceHolder() {
        return _surfaceHolder;
    }

    @Override
    public void run() {
        Canvas c;
        _Blocks.DrawBlocks();

        while (_run) {
            c = null;
            try {
                c = _surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {
                    _Blocks.updatePhysics();
                    _Blocks.onDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    _surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}

class GraphicObject {
    //
     // Contains the coordinates of the graphic.
     //
    public class Coordinates {
        private int _x = 100;
        private int _y = 0;

        public int getX() {
            return _x + _bitmap.getWidth() / 2;
        }

        public void setX(int value) {
            _x = value - _bitmap.getWidth() / 2;
        }

        public int getY() {
            return _y + _bitmap.getHeight() / 2;
        }

        public void setY(int value) {
            _y = value - _bitmap.getHeight() / 2;
        }

        public String toString() {
            return "Coordinates: (" + _x + "/" + _y + ")";
        }
    }

    private Bitmap _bitmap;
    private Coordinates _coordinates;


    public GraphicObject(Bitmap bitmap) {
        _bitmap = bitmap;
        _coordinates = new Coordinates();

    }

    public Bitmap getGraphic() {
        return _bitmap;
    }



    public Coordinates getCoordinates() {
        return _coordinates;
    }
}
package com.example.mobilecoursework;
导入java.util.ArrayList;
导入java.util.Random;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.view.SurfaceHolder;
导入android.view.SurfaceView;
类块扩展了SurfaceView实现了SurfaceHolder.Callback{
//
私有块线程\u线程;
//声明块精灵的数组
private ArrayList_graphics=new ArrayList();
公共块(上下文){
超级(上下文);
getHolder().addCallback(此);
_线程=新的块线程(getHolder(),this);
设置聚焦(真);
}
公用抽油机(){
对于(int i=0;i<20;i++)
{
//创建新的随机x和y位置值
Random randX=新的Random();
inti1=randX.nextInt(getWidth()-0)+0;
随机随机数=新随机数();
inti2=randY.nextInt(getHeight()-0)+0;
GraphicObject graphic=新的GraphicObject(BitmapFactory.decodeResource(getResources(),R.drawable.block));
graphic.getCoordinates().setX((int)i1-graphic.getGraphic().getWidth()/2);
graphic.getCoordinates().setY((int)i2-graphic.getGraphic().getWidth()/2);
_图形。添加(图形);
}
}
公共物理学(){
坐标坐标坐标;
用于(图形对象图形:_图形){
//向下移动块
coord=graphic.getCoordinates();
坐标setY(坐标getY()+5);
//复位块
如果(坐标.getY()+graphic.getGraphic().getHeight()>getHeight()+10){
坐标系-10;
}
}
}
@凌驾
公共空白onDraw(画布){
画布。drawColor(颜色。黑色);
位图;
坐标坐标系;
用于(图形对象图形:_图形){
位图=graphic.getGraphic();
coords=graphic.getCoordinates();
drawBitmap(位图,coords.getX(),coords.getY(),null);
}
}
@凌驾
公共空白表面更改(表面文件夹持有者、整型格式、整型宽度、整型高度){
//TODO自动生成的方法存根
}
@凌驾
已创建的公共空白表面(表面持有人){
_thread.setRunning(true);
_thread.start();
}
@凌驾
公共空间表面覆盖(表面覆盖物持有人){
//仅从示例应用程序LunarLander复制:
//我们必须告诉线程关闭&等待它完成,否则
//它可能会在我们返回并爆炸后接触到表面
布尔重试=真;
_thread.setRunning(false);
while(重试){
试一试{
_thread.join();
重试=错误;
}捕捉(中断异常e){
//我们会一次又一次的尝试。。。
}
}
}
}
类BlockThread扩展线程{
私人表面股东(SurfaceHolder);;
私人大厦(u座),;
私有布尔值_run=false;
公共块线程(表面文件夹表面文件夹,块){
_surfaceHolder=surfaceHolder;
_块=块;
}
公共void setRunning(布尔运行){
_跑=跑;
}
publicssurfaceholder getSurfaceHolder(){
返回(surfaceHolder);;
}
@凌驾
公开募捐{
帆布c;
_Blocks.DrawBlocks();
while(_run){
c=零;
试一试{
c=_surfaceHolder.lockCanvas(null);
已同步(_surfaceHolder){
_Blocks.updatePhysics();
_(c)段;
}
}最后{
//在finally中执行此操作,以便在引发异常时
//在上述过程中,我们不会将曲面保留在
//不一致状态
如果(c!=null){
_打开CanvasandPost(c)的锁;
}
}
}
}
}
类GraphicObject{
//
//包含图形的坐标。
//
公共类坐标{
私有整数x=100;
私有整数_y=0;
公共int getX(){
返回x+bitmap.getWidth()/2;
}
公共void setX(int值){
_x=value-\u bitmap.getWidth()/2;
}
公共int getY(){
返回_y+_bitmap.getHeight()/2;
}
公共void setY(int值){
_y=value-_bitmap.getHeight()/2;
}
公共字符串toString(){
返回“坐标:(“++ux+”/“++uy+”);
}
}
私有位图;
私有坐标_坐标;
公共图形对象(位图){
_位图=位图;
_坐标=新坐标();
}
公共位图getGraphic(){
返回位图;
}
公共坐标getCoordinates(){
返回_坐标;
}
}

模拟器速度非常慢,可能是您出现问题的原因。您的代码不会跳出任何内容,因此请在真正的设备上试用,然后再将头发撕下来,使其更平滑。

您在真正的设备上测试过吗?+1000。永远不要相信模拟器会给你真实的世界