2017-02-07 2 views
0

Я хочу анимировать цвет подсказки в тексте редактирования, но следующее, похоже, не работает.Как анимировать свойство textColorHint EditText?

fun animateHintTextColorChange(et: EditText, colorFromResId: Int, colorToResId: Int) { 
     val colorFrom = ContextCompat.getColor(et.context, colorFromResId) 
     val colorTo = ContextCompat.getColor(et.context, colorToResId) 
     val animator = ObjectAnimator.ofInt(et, "textColorHint", colorFrom, Color.RED) 
     animator.setEvaluator(ArgbEvaluator()) 
     animator.start() 
    } 
+0

Вы должны использовать 'PropertyAnimator' или крючок аниматор слушатель и установить анимированное значение с помощью' et.setHintTextColor (animator.getAnimatedValue()); ' –

+0

Пробовали ли вы ниже это удовлетворить ваши потребности? – Raghunandan

+0

Извините @ Raghunandan Я не пробовал, решение, которое я придумал, намного проще, чем я себя чувствую. Спасибо хоть – TomTaila

ответ

0

Я нашел решение благодаря намеком @Nikola Despotoski в.

fun animateHintTextColorChange(editText: EditText, colorFrom: Int, colorTo: Int, startDelay: Long = 0) : Animator { 
     val animator = ValueAnimator.ofObject(ArgbEvaluator(), colorFrom, colorTo) 
     animator.duration = DURATION_MEDIUM 
     animator.startDelay = startDelay 
     animator.addUpdateListener { animator -> editText.setHintTextColor(animator.animatedValue as Int) } 

     return animator 
    } 
0

Создать CustomSpan

import android.text.TextPaint; 
import android.text.style.CharacterStyle; 
import android.text.style.UpdateAppearance; 


public class CustomSpan extends CharacterStyle implements UpdateAppearance { 

    private int color; 

    public int getColor() { 
     return color; 
    } 

    public void setColor(int color) { 
     this.color = color; 
    } 

    public CustomSpan() { 

    } 

    @Override 
    public void updateDrawState(TextPaint paint) { 
     paint.setColor(color); 

    } 
} 

Затем используйте ObjectAnimator

final EditText editText = (EditText) this.findViewById(R.id.edit_text); 

    CustomSpan span = new CustomSpan(); 

    final String text = getResources().getString(R.string.edit_hint); 

    final SpannableString spannableString = new SpannableString(text); 

    int start = 0; 
    int end = text.length(); 
    spannableString.setSpan(span, start, end, 0); 



    int colorFrom = Color.BLACK; 
    int colorTo = Color.RED; 
    int duration = 2000; 

    final ObjectAnimator objectAnimator = ObjectAnimator.ofInt(
      span, CHANGE_COLOR_PROPERTY, colorFrom, colorTo); 

    objectAnimator.setEvaluator(new ArgbEvaluator()); 
    objectAnimator.setDuration(duration); 
    objectAnimator.start(); 

    objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator animation) { 
      editText.setHint(spannableString); 
     } 
    }); 

CustomProperty

private static final Property<CustomSpan, Integer> CHANGE_COLOR_PROPERTY 
     = new Property<CustomSpan, Integer>(Integer.class, "CHANGE_COLOR_PROPERTY") { 

    @Override 
    public void set(CustomSpan span, Integer value) { 
     span.setColor(value); 
    } 
    @Override 
    public Integer get(CustomSpan object) { 
     return object.getColor(); 
    } 
}; 

Примечание: Если длительность медленно вы можете увидеть некоторые промежуточные цвета, Начало от начала цвет до конца цвет.

Обновление: Я также хотел бы предложить, чтобы посмотреть на MutableForegroundSpan @http://flavienlaurent.com/blog/2014/01/31/spans/