I have an animation that scrolls an ImageView layer while dreaming and I want it to scroll across the thin side of the screen (ie on 1280 x 720 screen across 720) I get a display variable that tells me if the display is in a landscape rotation. Sometimes this doesn't make it react right and it will scroll on the wrong axis. I have tried solving this issue in many ways. Including pulling the dimensions of the frame the ImageView is on. This still randomly mistakes the width and height. It almost feels like though the screen is laid out properly it the axis positions are swapped (in landscape x is still the shortest side and y is the longest) This happens randomly and I can't figure out why.
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();
}
}
});