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 import android.graphics.Rect;
     21 
     22 import java.io.PrintWriter;
     23 
     24 /**
     25  * Defines the transformation to be applied at
     26  * one point in time of an Animation.
     27  *
     28  */
     29 public class Transformation {
     30     /**
     31      * Indicates a transformation that has no effect (alpha = 1 and identity matrix.)
     32      */
     33     public static final int TYPE_IDENTITY = 0x0;
     34     /**
     35      * Indicates a transformation that applies an alpha only (uses an identity matrix.)
     36      */
     37     public static final int TYPE_ALPHA = 0x1;
     38     /**
     39      * Indicates a transformation that applies a matrix only (alpha = 1.)
     40      */
     41     public static final int TYPE_MATRIX = 0x2;
     42     /**
     43      * Indicates a transformation that applies an alpha and a matrix.
     44      */
     45     public static final int TYPE_BOTH = TYPE_ALPHA | TYPE_MATRIX;
     46 
     47     protected Matrix mMatrix;
     48     protected float mAlpha;
     49     protected int mTransformationType;
     50 
     51     private boolean mHasClipRect;
     52     private Rect mClipRect = new Rect();
     53 
     54     /**
     55      * Creates a new transformation with alpha = 1 and the identity matrix.
     56      */
     57     public Transformation() {
     58         clear();
     59     }
     60 
     61     /**
     62      * Reset the transformation to a state that leaves the object
     63      * being animated in an unmodified state. The transformation type is
     64      * {@link #TYPE_BOTH} by default.
     65      */
     66     public void clear() {
     67         if (mMatrix == null) {
     68             mMatrix = new Matrix();
     69         } else {
     70             mMatrix.reset();
     71         }
     72         mClipRect.setEmpty();
     73         mHasClipRect = false;
     74         mAlpha = 1.0f;
     75         mTransformationType = TYPE_BOTH;
     76     }
     77 
     78     /**
     79      * Indicates the nature of this transformation.
     80      *
     81      * @return {@link #TYPE_ALPHA}, {@link #TYPE_MATRIX},
     82      *         {@link #TYPE_BOTH} or {@link #TYPE_IDENTITY}.
     83      */
     84     public int getTransformationType() {
     85         return mTransformationType;
     86     }
     87 
     88     /**
     89      * Sets the transformation type.
     90      *
     91      * @param transformationType One of {@link #TYPE_ALPHA},
     92      *        {@link #TYPE_MATRIX}, {@link #TYPE_BOTH} or
     93      *        {@link #TYPE_IDENTITY}.
     94      */
     95     public void setTransformationType(int transformationType) {
     96         mTransformationType = transformationType;
     97     }
     98 
     99     /**
    100      * Clones the specified transformation.
    101      *
    102      * @param t The transformation to clone.
    103      */
    104     public void set(Transformation t) {
    105         mAlpha = t.getAlpha();
    106         mMatrix.set(t.getMatrix());
    107         if (t.mHasClipRect) {
    108             setClipRect(t.getClipRect());
    109         } else {
    110             mHasClipRect = false;
    111             mClipRect.setEmpty();
    112         }
    113         mTransformationType = t.getTransformationType();
    114     }
    115 
    116     /**
    117      * Apply this Transformation to an existing Transformation, e.g. apply
    118      * a scale effect to something that has already been rotated.
    119      * @param t
    120      */
    121     public void compose(Transformation t) {
    122         mAlpha *= t.getAlpha();
    123         mMatrix.preConcat(t.getMatrix());
    124         if (t.mHasClipRect) {
    125             setClipRect(t.getClipRect());
    126         }
    127     }
    128 
    129     /**
    130      * Like {@link #compose(Transformation)} but does this.postConcat(t) of
    131      * the transformation matrix.
    132      * @hide
    133      */
    134     public void postCompose(Transformation t) {
    135         mAlpha *= t.getAlpha();
    136         mMatrix.postConcat(t.getMatrix());
    137         if (t.mHasClipRect) {
    138             setClipRect(t.getClipRect());
    139         }
    140     }
    141 
    142     /**
    143      * @return The 3x3 Matrix representing the trnasformation to apply to the
    144      * coordinates of the object being animated
    145      */
    146     public Matrix getMatrix() {
    147         return mMatrix;
    148     }
    149 
    150     /**
    151      * Sets the degree of transparency
    152      * @param alpha 1.0 means fully opaqe and 0.0 means fully transparent
    153      */
    154     public void setAlpha(float alpha) {
    155         mAlpha = alpha;
    156     }
    157 
    158     /**
    159      * Sets the current Transform's clip rect
    160      * @hide
    161      */
    162     public void setClipRect(Rect r) {
    163         setClipRect(r.left, r.top, r.right, r.bottom);
    164     }
    165 
    166     /**
    167      * Sets the current Transform's clip rect
    168      * @hide
    169      */
    170     public void setClipRect(int l, int t, int r, int b) {
    171         mClipRect.set(l, t, r, b);
    172         mHasClipRect = true;
    173     }
    174 
    175     /**
    176      * Returns the current Transform's clip rect
    177      * @hide
    178      */
    179     public Rect getClipRect() {
    180         return mClipRect;
    181     }
    182 
    183     /**
    184      * Returns whether the current Transform's clip rect is set
    185      * @hide
    186      */
    187     public boolean hasClipRect() {
    188         return mHasClipRect;
    189     }
    190 
    191     /**
    192      * @return The degree of transparency
    193      */
    194     public float getAlpha() {
    195         return mAlpha;
    196     }
    197 
    198     @Override
    199     public String toString() {
    200         StringBuilder sb = new StringBuilder(64);
    201         sb.append("Transformation");
    202         toShortString(sb);
    203         return sb.toString();
    204     }
    205 
    206     /**
    207      * Return a string representation of the transformation in a compact form.
    208      */
    209     public String toShortString() {
    210         StringBuilder sb = new StringBuilder(64);
    211         toShortString(sb);
    212         return sb.toString();
    213     }
    214 
    215     /**
    216      * @hide
    217      */
    218     public void toShortString(StringBuilder sb) {
    219         sb.append("{alpha="); sb.append(mAlpha);
    220         sb.append(" matrix="); mMatrix.toShortString(sb);
    221         sb.append('}');
    222     }
    223 
    224     /**
    225      * Print short string, to optimize dumping.
    226      * @hide
    227      */
    228     public void printShortString(PrintWriter pw) {
    229         pw.print("{alpha="); pw.print(mAlpha);
    230         pw.print(" matrix=");
    231         mMatrix.printShortString(pw);
    232         pw.print('}');
    233     }
    234 }
    235