Skip to content

Commit

Permalink
Merge pull request #2606 from billdawson/timob-9825
Browse files Browse the repository at this point in the history
Timob 9825: Android animations need to accept unit specifiers such as "dp"
  • Loading branch information
ayeung committed Jul 20, 2012
2 parents 8d508e2 + 9a442f8 commit a2733b9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
Expand All @@ -32,7 +33,6 @@
import android.os.Looper;
import android.os.MessageQueue;
import android.util.FloatMath;
import android.util.Pair;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -59,9 +59,9 @@ public class TiAnimationBuilder
protected Double fromOpacity = null;
protected Double repeat = null;
protected Boolean autoreverse = null;
protected Integer top = null, bottom = null, left = null, right = null;
protected Integer centerX = null, centerY = null;
protected Integer width = null, height = null;
protected String top = null, bottom = null, left = null, right = null;
protected String centerX = null, centerY = null;
protected String width = null, height = null;
protected Integer backgroundColor = null;

protected TiAnimation animationProxy;
Expand Down Expand Up @@ -123,33 +123,33 @@ public void applyOptions(HashMap options)
autoreverse = TiConvert.toBoolean(options, TiC.PROPERTY_AUTOREVERSE);
}
if (options.containsKey(TiC.PROPERTY_TOP)) {
top = TiConvert.toInt(options, TiC.PROPERTY_TOP);
top = TiConvert.toString(options, TiC.PROPERTY_TOP);
}
if (options.containsKey(TiC.PROPERTY_BOTTOM)) {
bottom = TiConvert.toInt(options, TiC.PROPERTY_BOTTOM);
bottom = TiConvert.toString(options, TiC.PROPERTY_BOTTOM);
}
if (options.containsKey(TiC.PROPERTY_LEFT)) {
left = TiConvert.toInt(options, TiC.PROPERTY_LEFT);
left = TiConvert.toString(options, TiC.PROPERTY_LEFT);
}
if (options.containsKey(TiC.PROPERTY_RIGHT)) {
right = TiConvert.toInt(options, TiC.PROPERTY_RIGHT);
right = TiConvert.toString(options, TiC.PROPERTY_RIGHT);
}
if (options.containsKey(TiC.PROPERTY_CENTER)) {
Object centerPoint = options.get(TiC.PROPERTY_CENTER);
if (centerPoint instanceof HashMap) {
HashMap center = (HashMap) centerPoint;
centerX = TiConvert.toInt(center, TiC.PROPERTY_X);
centerY = TiConvert.toInt(center, TiC.PROPERTY_Y);
centerX = TiConvert.toString(center, TiC.PROPERTY_X);
centerY = TiConvert.toString(center, TiC.PROPERTY_Y);

} else {
Log.e(LCAT, "Invalid argument type for center property. Ignoring");
}
}
if (options.containsKey(TiC.PROPERTY_WIDTH)) {
width = TiConvert.toInt(options, TiC.PROPERTY_WIDTH);
width = TiConvert.toString(options, TiC.PROPERTY_WIDTH);
}
if (options.containsKey(TiC.PROPERTY_HEIGHT)) {
height = TiConvert.toInt(options, TiC.PROPERTY_HEIGHT);
height = TiConvert.toString(options, TiC.PROPERTY_HEIGHT);
}
if (options.containsKey(TiC.PROPERTY_BACKGROUND_COLOR)) {
backgroundColor = TiConvert.toColor(options, TiC.PROPERTY_BACKGROUND_COLOR);
Expand Down Expand Up @@ -213,42 +213,43 @@ public AnimationSet render(TiViewProxy viewProxy, View view, int x, int y, int w
{
this.view = view;
this.viewProxy = viewProxy;

TiUIView tiView = viewProxy.peekView();

AnimationSet as = new AnimationSet(false);
AnimationListener animationListener = new AnimationListener();

if (toOpacity != null) {
if (viewProxy.hasProperty(TiC.PROPERTY_OPACITY)) {
fromOpacity = TiConvert.toDouble(viewProxy.getProperty(TiC.PROPERTY_OPACITY));

} else {
fromOpacity = 1.0 - toOpacity;
}

Animation animation = new AlphaAnimation(fromOpacity.floatValue(), toOpacity.floatValue());
applyOpacity = true;
addAnimation(as, animation);
animation.setAnimationListener(animationListener);

TiUIView uiView = viewProxy.peekView();
if (viewProxy.hasProperty(TiC.PROPERTY_OPACITY) && fromOpacity != null && toOpacity != null
&& uiView != null) {
&& tiView != null) {
// Initialize the opacity to 1 when we are going to change it in
// the animation. If the opacity of the view was initialized to
// 0, the animation doesn't work
uiView.setOpacity(1);
tiView.setOpacity(1);
}
}

if (backgroundColor != null) {
int fromBackgroundColor = 0;

if (viewProxy.hasProperty(TiC.PROPERTY_BACKGROUND_COLOR)) {
fromBackgroundColor = TiConvert.toColor(TiConvert.toString(viewProxy.getProperty(TiC.PROPERTY_BACKGROUND_COLOR)));
} else {
Log.w(LCAT, "Cannot animate view without a backgroundColor. View doesn't have that property. Using #00000000");
fromBackgroundColor = Color.argb(0,0,0,0);
}

Animation a = new TiColorAnimation(view, fromBackgroundColor, backgroundColor);
addAnimation(as, a);
}
Expand All @@ -257,8 +258,6 @@ public AnimationSet render(TiViewProxy viewProxy, View view, int x, int y, int w
as.setFillAfter(true);
as.setFillEnabled(true);

TiUIView tiView = viewProxy.peekView();

Animation anim;
if (tdm.hasScaleOperation() && tiView != null) {
tiView.setAnimatedScaleValues(tdm.verifyScaleValues(tiView, (autoreverse != null && autoreverse.booleanValue())));
Expand Down Expand Up @@ -294,34 +293,32 @@ public AnimationSet render(TiViewProxy viewProxy, View view, int x, int y, int w
TiDimension optionLeft = null, optionRight = null;
TiDimension optionCenterX = null, optionCenterY = null;

TiUIView tiView = viewProxy.peekView();

// Note that we're stringifying the values to make sure we
// use the correct TiDimension constructor, except when
// we know the values are expressed for certain in pixels.
if (top != null) {
optionTop = new TiDimension(String.valueOf(top), TiDimension.TYPE_TOP);
optionTop = new TiDimension(top, TiDimension.TYPE_TOP);
} else {
optionTop = new TiDimension(view.getTop(), TiDimension.TYPE_TOP);
optionTop.setUnits(TypedValue.COMPLEX_UNIT_PX);
}

if (bottom != null) {
optionBottom = new TiDimension(String.valueOf(bottom), TiDimension.TYPE_BOTTOM);
optionBottom = new TiDimension(bottom, TiDimension.TYPE_BOTTOM);
} else {
optionBottom = new TiDimension(view.getBottom(), TiDimension.TYPE_BOTTOM);
optionBottom.setUnits(TypedValue.COMPLEX_UNIT_PX);
}

if (left != null) {
optionLeft = new TiDimension(String.valueOf(left), TiDimension.TYPE_LEFT);
optionLeft = new TiDimension(left, TiDimension.TYPE_LEFT);
} else {
optionLeft = new TiDimension(view.getLeft(), TiDimension.TYPE_LEFT);
optionLeft.setUnits(TypedValue.COMPLEX_UNIT_PX);
}

if (right != null) {
optionRight = new TiDimension(String.valueOf(right), TiDimension.TYPE_RIGHT);
optionRight = new TiDimension(right, TiDimension.TYPE_RIGHT);
} else {
optionRight = new TiDimension(view.getRight(), TiDimension.TYPE_RIGHT);
optionRight.setUnits(TypedValue.COMPLEX_UNIT_PX);
Expand Down Expand Up @@ -356,10 +353,10 @@ public AnimationSet render(TiViewProxy viewProxy, View view, int x, int y, int w
int newXDelta = horizontal[0] - x;
int newYDelta = vertical[0] - y;
if (tiView != null) {
Pair<Integer, Integer> currentTranslation = tiView.getAnimatedXYTranslationValues();
Point currentTranslation = tiView.getAnimatedXYTranslationValues();
if (currentTranslation != null) {
previousXDelta = currentTranslation.first;
previousYDelta = currentTranslation.second;
previousXDelta = currentTranslation.x;
previousYDelta = currentTranslation.y;
}
}

Expand All @@ -371,7 +368,7 @@ public AnimationSet render(TiViewProxy viewProxy, View view, int x, int y, int w
// Remember where we're going to, since there is no native way to look it up later.
// We don't need to remember it if we're autoreversing, however.
if (tiView != null && (autoreverse == null || !autoreverse.booleanValue())) {
tiView.setAnimatedXYTranslationValues(Pair.create(Integer.valueOf(newXDelta), Integer.valueOf(newYDelta)));
tiView.setAnimatedXYTranslationValues(new Point(newXDelta, newYDelta));
}

if (duration != null) {
Expand All @@ -390,29 +387,50 @@ public AnimationSet render(TiViewProxy viewProxy, View view, int x, int y, int w
}

if (tdm == null && (width != null || height != null)) {
TiDimension optionWidth, optionHeight;
// we need to setup a custom animation for this, is there a better way?
int toWidth = width == null ? w : width;
int toHeight = height == null ? h : height;
if (width != null) {
optionWidth = new TiDimension(width, TiDimension.TYPE_WIDTH);
} else {
optionWidth = new TiDimension(w, TiDimension.TYPE_WIDTH);
optionWidth.setUnits(TypedValue.COMPLEX_UNIT_PX);
}

if (height != null) {
optionHeight = new TiDimension(height, TiDimension.TYPE_HEIGHT);
} else {
optionHeight = new TiDimension(w, TiDimension.TYPE_HEIGHT);
optionHeight.setUnits(TypedValue.COMPLEX_UNIT_PX);
}

int toWidth = optionWidth.getAsPixels(view);
int toHeight = optionHeight.getAsPixels(view);
SizeAnimation sizeAnimation = new SizeAnimation(view, w, h, toWidth, toHeight);

if (duration != null) {
sizeAnimation.setDuration(duration.longValue());
}

// Also wipe out any saved x,y deltas from Translate animation above,
// since this size animation is going to reset the layout completely.
if (tiView != null) {
tiView.setAnimatedXYTranslationValues(new Point(0, 0));
}

sizeAnimation.setInterpolator(new LinearInterpolator());
sizeAnimation.setAnimationListener(animationListener);
as.addAnimation(sizeAnimation);

relayoutChild = true;
}

if (callback != null || animationProxy != null) {
as.setAnimationListener(animationListener);
}

return as;
}

protected class SizeAnimation extends Animation
{
protected View view;
Expand Down Expand Up @@ -461,7 +479,9 @@ protected void applyTransformation(float interpolatedTime, Transformation transf
if (params instanceof TiCompositeLayout.LayoutParams) {
TiCompositeLayout.LayoutParams tiParams = (TiCompositeLayout.LayoutParams)params;
tiParams.optionHeight = new TiDimension(height, TiDimension.TYPE_HEIGHT);
tiParams.optionHeight.setUnits(TypedValue.COMPLEX_UNIT_PX);
tiParams.optionWidth = new TiDimension(width, TiDimension.TYPE_WIDTH);
tiParams.optionWidth.setUnits(TypedValue.COMPLEX_UNIT_PX);
}

view.setLayoutParams(params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public abstract class TiUIView
private float animatedRotationDegrees = 0f; // i.e., no rotation.

// Same for translate animation.
private Pair<Integer, Integer> animatedXYTranslationValues = Pair.create(Integer.valueOf(0), Integer.valueOf(0));
private Point animatedXYTranslationValues = new Point(0, 0);

private KrollDict lastUpEvent = new KrollDict(2);
// In the case of heavy-weight windows, the "nativeView" is null,
Expand Down Expand Up @@ -1391,7 +1391,7 @@ public float getAnimatedRotationDegrees()
* Retrieve the saved translate animation x & y deltas, which we store here since Android provides no property
* for looking them up.
*/
public Pair<Integer, Integer> getAnimatedXYTranslationValues()
public Point getAnimatedXYTranslationValues()
{
return animatedXYTranslationValues;
}
Expand All @@ -1400,7 +1400,7 @@ public Pair<Integer, Integer> getAnimatedXYTranslationValues()
* Store the translate animation x and y delta values
* since Android provides no property for looking them up.
*/
public void setAnimatedXYTranslationValues(Pair<Integer, Integer> newValues)
public void setAnimatedXYTranslationValues(Point newValues)
{
animatedXYTranslationValues = newValues;
}
Expand All @@ -1412,7 +1412,7 @@ private void resetPostAnimationValues()
{
animatedRotationDegrees = 0f; // i.e., no rotation.
animatedScaleValues = Pair.create(Float.valueOf(1f), Float.valueOf(1f)); // 1 means no scaling
animatedXYTranslationValues = Pair.create(Integer.valueOf(0), Integer.valueOf(0));
animatedXYTranslationValues = new Point(0, 0);
}

}

0 comments on commit a2733b9

Please sign in to comment.