簡體   English   中英

Galaxy Nexus動畫速度慢

[英]Galaxy Nexus slow with animations

我編輯了這段代碼 ,將Rect實例移出onDraw方法。 我在幾台設備上測試過它。

public class BallBouncesActivity extends Activity {
    BallBounces ball;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ball = new BallBounces(this);
        setContentView(ball);
    }
}


class BallBounces extends SurfaceView implements SurfaceHolder.Callback {
    GameThread thread;
    int screenW; //Device's screen width.
    int screenH; //Devices's screen height.
    int ballX; //Ball x position.
    int ballY; //Ball y position.
    int initialY ;
    float dY; //Ball vertical speed.
    int ballW;
    int ballH;
    int bgrW;
    int bgrH;
    int angle;
    int bgrScroll;
    int dBgrY; //Background scroll speed.
    float acc;
    Bitmap ball, bgr, bgrReverse;
    boolean reverseBackroundFirst;
    boolean ballFingerMove;

    Rect toRect1, fromRect1;
    Rect toRect2, fromRect2;

    //Measure frames per second.
    long now;
    int framesCount=0;
    int framesCountAvg=0;
    long framesTimer=0;
    Paint fpsPaint=new Paint();

    //Frame speed
    long timeNow;
    long timePrev = 0;
    long timePrevFrame = 0;
    long timeDelta;


    public BallBounces(Context context) {
        super(context);
        ball = BitmapFactory.decodeResource(getResources(), R.drawable.rocket); //Load a ball image.
        bgr = BitmapFactory.decodeResource(getResources(), R.drawable.sky_bgr); //Load a background.
        ballW = ball.getWidth();
        ballH = ball.getHeight();

        toRect1 = new Rect(0, 0, bgr.getWidth(), bgr.getHeight());
        fromRect1 = new Rect(0, 0, bgr.getWidth(), bgr.getHeight());
        toRect2 = new Rect(0, 0, bgr.getWidth(), bgr.getHeight());
        fromRect2 = new Rect(0, 0, bgr.getWidth(), bgr.getHeight());

        //Create a flag for the onDraw method to alternate background with its mirror image.
        reverseBackroundFirst = false;

        //Initialise animation variables.
        acc = 0.2f; //Acceleration
        dY = 0; //vertical speed
        initialY = 100; //Initial vertical position
        angle = 0; //Start value for the rotation angle
        bgrScroll = 0;  //Background scroll position
        dBgrY = 1; //Scrolling background speed

        fpsPaint.setTextSize(30);

        //Set thread
        getHolder().addCallback(this);

        setFocusable(true);
    }

    @Override
    public void onSizeChanged (int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //This event-method provides the real dimensions of this custom view.
        screenW = w;
        screenH = h;

        bgr = Bitmap.createScaledBitmap(bgr, w, h, true); //Scale background to fit the screen.
        bgrW = bgr.getWidth();
        bgrH = bgr.getHeight();

        //Create a mirror image of the background (horizontal flip) - for a more circular background.
        Matrix matrix = new Matrix();  //Like a frame or mould for an image.
        matrix.setScale(-1, 1); //Horizontal mirror effect.
        bgrReverse = Bitmap.createBitmap(bgr, 0, 0, bgrW, bgrH, matrix, true); //Create a new mirrored bitmap by applying the matrix.

        ballX = (int) (screenW /2) - (ballW / 2) ; //Centre ball X into the centre of the screen.
        ballY = -50; //Centre ball height above the screen.
    }

    //***************************************
    //*************  TOUCH  *****************
    //***************************************
    @Override
    public synchronized boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                ballX = (int) ev.getX() - ballW/2;
                ballY = (int) ev.getY() - ballH/2;

                ballFingerMove = true;
                break;
            }

            case MotionEvent.ACTION_MOVE: {
                ballX = (int) ev.getX() - ballW/2;
                ballY = (int) ev.getY() - ballH/2;

                break;
            }

            case MotionEvent.ACTION_UP:
                ballFingerMove = false;
                dY = 0;
                break;
            }
        return true;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //Draw scrolling background.
        fromRect1.set(0, 0, bgrW - bgrScroll, bgrH);
        toRect1.set(bgrScroll, 0, bgrW, bgrH);

        fromRect2.set(bgrW - bgrScroll, 0, bgrW, bgrH);
        toRect2.set(0, 0, bgrScroll, bgrH);

