RotateAnimation.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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 17package android.view.animation; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.util.AttributeSet; 22 23/** 24 * An animation that controls the rotation of an object. This rotation takes 25 * place int the X-Y plane. You can specify the point to use for the center of 26 * the rotation, where (0,0) is the top left point. If not specified, (0,0) is 27 * the default rotation point. 28 * 29 */ 30public class RotateAnimation extends Animation { 31 private float mFromDegrees; 32 private float mToDegrees; 33 34 private int mPivotXType = ABSOLUTE; 35 private int mPivotYType = ABSOLUTE; 36 private float mPivotXValue = 0.0f; 37 private float mPivotYValue = 0.0f; 38 39 private float mPivotX; 40 private float mPivotY; 41 42 /** 43 * Constructor used whan an RotateAnimation is loaded from a resource. 44 * 45 * @param context Application context to use 46 * @param attrs Attribute set from which to read values 47 */ 48 public RotateAnimation(Context context, AttributeSet attrs) { 49 super(context, attrs); 50 51 TypedArray a = context.obtainStyledAttributes(attrs, 52 com.android.internal.R.styleable.RotateAnimation); 53 54 mFromDegrees = a.getFloat( 55 com.android.internal.R.styleable.RotateAnimation_fromDegrees, 0.0f); 56 mToDegrees = a.getFloat(com.android.internal.R.styleable.RotateAnimation_toDegrees, 0.0f); 57 58 Description d = Description.parseValue(a.peekValue( 59 com.android.internal.R.styleable.RotateAnimation_pivotX)); 60 mPivotXType = d.type; 61 mPivotXValue = d.value; 62 63 d = Description.parseValue(a.peekValue( 64 com.android.internal.R.styleable.RotateAnimation_pivotY)); 65 mPivotYType = d.type; 66 mPivotYValue = d.value; 67 68 a.recycle(); 69 } 70 71 /** 72 * Constructor to use when building a RotateAnimation from code. 73 * Default pivotX/pivotY point is (0,0). 74 * 75 * @param fromDegrees Rotation offset to apply at the start of the 76 * animation. 77 * 78 * @param toDegrees Rotation offset to apply at the end of the animation. 79 */ 80 public RotateAnimation(float fromDegrees, float toDegrees) { 81 mFromDegrees = fromDegrees; 82 mToDegrees = toDegrees; 83 mPivotX = 0.0f; 84 mPivotY = 0.0f; 85 } 86 87 /** 88 * Constructor to use when building a RotateAnimation from code 89 * 90 * @param fromDegrees Rotation offset to apply at the start of the 91 * animation. 92 * 93 * @param toDegrees Rotation offset to apply at the end of the animation. 94 * 95 * @param pivotX The X coordinate of the point about which the object is 96 * being rotated, specified as an absolute number where 0 is the left 97 * edge. 98 * @param pivotY The Y coordinate of the point about which the object is 99 * being rotated, specified as an absolute number where 0 is the top 100 * edge. 101 */ 102 public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) { 103 mFromDegrees = fromDegrees; 104 mToDegrees = toDegrees; 105 106 mPivotXType = ABSOLUTE; 107 mPivotYType = ABSOLUTE; 108 mPivotXValue = pivotX; 109 mPivotYValue = pivotY; 110 } 111 112 /** 113 * Constructor to use when building a RotateAnimation from code 114 * 115 * @param fromDegrees Rotation offset to apply at the start of the 116 * animation. 117 * 118 * @param toDegrees Rotation offset to apply at the end of the animation. 119 * 120 * @param pivotXType Specifies how pivotXValue should be interpreted. One of 121 * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 122 * Animation.RELATIVE_TO_PARENT. 123 * @param pivotXValue The X coordinate of the point about which the object 124 * is being rotated, specified as an absolute number where 0 is the 125 * left edge. This value can either be an absolute number if 126 * pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) 127 * otherwise. 128 * @param pivotYType Specifies how pivotYValue should be interpreted. One of 129 * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or 130 * Animation.RELATIVE_TO_PARENT. 131 * @param pivotYValue The Y coordinate of the point about which the object 132 * is being rotated, specified as an absolute number where 0 is the 133 * top edge. This value can either be an absolute number if 134 * pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) 135 * otherwise. 136 */ 137 public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, 138 int pivotYType, float pivotYValue) { 139 mFromDegrees = fromDegrees; 140 mToDegrees = toDegrees; 141 142 mPivotXValue = pivotXValue; 143 mPivotXType = pivotXType; 144 mPivotYValue = pivotYValue; 145 mPivotYType = pivotYType; 146 } 147 148 @Override 149 protected void applyTransformation(float interpolatedTime, Transformation t) { 150 float degrees = mFromDegrees + ((mToDegrees - mFromDegrees) * interpolatedTime); 151 152 if (mPivotX == 0.0f && mPivotY == 0.0f) { 153 t.getMatrix().setRotate(degrees); 154 } else { 155 t.getMatrix().setRotate(degrees, mPivotX, mPivotY); 156 } 157 } 158 159 @Override 160 public void initialize(int width, int height, int parentWidth, int parentHeight) { 161 super.initialize(width, height, parentWidth, parentHeight); 162 mPivotX = resolveSize(mPivotXType, mPivotXValue, width, parentWidth); 163 mPivotY = resolveSize(mPivotYType, mPivotYValue, height, parentHeight); 164 } 165} 166