Skip to content

Commit

Permalink
feat(android): animate color (#11658)
Browse files Browse the repository at this point in the history
Fixes TIMOB-27855
  • Loading branch information
m1ga committed Apr 28, 2020
1 parent 5bce3d5 commit 4fa4e19
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,12 @@ private boolean isSingleLineMode()
return false;
}

public int getColor()
{
TextView tv = (TextView) getNativeView();
return tv.getCurrentTextColor();
}

/**
* Updates this object's Android "TextView" with the current member variable settings that affect
* how text is displayed, which includes the single-line/multiline and ellipsize related settings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.Transformation;
import android.widget.TextView;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ArgbEvaluator;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;

import ti.modules.titanium.ui.widget.TiUILabel;

/**
* Builds and starts animations. When possible, Honeycomb+ animations
* (i.e., Property Animators) are used, even on pre-Honeycomb, which is
Expand Down Expand Up @@ -116,6 +119,7 @@ public class TiAnimationBuilder
protected String centerX = null, centerY = null;
protected String width = null, height = null;
protected Integer backgroundColor = null;
protected Integer color = null;
protected TiAnimationCurve curve = TiAnimationBuilder.DEFAULT_CURVE;

protected TiAnimation animationProxy;
Expand Down Expand Up @@ -232,6 +236,10 @@ public void applyOptions(HashMap options)
backgroundColor = TiConvert.toColor(options, TiC.PROPERTY_BACKGROUND_COLOR);
}

if (options.containsKey(TiC.PROPERTY_COLOR)) {
color = TiConvert.toColor(options, TiC.PROPERTY_COLOR);
}

if (options.containsKey(TiC.PROPERTY_ELEVATION)) {
elevation = TiConvert.toFloat(options, TiC.PROPERTY_ELEVATION, -1);
}
Expand Down Expand Up @@ -313,6 +321,18 @@ private AnimatorSet buildPropertyAnimators(int x, int y, int w, int h, int paren
addAnimator(animators, bgAnimator);
}

if (color != null) {
if (viewProxy.peekView() instanceof TiUILabel) {
TiUILabel lblView = (TiUILabel) viewProxy.peekView();

int currentColor = lblView.getColor();
ObjectAnimator colAnimator =
ObjectAnimator.ofInt((TextView) lblView.getNativeView(), "textColor", currentColor, color);
colAnimator.setEvaluator(new ArgbEvaluator());
addAnimator(animators, colAnimator);
}
}

if (tdm != null) {
AnimatorUpdateListener updateListener = null;
updateListener = new AnimatorUpdateListener();
Expand Down
3 changes: 2 additions & 1 deletion apidoc/Titanium/UI/Animation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ properties:
description: |
For information about color values, see the "Colors" section of <Titanium.UI>.
type: String
platforms: [iphone, ipad]
platforms: [android, iphone, ipad]
since: { android: "9.1.0" }

- name: curve
summary: Animation curve or easing function to apply to the animation.
Expand Down
70 changes: 70 additions & 0 deletions tests/Resources/ti.ui.label.addontest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2011-Present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* eslint no-unused-expressions: "off" */
'use strict';
const should = require('./utilities/assertions');
const utilities = require('./utilities/utilities');

describe('Titanium.UI.Label', function () {
let win;

afterEach(function (done) {
if (win) {
// If `win` is already closed, we're done.
let t = setTimeout(function () {
if (win) {
win = null;
done();
}
}, 3000);

win.addEventListener('close', function listener () {
clearTimeout(t);

if (win) {
win.removeEventListener('close', listener);
}
win = null;
done();
});
win.close();
} else {
win = null;
done();
}
});

it.allBroken('animate font color', function (finish) {
win = Ti.UI.createWindow();

const label = Ti.UI.createLabel({
text: 'this is some text',
color: '#f00',
});
const animation = Ti.UI.createAnimation({
color: '#fff',
duration: 1000
});
animation.addEventListener('complete', function () {
// FIXME: iOS appears to be firing this event immediately, not when the animation is actually done!
// test takes 206 ms, but fastest it could run is 1200ms due to 1s animation value
try {
should(label.color).be.eql('#fff'); // FIXME: animations don't update the view's properties when they complete!
// This is a longstanding issue and should be addressed
} catch (err) {
return finish(err);
}
finish();
});
win.addEventListener('open', function () {
setTimeout(() => label.animate(animation), 200);
});
win.add(label);
win.open();
});
});

0 comments on commit 4fa4e19

Please sign in to comment.