Home | History | Annotate | Download | only in animation
      1 /*
      2  * Copyright (C) 2006 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 android.view.animation;
     18 
     19 import android.graphics.Matrix;
     20 
     21 import java.io.PrintWriter;
     22 
     23 /**
     24  * Defines the transformation to be applied at
     25  * one point in time of an Animation.
     26  *
     27  */
     28 public class Transformation {
     29     /**
     30      * Indicates a transformation that has no effect (alpha = 1 and identity matrix.)
     31      */
     32     public static final int TYPE_IDENTITY = 0x0;
     33     /**
     34      * Indicates a transformation that applies an alpha only (uses an identity matrix.)
     35      */
     36     public static final int TYPE_ALPHA = 0x1;
     37     /**
     38      * Indicates a transformation that applies a matrix only (alpha = 1.)
     39      */
     40     public static final int TYPE_MATRIX = 0x2;
     41     /**
     42      * Indicates a transformation that applies an alpha and a matrix.
     43      */
     44     public static final int TYPE_BOTH = TYPE_ALPHA | TYPE_MATRIX;
     45 
     46     protected Matrix mMatrix;
     47     protected float mAlpha;
     48     protected int mTransformationType;
     49 
     50     /**
     51      * Creates a new transformation with alpha = 1 and the identity matrix.
     52      */
     53     public Transformation() {
     54         clear();
     55     }
     56 
     57     /**
     58      * Reset the transformation to a state that leaves the object
     59      * being animated in an unmodified state. The transformation type is
     60      * {@link #TYPE_BOTH} by default.
     61      */
     62     public void clear() {
     63         if (mMatrix == null) {
     64             mMatrix = new Matrix();
     65         } else {
     66             mMatrix.reset();
     67         }
     68         mAlpha = 1.0f;
     69         mTransformationType = TYPE_BOTH;
     70     }
     71 
     72     /**
     73      * Indicates the nature of this transformation.
     74      *
     75      * @return {@link #TYPE_ALPHA}, {@link #TYPE_MATRIX},
     76      *         {@link #TYPE_BOTH} or {@link #TYPE_IDENTITY}.
     77      */
     78     public int getTransformationType() {
     79         return mTransformationType;
     80     }
     81 
     82     /**
     83      * Sets the transformation type.
     84      *
     85      * @param transformationType One of {@link #TYPE_ALPHA},
     86      *        {@link #TYPE_MATRIX}, {@link #TYPE_BOTH} or
     87      *        {@link #TYPE_IDENTITY}.
     88      */
     89     public void setTransformationType(int transformationType) {
     90         mTransformationType = transformationType;
     91     }
     92 
     93     /**
     94      * Clones the specified transformation.
     95      *
     96      * @param t The transformation to clone.
     97      */
     98     public void set(Transformation t) {
     99         mAlpha = t.getAlpha();
    100         mMatrix.set(t.getMatrix());
    101         mTransformationType = t.getTransformationType();
    102     }
    103 
    104     /**
    105      * Apply this Transformation to an existing Transformation, e.g. apply
    106      * a scale effect to something that has already been rotated.
    107      * @param t
    108      */
    109     public void compose(Transformation t) {
    110         mAlpha *= t.getAlpha();
    111         mMatrix.preConcat(t.getMatrix());
    112     }
    113 
    114     /**
    115      * Like {@link #compose(Transformation)} but does this.postConcat(t) of
    116      * the transformation matrix.
    117      * @hide
    118      */
    119     public void postCompose(Transformation t) {
    120         mAlpha *= t.getAlpha();
    121         mMatrix.postConcat(t.getMatrix());
    122     }
    123 
    124     /**
    125      * @return The 3x3 Matrix representing the trnasformation to apply to the
    126      * coordinates of the object being animated
    127      */
    128     public Matrix getMatrix() {
    129         return mMatrix;
    130     }
    131 
    132     /**
    133      * Sets the degree of transparency
    134      * @param alpha 1.0 means fully opaqe and 0.0 means fully transparent
    135      */
    136     public void setAlpha(float alpha) {
    137         mAlpha = alpha;
    138     }
    139 
    140     /**
    141      * @return The degree of transparency
    142      */
    143     public float getAlpha() {
    144         return mAlpha;
    145     }
    146 
    147     @Override
    148     public String toString() {
    149         StringBuilder sb = new StringBuilder(64);
    150         sb.append("Transformation");
    151         toShortString(sb);
    152         return sb.toString();
    153     }
    154 
    155     /**
    156      * Return a string representation of the transformation in a compact form.
    157      */
    158     public String toShortString() {
    159         StringBuilder sb = new StringBuilder(64);
    160         toShortString(sb);
    161         return sb.toString();
    162     }
    163 
    164     /**
    165      * @hide
    166      */
    167     public void toShortString(StringBuilder sb) {
    168         sb.append("{alpha="); sb.append(mAlpha);
    169         sb.append(" matrix="); mMatrix.toShortString(sb);
    170         sb.append('}');
    171     }
    172 
    173     /**
    174      * Print short string, to optimize dumping.
    175      * @hide
    176      */
    177     public void printShortString(PrintWriter pw) {
    178         pw.print("{alpha="); pw.print(mAlpha);
    179         pw.print(" matrix=");
    180         mMatrix.printShortString(pw);
    181         pw.print('}');
    182     }
    183 }
    184