Search in sources :

Example 6 with ViewOverlay

use of android.view.ViewOverlay in project Transitions-Everywhere by andkulikov.

the class Crossfade method createAnimator.

@Nullable
@Override
public Animator createAnimator(@NonNull ViewGroup sceneRoot, @Nullable TransitionValues startValues, @Nullable TransitionValues endValues) {
    if (startValues == null || endValues == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
        return null;
    }
    if (sRectEvaluator == null) {
        sRectEvaluator = new RectEvaluator();
    }
    final boolean useParentOverlay = mFadeBehavior != FADE_BEHAVIOR_REVEAL;
    final View view = endValues.view;
    Map<String, Object> startVals = startValues.values;
    Map<String, Object> endVals = endValues.values;
    Rect startBounds = (Rect) startVals.get(PROPNAME_BOUNDS);
    Rect endBounds = (Rect) endVals.get(PROPNAME_BOUNDS);
    if (startBounds == null || endBounds == null) {
        return null;
    }
    Bitmap startBitmap = (Bitmap) startVals.get(PROPNAME_BITMAP);
    Bitmap endBitmap = (Bitmap) endVals.get(PROPNAME_BITMAP);
    final BitmapDrawable startDrawable = (BitmapDrawable) startVals.get(PROPNAME_DRAWABLE);
    final BitmapDrawable endDrawable = (BitmapDrawable) endVals.get(PROPNAME_DRAWABLE);
    if (startDrawable != null && endDrawable != null && !startBitmap.sameAs(endBitmap)) {
        ViewOverlay overlay = useParentOverlay ? ((ViewGroup) view.getParent()).getOverlay() : view.getOverlay();
        if (mFadeBehavior == FADE_BEHAVIOR_REVEAL) {
            overlay.add(endDrawable);
        }
        overlay.add(startDrawable);
        // The transition works by placing the end drawable under the start drawable and
        // gradually fading out the start drawable. So it's not really a cross-fade, but rather
        // a reveal of the end scene over time. Also, animate the bounds of both drawables
        // to mimic the change in the size of the view itself between scenes.
        ObjectAnimator anim;
        if (mFadeBehavior == FADE_BEHAVIOR_OUT_IN) {
            // Fade out completely halfway through the transition
            anim = ObjectAnimator.ofInt(startDrawable, "alpha", 255, 0, 0);
        } else {
            anim = ObjectAnimator.ofInt(startDrawable, "alpha", 0);
        }
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // TODO: some way to auto-invalidate views based on drawable changes? callbacks?
                view.invalidate(startDrawable.getBounds());
            }
        });
        ObjectAnimator anim1 = null;
        if (mFadeBehavior == FADE_BEHAVIOR_OUT_IN) {
            // start fading in halfway through the transition
            anim1 = ObjectAnimator.ofFloat(view, View.ALPHA, 0, 0, 1);
        } else if (mFadeBehavior == FADE_BEHAVIOR_CROSSFADE) {
            anim1 = ObjectAnimator.ofFloat(view, View.ALPHA, 0, 1);
        }
        anim.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                ViewOverlay overlay = useParentOverlay ? ((ViewGroup) view.getParent()).getOverlay() : view.getOverlay();
                overlay.remove(startDrawable);
                if (mFadeBehavior == FADE_BEHAVIOR_REVEAL) {
                    overlay.remove(endDrawable);
                }
            }
        });
        AnimatorSet set = new AnimatorSet();
        set.playTogether(anim);
        if (anim1 != null) {
            set.playTogether(anim1);
        }
        if (mResizeBehavior == RESIZE_BEHAVIOR_SCALE && !startBounds.equals(endBounds)) {
            Animator anim2 = ObjectAnimator.ofObject(startDrawable, "bounds", sRectEvaluator, startBounds, endBounds);
            set.playTogether(anim2);
            if (mResizeBehavior == RESIZE_BEHAVIOR_SCALE) {
                // TODO: How to handle resizing with a CROSSFADE (vs. REVEAL) effect
                // when we are animating the view directly?
                Animator anim3 = ObjectAnimator.ofObject(endDrawable, "bounds", sRectEvaluator, startBounds, endBounds);
                set.playTogether(anim3);
            }
        }
        return set;
    } else {
        return null;
    }
}
Also used : ViewOverlay(android.view.ViewOverlay) RectEvaluator(android.animation.RectEvaluator) Rect(android.graphics.Rect) ObjectAnimator(android.animation.ObjectAnimator) ViewGroup(android.view.ViewGroup) AnimatorSet(android.animation.AnimatorSet) BitmapDrawable(android.graphics.drawable.BitmapDrawable) ValueAnimator(android.animation.ValueAnimator) SurfaceView(android.view.SurfaceView) TextureView(android.view.TextureView) View(android.view.View) Bitmap(android.graphics.Bitmap) ObjectAnimator(android.animation.ObjectAnimator) Animator(android.animation.Animator) ValueAnimator(android.animation.ValueAnimator) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) Nullable(androidx.annotation.Nullable)

Example 7 with ViewOverlay

use of android.view.ViewOverlay in project platform_frameworks_base by android.

the class Crossfade method createAnimator.

