Home | History | Annotate | Download | only in transition
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package androidx.transition;
     18 
     19 import android.graphics.Path;
     20 import android.graphics.PathMeasure;
     21 import android.graphics.PointF;
     22 import android.util.Property;
     23 
     24 /**
     25  * A special {@link Property} that can animate a pair of properties bi-dimensionally along the
     26  * specified path.
     27  * <p>
     28  * This property should always be used with Animator that sets float fractions between
     29  * {@code 0.f} and {@code 1.f}. For example, setting {@code 0.5f} to this property sets the
     30  * values right in the middle of the specified path to the underlying properties.
     31  * <p>
     32  * Unlike many of the platform built-in properties, instances of this class cannot be reused
     33  * for later animations.
     34  */
     35 class PathProperty<T> extends Property<T, Float> {
     36 
     37     private final Property<T, PointF> mProperty;
     38     private final PathMeasure mPathMeasure;
     39     private final float mPathLength;
     40     private final float[] mPosition = new float[2];
     41     private final PointF mPointF = new PointF();
     42     private float mCurrentFraction;
     43 
     44     PathProperty(Property<T, PointF> property, Path path) {
     45         super(Float.class, property.getName());
     46         mProperty = property;
     47         mPathMeasure = new PathMeasure(path, false);
     48         mPathLength = mPathMeasure.getLength();
     49     }
     50 
     51     @Override
     52     public Float get(T object) {
     53         return mCurrentFraction;
     54     }
     55 
     56     @Override
     57     public void set(T target, Float fraction) {
     58         mCurrentFraction = fraction;
     59         mPathMeasure.getPosTan(mPathLength * fraction, mPosition, null);
     60         mPointF.x = mPosition[0];
     61         mPointF.y = mPosition[1];
     62         mProperty.set(target, mPointF);
     63     }
     64 
     65 }
     66