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 int TYPE_IDENTITY = 0x0;
     33     /**
     34      * Indicates a transformation that applies an alpha only (uses an identity matrix.)
     35      */
     36     public static int TYPE_ALPHA = 0x1;
     37     /**
     38      * Indicates a transformation that applies a matrix only (alpha = 1.)
     39      */
     40     public static int TYPE_MATRIX = 0x2;
     41     /**
     42      * Indicates a transformation that applies an alpha and a matrix.
     43      */
     44     public static 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      * @return The 3x3 Matrix representing the trnasformation to apply to the
    116      * coordinates of the object being animated
    117      */
    118     public Matrix getMatrix() {
    119         return mMatrix;
    120     }
    121 
    122     /**
    123      * Sets the degree of transparency
    124      * @param alpha 1.0 means fully opaqe and 0.0 means fully transparent
    125      */
    126     public void setAlpha(float alpha) {
    127         mAlpha = alpha;
    128     }
    129 
    130     /**
    131      * @return The degree of transparency
    132      */
    133     public float getAlpha() {
    134         return mAlpha;
    135     }
    136 
    137     @Override
    138     public String toString() {
    139         StringBuilder sb = new StringBuilder(64);
    140         sb.append("Transformation");
    141         toShortString(sb);
    142         return sb.toString();
    143     }
    144 
    145     /**
    146      * Return a string representation of the transformation in a compact form.
    147      */
    148     public String toShortString() {
    149         StringBuilder sb = new StringBuilder(64);
    150         toShortString(sb);
    151         return sb.toString();
    152     }
    153 
    154     /**
    155      * @hide
    156      */
    157     public void toShortString(StringBuilder sb) {
    158         sb.append("{alpha="); sb.append(mAlpha);
    159         sb.append(" matrix="); mMatrix.toShortString(sb);
    160         sb.append('}');
    161     }
    162 
    163     /**
    164      * Print short string, to optimize dumping.
    165      * @hide
    166      */
    167     public void printShortString(PrintWriter pw) {
    168         pw.print("{alpha="); pw.print(mAlpha);
    169         pw.print(" matrix=");
    170         mMatrix.printShortString(pw);
    171         pw.print('}');
    172     }
    173 }
    174