使用CharSequence属性来设置ObjectAnimator

3
我是一位有用的助手,可以为您翻译文本。
我正在尝试使用Android中的“新”属性动画。在尝试实现更改TextView文本的ValueAnimator时遇到了问题。
这是我的动画逻辑(text1是一个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();

这是我得到的错误:11-22 14:37:35.848: E/PropertyValuesHolder(3481): Couldn't find setter/getter for property text with value type class java.lang.String
有人知道我如何强制ObjectAnimator查找带有CharSequence参数的setText吗?
2个回答

4

我还没有找到一种方法可以使ObjectAnimator与CharSequence值一起使用。

但是,我成功地使用了标准的ValueAnimator来实现这个目标。

以下是示例。

ValueAnimator textAnimator = new ValueAnimator();
textAnimator.setObjectValues(start, end);
textAnimator.addUpdateListener(new AnimatorUpdateListener() {
    public void onAnimationUpdate(ValueAnimator animation) {
        text1.setText((CharSequence)animation.getAnimatedValue());
    }
});
textAnimator.setEvaluator(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,
                                startValue.length()
                                        - (int) ((startValue.length() - endValue
                                                .length()) * fraction));
                }
            });

textAnimator.setDuration(6000);
textAnimator.setRepeatCount(ValueAnimator.INFINITE);
textAnimator.start();

4

这是一个老问题,我想知道是否有其他人遇到过这个问题。今天我遇到了。这里是我的解决方法。我仍然使用ObjectAnimator与一个包装类(这是Android文档中的提示)。

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);}
}

使用这个类,您可以使用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();

这段代码片段在3秒钟内从3到0进行倒计时。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接