@Override
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
    if (startValues == null || endValues == null) {
        return null;
    }
    final boolean useParentOverlay = mFadeBehavior != FADE_BEHAVIOR_REVEAL;
    final View view = endValues.view;
    Map<String, Object> startVals = startValues.values;
    Map<String, Object> endVals = endValues.values;
    Rect startBounds = (Rect) startVals.get(PROPNAME_BOUNDS);
    Rect endBounds = (Rect) endVals.get(PROPNAME_BOUNDS);
    Bitmap startBitmap = (Bitmap) startVals.get(PROPNAME_BITMAP);
    Bitmap endBitmap = (Bitmap) endVals.get(PROPNAME_BITMAP);
    final BitmapDrawable startDrawable = (BitmapDrawable) startVals.get(PROPNAME_DRAWABLE);
    final BitmapDrawable endDrawable = (BitmapDrawable) endVals.get(PROPNAME_DRAWABLE);
    if (Transition.DBG) {
        Log.d(LOG_TAG, "StartBitmap.sameAs(endBitmap) = " + startBitmap.sameAs(endBitmap) + " for start, end: " + startBitmap + ", " + endBitmap);
    }
    if (startDrawable != null && endDrawable != null && !startBitmap.sameAs(endBitmap)) {
        ViewOverlay overlay = useParentOverlay ? ((ViewGroup) view.getParent()).getOverlay() : view.getOverlay();
        if (mFadeBehavior == FADE_BEHAVIOR_REVEAL) {
            overlay.add(endDrawable);
        }
        overlay.add(startDrawable);
        // The transition works by placing the end drawable under the start drawable and
        // gradually fading out the start drawable. So it's not really a cross-fade, but rather
        // a reveal of the end scene over time. Also, animate the bounds of both drawables
        // to mimic the change in the size of the view itself between scenes.
        ObjectAnimator anim;
        if (mFadeBehavior == FADE_BEHAVIOR_OUT_IN) {
            // Fade out completely halfway through the transition
            anim = ObjectAnimator.ofInt(startDrawable, "alpha", 255, 0, 0);
        } else {
            anim = ObjectAnimator.ofInt(startDrawable, "alpha", 0);
        }
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // TODO: some way to auto-invalidate views based on drawable changes? callbacks?
                view.invalidate(startDrawable.getBounds());
            }
        });
        ObjectAnimator anim1 = null;
        if (mFadeBehavior == FADE_BEHAVIOR_OUT_IN) {
            // start fading in halfway through the transition
            anim1 = ObjectAnimator.ofFloat(view, View.ALPHA, 0, 0, 1);
        } else if (mFadeBehavior == FADE_BEHAVIOR_CROSSFADE) {
            anim1 = ObjectAnimator.ofFloat(view, View.ALPHA, 0, 1);
        }
        if (Transition.DBG) {
            Log.d(LOG_TAG, "Crossfade: created anim " + anim + " for start, end values " + startValues + ", " + endValues);
        }
        anim.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                ViewOverlay overlay = useParentOverlay ? ((ViewGroup) view.getParent()).getOverlay() : view.getOverlay();
                overlay.remove(startDrawable);
                if (mFadeBehavior == FADE_BEHAVIOR_REVEAL) {
                    overlay.remove(endDrawable);
                }
            }
        });
        AnimatorSet set = new AnimatorSet();
        set.playTogether(anim);
        if (anim1 != null) {
            set.playTogether(anim1);
        }
        if (mResizeBehavior == RESIZE_BEHAVIOR_SCALE && !startBounds.equals(endBounds)) {
            if (Transition.DBG) {
                Log.d(LOG_TAG, "animating from startBounds to endBounds: " + startBounds + ", " + endBounds);
            }
            Animator anim2 = ObjectAnimator.ofObject(startDrawable, "bounds", sRectEvaluator, startBounds, endBounds);
            set.playTogether(anim2);
            if (mResizeBehavior == RESIZE_BEHAVIOR_SCALE) {
                // TODO: How to handle resizing with a CROSSFADE (vs. REVEAL) effect
                // when we are animating the view directly?
                Animator anim3 = ObjectAnimator.ofObject(endDrawable, "bounds", sRectEvaluator, startBounds, endBounds);
                set.playTogether(anim3);
            }
        }
        return set;
    } else {
        return null;
    }
}
Also used : ViewOverlay(android.view.ViewOverlay) Rect(android.graphics.Rect) ObjectAnimator(android.animation.ObjectAnimator) ViewGroup(android.view.ViewGroup) AnimatorSet(android.animation.AnimatorSet) BitmapDrawable(android.graphics.drawable.BitmapDrawable) ValueAnimator(android.animation.ValueAnimator) SurfaceView(android.view.SurfaceView) TextureView(android.view.TextureView) View(android.view.View) Bitmap(android.graphics.Bitmap) ObjectAnimator(android.animation.ObjectAnimator) Animator(android.animation.Animator) ValueAnimator(android.animation.ValueAnimator) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter)

Aggregations

View (android.view.View)7 ViewOverlay (android.view.ViewOverlay)7 Animator (android.animation.Animator)6 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)6 AnimatorSet (android.animation.AnimatorSet)6 ObjectAnimator (android.animation.ObjectAnimator)6 ValueAnimator (android.animation.ValueAnimator)6 Bitmap (android.graphics.Bitmap)6 Rect (android.graphics.Rect)6 BitmapDrawable (android.graphics.drawable.BitmapDrawable)6 SurfaceView (android.view.SurfaceView)6 TextureView (android.view.TextureView)6 ViewGroup (android.view.ViewGroup)6 RectEvaluator (android.animation.RectEvaluator)1 Nullable (androidx.annotation.Nullable)1 JSONObject (com.alibaba.fastjson.JSONObject)1 IWXObject (com.taobao.weex.common.IWXObject)1 ImmutableDomObject (com.taobao.weex.dom.ImmutableDomObject)1 WXDomObject (com.taobao.weex.dom.WXDomObject)1