Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3c0befde authored by nicolasroard's avatar nicolasroard
Browse files

Fix rotation anims on slow device (svelte)

Change-Id: If9050f7a17d9e3964d6bf35d649567c222f7fefe
parent 80b216d5
Loading
Loading
Loading
Loading
+23 −10
Original line number Diff line number Diff line
@@ -101,6 +101,7 @@ public class ImageShow extends View implements OnGestureListener,
    private static Bitmap sMask;
    private Paint mMaskPaint = new Paint();
    private Matrix mShaderMatrix = new Matrix();
    private boolean mDidStartAnimation = false;

    private static Bitmap convertToAlphaMask(Bitmap b) {
        Bitmap a = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ALPHA_8);
@@ -302,7 +303,7 @@ public class ImageShow extends View implements OnGestureListener,
            return;
        }

        Rect d = computeImageBounds(image);
        Rect d = computeImageBounds(image.getWidth(), image.getHeight());

        if (updateBounds) {
            mImageBounds = d;
@@ -313,12 +314,14 @@ public class ImageShow extends View implements OnGestureListener,

        MasterImage master = MasterImage.getImage();
        canvas.save();
        if (master.onGoingNewLookAnimation()) {
        if (master.onGoingNewLookAnimation()
                || mDidStartAnimation) {
            mDidStartAnimation = true;
            if (master.getCurrentLookAnimation()
                    == MasterImage.CIRCLE_ANIMATION
                    && MasterImage.getImage().getPreviousImage() != null) {
                float maskScale = MasterImage.getImage().getMaskScale();
                if (maskScale > 1.0f) {
                if (maskScale > 0.0f) {
                    float maskW = sMask.getWidth() / 2.0f;
                    float maskH = sMask.getHeight() / 2.0f;
                    float x = centerX - maskW * maskScale;
@@ -344,8 +347,11 @@ public class ImageShow extends View implements OnGestureListener,
                }
            } else if (master.getCurrentLookAnimation()
                    == MasterImage.ROTATE_ANIMATION) {
                Rect d2 = computeImageBounds(master.getPreviousImage());
                float finalScale = d.width() / (float) d2.height();
                Rect d1 = computeImageBounds(master.getPreviousImage().getHeight(),
                        master.getPreviousImage().getWidth());
                Rect d2 = computeImageBounds(master.getPreviousImage().getWidth(),
                        master.getPreviousImage().getHeight());
                float finalScale = d1.width() / (float) d2.height();
                finalScale = (1.0f * (1.0f - master.getAnimFraction()))
                        + (finalScale * master.getAnimFraction());
                canvas.rotate(master.getAnimRotationValue(), centerX, centerY);
@@ -391,11 +397,18 @@ public class ImageShow extends View implements OnGestureListener,
        } else {
            drawImage(canvas, image);
        }

        if (!master.onGoingNewLookAnimation()
                && mDidStartAnimation
                && !master.getPreviousPreset().equals(master.getCurrentPreset())) {
            mDidStartAnimation = false;
            MasterImage.getImage().resetAnimBitmap();
        }
        canvas.restore();
    }

    private void drawImage(Canvas canvas, Bitmap image) {
        Rect d = computeImageBounds(image);
        Rect d = computeImageBounds(image.getWidth(), image.getHeight());
        float scaleImageX = d.width() / (float) image.getWidth();
        float scaleImageY = d.height() / (float) image.getHeight();
        Matrix imageMatrix = new Matrix();
@@ -406,12 +419,12 @@ public class ImageShow extends View implements OnGestureListener,
        canvas.drawBitmap(image, imageMatrix, mPaint);
    }

    private Rect computeImageBounds(Bitmap image) {
        float scale = GeometryMathUtils.scale(image.getWidth(), image.getHeight(),
    private Rect computeImageBounds(int imageWidth, int imageHeight) {
        float scale = GeometryMathUtils.scale(imageWidth, imageHeight,
                getWidth(), getHeight());

        float w = image.getWidth() * scale;
        float h = image.getHeight() * scale;
        float w = imageWidth * scale;
        float h = imageHeight * scale;
        float ty = (getHeight() - h) / 2.0f;
        float tx = (getWidth() - w) / 2.0f;
        return new Rect((int) tx + mShadowMargin,
+37 −18
Original line number Diff line number Diff line
@@ -24,9 +24,6 @@ import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.android.gallery3d.exif.ExifTag;
import com.android.gallery3d.filtershow.FilterShowActivity;
@@ -84,10 +81,13 @@ public class MasterImage implements RenderingRequestCaller {
    private Bitmap mPartialBitmap = null;
    private Bitmap mHighresBitmap = null;
    private Bitmap mPreviousImage = null;
    private ImagePreset mPreviousPreset = null;

    private ValueAnimator mAnimator = null;
    private float mMaskScale = 1;
    private boolean mOnGoingNewLookAnimation = false;
    private float mAnimRotationValue = 0;
    private float mCurrentAnimRotationStartValue = 0;
    private float mAnimFraction = 0;
    private int mCurrentLookAnimation = 0;
    public static final int CIRCLE_ANIMATION = 1;
@@ -358,6 +358,14 @@ public class MasterImage implements RenderingRequestCaller {
        return mPreviousImage;
    }

    public ImagePreset getPreviousPreset() {
        return mPreviousPreset;
    }

    public ImagePreset getCurrentPreset() {
        return getPreviewBuffer().getConsumer().getPreset();
    }

    public float getMaskScale() {
        return mMaskScale;
    }
@@ -372,7 +380,7 @@ public class MasterImage implements RenderingRequestCaller {
    }

    public void setAnimRotation(float rotation) {
        mAnimRotationValue = rotation;
        mAnimRotationValue = mCurrentAnimRotationStartValue + rotation;
        notifyObservers();
    }

@@ -392,31 +400,44 @@ public class MasterImage implements RenderingRequestCaller {
        return mCurrentLookAnimation;
    }

    public void resetAnimBitmap() {
        mBitmapCache.cache(mPreviousImage);
        mPreviousImage = null;
    }

    public void onNewLook(FilterRepresentation newRepresentation) {
        getBitmapCache().cache(mPreviousImage);
        if (getFilteredImage() == null) {
            return;
        }
        if (mAnimator != null) {
            mAnimator.cancel();
            if (mCurrentLookAnimation == ROTATE_ANIMATION) {
                mCurrentAnimRotationStartValue += 90;
            }
        } else {
            mPreviousImage = getBitmapCache().getBitmapCopy(getFilteredImage());
        ValueAnimator animator = null;
        }
        mPreviousPreset = getPreviewBuffer().getConsumer().getPreset();
        if (newRepresentation instanceof FilterUserPresetRepresentation) {
            mCurrentLookAnimation = CIRCLE_ANIMATION;
            animator = ValueAnimator.ofFloat(1, 20);
            mAnimator = ValueAnimator.ofFloat(0, 20);
            mAnimator.setDuration(500);
        }
        if (newRepresentation instanceof FilterRotateRepresentation) {
            mCurrentLookAnimation = ROTATE_ANIMATION;
            animator = ValueAnimator.ofFloat(0, 90);
            mAnimator = ValueAnimator.ofFloat(0, 90);
            mAnimator.setDuration(500);
        }
        if (newRepresentation instanceof FilterMirrorRepresentation) {
            mCurrentLookAnimation = MIRROR_ANIMATION;
            animator = ValueAnimator.ofFloat(1, 0, -1);
            mAnimator = ValueAnimator.ofFloat(1, 0, -1);
            mAnimator.setDuration(500);
        }
        animator.setDuration(400);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                if (mCurrentLookAnimation == CIRCLE_ANIMATION) {
                    Log.v(LOGTAG, "circle animation " + animation.getAnimatedValue());
                    setMaskScale((Float) animation.getAnimatedValue());
                } else if (mCurrentLookAnimation == ROTATE_ANIMATION
                        || mCurrentLookAnimation == MIRROR_ANIMATION) {
@@ -425,7 +446,7 @@ public class MasterImage implements RenderingRequestCaller {
                }
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
        mAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                mOnGoingNewLookAnimation = true;
@@ -433,11 +454,9 @@ public class MasterImage implements RenderingRequestCaller {

            @Override
            public void onAnimationEnd(Animator animation) {
                mBitmapCache.cache(mPreviousImage);
                mPreviousImage = null;
                mOnGoingNewLookAnimation = false;
                setMaskScale(1);
                setAnimRotation(0);
                mCurrentAnimRotationStartValue = 0;
                mAnimator = null;
            }

            @Override
@@ -450,7 +469,7 @@ public class MasterImage implements RenderingRequestCaller {

            }
        });
        animator.start();
        mAnimator.start();
        notifyObservers();
    }