Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObjectAnimator using CharSequence setters

I'm playing around with the "new" property animations in Android. And ran into a wall when trying to implement a ValueAnimator that changes the text of a TextView.

This is my animation logic (text1 is a TextView)

ValueAnimator textAnim = ObjectAnimator.ofObject(text1, "text",
            new TypeEvaluator<CharSequence>() {
                public CharSequence evaluate(float fraction,
                        CharSequence startValue, CharSequence endValue) {
                    if (startValue.length() < endValue.length())
                        return endValue.subSequence(0,
                                (int) (endValue.length() * fraction));
                    else
                        return startValue.subSequence(
                                0,
                                endValue.length()
                                        + (int) ((startValue.length() - endValue
                                                .length()) * fraction));
                }
            }, start, end);
textAnim.setRepeatCount(ValueAnimator.INFINITE);
textAnim.setDuration(6000);
textAnim.start();

This is the error im getting: 11-22 14:37:35.848: E/PropertyValuesHolder(3481): Couldn't find setter/getter for property text with value type class java.lang.String.

Does anyone know how i can force the ObjectAnimator to look for a setText with a CharSequence parameter instead?

like image 929
CoolMcGrrr Avatar asked Mar 14 '26 14:03

CoolMcGrrr


1 Answers

It's an old question and I wonder if anybody else came across this issue. I did today. Here's how I created a work around. I still use ObjectAnimator with a wrapper class (that was a hint in the Android documentation)

Wrapper class for TextView:

private class AnimatedTextView {
    private final TextView textView;

    public AnimatedTextView(TextView textView) {this.textView = textView;}
    public String getText() {return textView.getText().toString();}
    public void setText(String text) {textView.setText(text);}
}

With this class you can use the ObjectAnimator:

    ObjectAnimator.ofObject(new AnimatedTextView((TextView) findViewById(R.id.shortcutLabel)), "Text", new TypeEvaluator<String>() {
        @Override
        public String evaluate(float fraction, String startValue, String endValue) {
            return (fraction < 0.5)? startValue:endValue;
        }
    }, "3", "2", "1", "0")
        .setDuration(3000L)
        .start();

This code snippet does a countdown from 3 to 0 in 3 seconds.

like image 88
jboi Avatar answered Mar 17 '26 04:03

jboi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!