Android Open Source - transitions-everywhere Pattern Path Motion






From Project

Back to project page transitions-everywhere.

License

The source code is released under:

Apache License

If you think the Android project transitions-everywhere listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 The Android Open Source Project
 */*from w ww.ja v  a2 s . c om*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.transitions.everywhere;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Matrix;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.util.FloatMath;

/**
 * A PathMotion that takes a Path pattern and applies it to the separation between two points.
 * The starting point of the Path will be moved to the origin and the end point will be scaled
 * and rotated so that it matches with the target end point.
 * <p>This may be used in XML as an element inside a transition.</p>
 * <pre>
 * {@code
 * &lt;changeBounds>
 *     &lt;patternPathMotion android:patternPathData="M0 0 L0 100 L100 100"/>
 * &lt;/changeBounds>}
 * </pre>
 */
public class PatternPathMotion extends PathMotion {

    private Path mOriginalPatternPath;

    private final Path mPatternPath = new Path();

    private final Matrix mTempMatrix = new Matrix();

    /**
     * Constructs a PatternPathMotion with a straight-line pattern.
     */
    public PatternPathMotion() {
        mPatternPath.lineTo(1, 0);
        mOriginalPatternPath = mPatternPath;
    }

    public PatternPathMotion(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PatternPathMotion);
        try {
            String pathData = a.getString(R.styleable.PatternPathMotion_patternPathData);
            if (pathData == null) {
                throw new RuntimeException("pathData must be supplied for patternPathMotion");
            }
            Path pattern = PathParser.createPathFromPathData(pathData);
            setPatternPath(pattern);
        } finally {
            a.recycle();
        }

    }

    /**
     * Creates a PatternPathMotion with the Path defining a pattern of motion between two
     * coordinates. The pattern will be translated, rotated, and scaled to fit between the start
     * and end points. The pattern must not be empty and must have the end point differ from the
     * start point.
     *
     * @param patternPath A Path to be used as a pattern for two-dimensional motion.
     */
    public PatternPathMotion(Path patternPath) {
        setPatternPath(patternPath);
    }

    /**
     * Returns the Path defining a pattern of motion between two coordinates.
     * The pattern will be translated, rotated, and scaled to fit between the start and end points.
     * The pattern must not be empty and must have the end point differ from the start point.
     *
     * @return the Path defining a pattern of motion between two coordinates.
     * @attr ref android.R.styleable#PatternPathMotion_patternPathData
     */
    public Path getPatternPath() {
        return mOriginalPatternPath;
    }

    /**
     * Sets the Path defining a pattern of motion between two coordinates.
     * The pattern will be translated, rotated, and scaled to fit between the start and end points.
     * The pattern must not be empty and must have the end point differ from the start point.
     *
     * @param patternPath A Path to be used as a pattern for two-dimensional motion.
     * @attr ref android.R.styleable#PatternPathMotion_patternPathData
     */
    public void setPatternPath(Path patternPath) {
        PathMeasure pathMeasure = new PathMeasure(patternPath, false);
        float length = pathMeasure.getLength();
        float[] pos = new float[2];
        pathMeasure.getPosTan(length, pos, null);
        float endX = pos[0];
        float endY = pos[1];
        pathMeasure.getPosTan(0, pos, null);
        float startX = pos[0];
        float startY = pos[1];

        if (startX == endX && startY == endY) {
            throw new IllegalArgumentException("pattern must not end at the starting point");
        }

        mTempMatrix.setTranslate(-startX, -startY);
        float dx = endX - startX;
        float dy = endY - startY;
        float distance = distance(dx, dy);
        float scale = 1 / distance;
        mTempMatrix.postScale(scale, scale);
        double angle = Math.atan2(dy, dx);
        mTempMatrix.postRotate((float) Math.toDegrees(-angle));
        patternPath.transform(mTempMatrix, mPatternPath);
        mOriginalPatternPath = patternPath;
    }

    @Override
    public Path getPath(float startX, float startY, float endX, float endY) {
        float dx = endX - startX;
        float dy = endY - startY;
        float length = distance(dx, dy);
        double angle = Math.atan2(dy, dx);

        mTempMatrix.setScale(length, length);
        mTempMatrix.postRotate((float) Math.toDegrees(angle));
        mTempMatrix.postTranslate(startX, startY);
        Path path = new Path();
        mPatternPath.transform(mTempMatrix, path);
        return path;
    }

    private static float distance(float x, float y) {
        return FloatMath.sqrt((x * x) + (y * y));
    }
}




Java Source Code List

android.transitions.everywhere.ArcMotion.java
android.transitions.everywhere.AutoTransition.java
android.transitions.everywhere.ChangeBounds.java
android.transitions.everywhere.ChangeClipBounds.java
android.transitions.everywhere.ChangeImageTransform.java
android.transitions.everywhere.ChangeTransform.java
android.transitions.everywhere.CircularPropagation.java
android.transitions.everywhere.Explode.java
android.transitions.everywhere.Fade.java
android.transitions.everywhere.IntProperty.java
android.transitions.everywhere.PathMotion.java
android.transitions.everywhere.PathParser.java
android.transitions.everywhere.PatternPathMotion.java
android.transitions.everywhere.Scene.java
android.transitions.everywhere.SidePropagation.java
android.transitions.everywhere.Slide.java
android.transitions.everywhere.TransitionInflater.java
android.transitions.everywhere.TransitionManager.java
android.transitions.everywhere.TransitionPropagation.java
android.transitions.everywhere.TransitionSet.java
android.transitions.everywhere.TransitionUtils.java
android.transitions.everywhere.TransitionValuesMaps.java
android.transitions.everywhere.TransitionValues.java
android.transitions.everywhere.Transition.java
android.transitions.everywhere.TranslationAnimationCreator.java
android.transitions.everywhere.VisibilityPropagation.java
android.transitions.everywhere.Visibility.java
android.transitions.everywhere.hidden.ChangeScroll.java
android.transitions.everywhere.hidden.ChangeText.java
android.transitions.everywhere.hidden.Crossfade.java
android.transitions.everywhere.hidden.Recolor.java
android.transitions.everywhere.hidden.Rotate.java
android.transitions.everywhere.utils.AnimatorUtils.java
android.transitions.everywhere.utils.ArrayMap.java
android.transitions.everywhere.utils.ContainerHelpers.java
android.transitions.everywhere.utils.MapCollections.java
android.transitions.everywhere.utils.MatrixUtils.java
android.transitions.everywhere.utils.Objects.java
android.transitions.everywhere.utils.PropertyCompatObject.java
android.transitions.everywhere.utils.RectEvaluator.java
android.transitions.everywhere.utils.ReflectionUtils.java
android.transitions.everywhere.utils.ViewGroupOverlayUtils.java
android.transitions.everywhere.utils.ViewGroupUtilsJellyBeanMr2.java
android.transitions.everywhere.utils.ViewGroupUtils.java
android.transitions.everywhere.utils.ViewOverlayPreJellybean.java
android.transitions.everywhere.utils.ViewOverlayUtils.java
android.transitions.everywhere.utils.ViewUtilsKitKat.java
android.transitions.everywhere.utils.ViewUtilsLolipop.java
android.transitions.everywhere.utils.ViewUtils.java
com.github.andkulikov.transitions.everywhere.MainActivity.java