0

我有一个在做梦时滚动 ImageView 层的动画,我希望它在屏幕的薄边滚动(即在 1280 x 720 屏幕上穿过 720)我得到一个显示变量,告诉我显示器是否处于横向旋转. 有时这不会使它反应正确,它会在错误的轴上滚动。我已经尝试以多种方式解决这个问题。包括拉动 ImageView 所在框架的尺寸。这仍然随机错误的宽度和高度。几乎感觉就像屏幕布局正确但轴位置被交换(在横向中,x 仍然是最短的边,y 是最长的)这是随机发生的,我不知道为什么。

private void initializeAlbumArtScroll() {

    albumArtwork.setX(0);
    albumArtwork.setY(0);

    mAnimator = albumArtwork.animate().x(0)
            .y(0)
            .setDuration(1)
            .setStartDelay(0)
            .setInterpolator(sInterpolator)
            .withEndAction(new Runnable() {
                @Override
                public void run() {
                    if (inPortrait) {
                        Log.i(TAG, "Scroll Portrait");
                        startAlbumArtLeftScroll();
                    } else {
                        Log.i(TAG, "Scroll Landscape");
                        startAlbumArtUpScroll();
                    }
                }
            });

    // Start the animation
    mAnimator.start();
}

/**
 * Album art scroll from the right of the screen to the left
 */
private void startAlbumArtLeftScroll() {
    albumArtwork.setX(0);
    albumArtwork.setY(0);

    mAnimator = albumArtwork.animate().x(-albumArtwork.getWidth() + screenWidth)
            .y(0)
            .setDuration(animationDuration)
            .setStartDelay(500)
            .setInterpolator(sInterpolator)
            .withEndAction(new Runnable() {
                @Override
                public void run() {
                    startAlbumArtRightScroll();
                }
            });

    // Start the animation
    mAnimator.start();
}

daydreamFrame.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    if ((display.getRotation() == Surface.ROTATION_0 || display.getRotation() == Surface.ROTATION_180) && !inPortrait) {
                        Log.i(TAG, "Now in portrait");
                        if (mAnimator != null) {
                            mAnimator.cancel();
                        }
                        inPortrait = true;
                        initializeAlbumArtScroll();
                    } else if ((display.getRotation() == Surface.ROTATION_270 || display.getRotation() == Surface.ROTATION_90) && inPortrait) {
                        Log.i(TAG, "Now in landscape");
                        if (mAnimator != null) {
                            mAnimator.cancel();
                        }
                        inPortrait = false;
                        initializeAlbumArtScroll();
                    }
                }
            });
4

0 回答 0