使用ObjectAnimator的Animate SpannableStringBuilder不会设置动画

时间:2015-02-01 22:34:03

标签: java android android-intent

我一直试图为spannablestringbuilder设置动画。但是,我从Devices Logcat收到此消息:“PropertyValuesHolder:Method setAlpha(),类型为float,未在目标类类上找到”。

关于如何为纯字符串设置动画的任何想法?

我一直在做的代码:

   private static void fadeEnhancedText(Context context,TextView labText, TextView labEnchangedText,
                                         int defaultColor) {
        //animation add objectanimation
        SpannableStringBuilder mSpannableStringBuilder=
                new SpannableStringBuilder(labEnchangedText
                        .getText().toString());
        mSpannableStringBuilder.setSpan(mForegroundColorSpan, mMatcher.start(),
                mMatcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        labText.setText(labEnchangedText.getText().toString());
        labText.setVisibility(View.GONE);
        //TODO spannable problem fader doesn't work fully
        labEnchangedText.setText(mSpannableStringBuilder, TextView.BufferType.SPANNABLE);
        mAnimationSet.play(fadeIn(labEnchangedText,mSpannableStringBuilder)).
                after(fadeOut(labEnchangedText,
                labText,
                defaultColor,mSpannableStringBuilder));
       mAnimationSet.start();
    }

    private static ObjectAnimator fadeIn(TextView textView, SpannableStringBuilder spannableStringBuilder)
    {
        ObjectAnimator animation = ObjectAnimator.ofFloat(spannableStringBuilder.getSpans(mMatcher.start(),
                mMatcher.end(),textView.getClass()), "alpha",0f,1f);
        animation.setDuration(300);
        return animation;

    }

    private static ObjectAnimator fadeOut(final TextView textView, final TextView currentView, final int defaultColor,
                                          final SpannableStringBuilder spannableStringBuilder)
    {
        //textView.setTextColor(Color.rgb(177, 24, 46));
        ObjectAnimator animation = ObjectAnimator.ofFloat(spannableStringBuilder.getSpans(mMatcher.start(),
                mMatcher.end(),textView.getClass()), "alpha",1f,0f);
        animation.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                textView.setTextColor(defaultColor);
                currentView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animation.setRepeatCount(4);
        animation.setDuration(300);
        return animation;

    }

1 个答案:

答案 0 :(得分:1)

TextView,在调用getSpans(指定类型为TextView)时为跨度返回的类型,没有setAlpha方法(也不是getAlpha方法)。另外,我不确定ObjectAnimator是如何喜欢数组的,但我还没有尝试过。

查看Android Transparent TextView?的TextView透明度。

也可以使用HTML来设置文字样式:How to display HTML in TextView?或查看字符串资源指南:http://developer.android.com/guide/topics/resources/string-resource.html#StylingWithHTML