//        Rect fromRect1 = new Rect(0, 0, bgrW - bgrScroll, bgrH);
//        Rect toRect1 = new Rect(bgrScroll, 0, bgrW, bgrH);
//
//        Rect fromRect2 = new Rect(bgrW - bgrScroll, 0, bgrW, bgrH);
//        Rect toRect2 = new Rect(0, 0, bgrScroll, bgrH);

        if (!reverseBackroundFirst) {
            canvas.drawBitmap(bgr, fromRect1, toRect1, null);
            canvas.drawBitmap(bgrReverse, fromRect2, toRect2, null);
        }
        else{
            canvas.drawBitmap(bgr, fromRect2, toRect2, null);
            canvas.drawBitmap(bgrReverse, fromRect1, toRect1, null);
        }

        //Next value for the background's position.
        if ( (bgrScroll += dBgrY) >= bgrW) {
            bgrScroll = 0;
            reverseBackroundFirst = !reverseBackroundFirst;
        }

        //Compute roughly the ball's speed and location.
        if (!ballFingerMove) {
            ballY += (int) dY; //Increase or decrease vertical position.
            if (ballY > (screenH - ballH)) {
                dY=(-1)*dY; //Reverse speed when bottom hit.
            }
            dY+= acc; //Increase or decrease speed.
        }

        //Increase rotating angle
        if (angle++ >360)
            angle =0;

        //DRAW BALL
        //Rotate method one
        /*
        Matrix matrix = new Matrix();
        matrix.postRotate(angle, (ballW / 2), (ballH / 2)); //Rotate it.
        matrix.postTranslate(ballX, ballY); //Move it into x, y position.
        canvas.drawBitmap(ball, matrix, null); //Draw the ball with applied matrix.

        */// Rotate method two

        canvas.save(); //Save the position of the canvas matrix.
        canvas.rotate(angle, ballX + (ballW / 2), ballY + (ballH / 2)); //Rotate the canvas matrix.
        canvas.drawBitmap(ball, ballX, ballY, null); //Draw the ball by applying the canvas rotated matrix.
        canvas.restore(); //Rotate the canvas matrix back to its saved position - only the ball bitmap was rotated not all canvas.

        //*/

        //Measure frame rate (unit: frames per second).
         now=System.currentTimeMillis();
         canvas.drawText(framesCountAvg+" fps", 40, 70, fpsPaint);
         framesCount++;
         if(now-framesTimer>1000) {
                 framesTimer=now;
                 framesCountAvg=framesCount;
                 framesCount=0;
         }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        thread = new GameThread(getHolder(), this);
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        thread.setRunning(false);
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {

            }
        }
    }


    class GameThread extends Thread {
        private SurfaceHolder surfaceHolder;
        private BallBounces gameView;
        private boolean run = false;

        public GameThread(SurfaceHolder surfaceHolder, BallBounces gameView) {
            this.surfaceHolder = surfaceHolder;
            this.gameView = gameView;
        }

        public void setRunning(boolean run) {
            this.run = run;
        }

        public SurfaceHolder getSurfaceHolder() {
            return surfaceHolder;
        }

        @Override
        public void run() {
            Canvas c;
            while (run) {
                c = null;

                //limit frame rate to max 60fps
                timeNow = System.currentTimeMillis();
                timeDelta = timeNow - timePrevFrame;
                if ( timeDelta < 16) {
                    try {
                        Thread.sleep(16 - timeDelta);
                    }
                    catch(InterruptedException e) {

                    }
                }
                timePrevFrame = System.currentTimeMillis();

                try {
                    c = surfaceHolder.lockCanvas(null);
                    synchronized (surfaceHolder) {
                       //call methods to draw and process next fame
                        gameView.onDraw(c);
                    }
                } finally {
                    if (c != null) {
                        surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }
}

如果你注意到,有測量幀速率的代碼:

     now=System.currentTimeMillis();
     canvas.drawText(framesCountAvg+" fps", 40, 70, fpsPaint);
     framesCount++;
     if(now-framesTimer>1000) {
             framesTimer=now;
             framesCountAvg=framesCount;
             framesCount=0;
     }

我在我的兩款Galaxy Nexus設備上看到了這一點,運行Android 4.0和4.2,它大約是22-24fps。 在我的HTC Desire上運行Android 2.2,它更像是60fps。

您還會注意到我沒有在onDraw方法中分配任何內容。 我也沒有創建新的Paint對象。 我實在看不出這是如何運行的話,那么,在我的Galaxy Nexus設備,以便慢得多。 有很多口吃,球移動得很慢。

有沒有人知道我可以撤消的設置或重新繪制Galaxy Nexus的已知問題? 這發生在運行4.0的Galaxy Nexus 運行4.2的Galaxy Nexus上,所以我不確定它是否是特定於操作系統的。 我已關閉窗口並在“開發者選項”中轉換動畫。 我是否強制2D加速並沒有什么區別。

你在應用程序中測試過android:hardwareAccelerated =“true | false”標志嗎?

Android Dev Doc: http//developer.android.com/guide/topics/graphics/hardware-accel.html

您可以在setContentView之前添加:

if (android.os.Build.VERSION.SDK_INT >= 11) {
   getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
}

你的targetSdk是什么設置的?

如果targetSdk設置為8(2.2),但您在4.x設備(15+)上運行它,則4.x設備將以兼容模式運行,這意味着它將虛擬化所有調用以便它們返回與它們在版本8設備上運行完全相同。

這種虛擬化可能解釋了一些緩慢的問題。 嘗試將targetSdk更改為17(對於您的4.2設備),看看它是否有所作為。

我一直在考慮這個問題,因為我的Nexus 10仍然存在表面問題,就像我之前說過的那樣,我的性能提升了很多我去除了油漆對象的使用,這是一個很長的鏡頭,因為你只是使用一個,但你可以嘗試從onDraw()中刪除文本繪圖部分,看看它是否對你的繪圖速度有任何影響。

除此之外,我認為這是一個試圖確定問題的案例。

我注意到即使我完全從等式中刪除了我的onDraw和Logic更新方法,它仍然需要長達25ms來鎖定和解鎖/發布畫布! 所以問題可能實際上並不在於onDraw方法 - 給它一個去,從Run()方法中注釋出來的對象,然后看看你得到的速度(使用Loglog來查看數字,記住具有諷刺意味的是,在屏幕上顯示一個幀/時間計數可能會影響你正在測量的東西。 :-)

我在Kindle Fire,Sony Xperia Z和三星S4(都是用android 4.2)上經歷了同樣的事情。

修復方法是:在App Manifest文件中刪除“android:supportsRtl =”true“”。

希望它能節省你的時間。 在我拿到它之前,我花了4個小時進行測試和合並。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM