2013-03-23 42 views
19

Tôi muốn sử dụng một ValueAnimator để tạo màu của văn bản TextView nhấp nháy hai lần giữa hai màu khác nhau nhưng tôi muốn tạo Hoạt ảnh trong XML. Tôi không thể tìm thấy bất kỳ ví dụ nào. Bất kỳ trợ giúp sẽ được đánh giá cao.Sử dụng một ValueAnimator để làm cho một TextView nhấp nháy các màu khác nhau

Cập nhật

Đoạn code dưới đây hoạt động hoàn hảo. Màu sắc thay đổi từ đen sang xanh dương, xanh lam thành đen, đen sang xanh, và xanh lam thành đen với 500ms ở giữa mỗi lần lặp lại ngược lại. Tuy nhiên tôi đang cố gắng để có được điều này để làm việc từ một tập tin xml animator.

ValueAnimator colorAnim = ObjectAnimator.OfInt(objectToFlash, "textColor", (int)fromColor, (int)toColor); 
colorAnim.SetDuration(500); 
colorAnim.SetEvaluator(new ArgbEvaluator()); 
colorAnim.RepeatCount = 3; 
colorAnim.RepeatMode = ValueAnimatorRepeatMode.Reverse; 

xml

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" 
     android:propertyName="textColor"   
     android:duration="500" 
     android:valueFrom="@color/black" 
     android:valueTo="@color/ei_blue" 
     android:repeatCount="3" 
     android:repeatMode="reverse" /> 

ValueAnimator anim = (ObjectAnimator)AnimatorInflater.LoadAnimator(Activity, Resource.Animator.blinking_text); 
anim.SetTarget(objectToFlash); 

Sử dụng xml làm cho màu sắc của màu sắc văn bản 's TextView thay đổi nhiều lần như nó có thể trong vòng 500ms.

Cập nhật Tôi nghĩ những gì tôi cần là Khung chính để bắt chước trong xml những gì cuộc gọi OfInt đang thực hiện theo chương trình. Đang thử điều này ngay bây giờ nhưng không có may mắn cho đến nay.

Trả lời

29

Hãy thử điều này:

private static final int RED = 0xffFF8080; 
private static final int BLUE = 0xff8080FF; 

ValueAnimator colorAnim = ObjectAnimator.ofInt(myTextView, "backgroundColor", RED, BLUE); 
     colorAnim.setDuration(3000); 
     colorAnim.setEvaluator(new ArgbEvaluator()); 
     colorAnim.setRepeatCount(ValueAnimator.INFINITE); 
     colorAnim.setRepeatMode(ValueAnimator.REVERSE); 
     colorAnim.start(); 

Hoặc thử phương pháp chưa được kiểm tra này qua xml: * res/phim hoạt hình/property_animator.xml *

<set > 

<objectAnimator 
    android:propertyName="backgroundColor" 
    android:duration="3000" 
    android:valueFrom="#FFFF8080" 
    android:valueTo="#FF8080FF" 
    android:repeatCount="-1" 
    android:repeatMode="reverse" /> 
</set> 

bây giờ trong mã Java:

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext, 
R.anim.property_animator); 
set.setTarget(myTextView); 
set.start(); 
+0

Điều đó có tác dụng, cảm ơn bạn nhưng người hoạt hình sẽ trông như thế nào? – wheels53

+0

đã cập nhật câu trả lời của tôi, vui lòng kiểm tra và đưa ra phản hồi. –

+0

cũng vui lòng đọc http://developer.android.com/guide/topics/resources/animation-resource.html –

3

Sự cố mà bạn mô tả là trình mô tả đối tượng được chỉ định trong XML không chỉ định chính xác ArgbEvaluator cho phép nội suy màu.

Để giải quyết vấn đề, hãy tạo đối tượng hoạt ảnh XML để xoay giữa các màu bạn muốn. Sau đó, trong mã nguồn, làm như sau đảm bảo rằng người đánh giá được sử dụng bởi các phim hoạt hình là một ArgbEvaluator:

ObjectAnimator colorAnimator = (ObjectAnimator)AnimatorInflater.loadAnimator(this, R.animator.color_rotation); 
colorAnimator.setTarget(objectToFlash); 
colorAnimator.setEvaluator(new ArgbEvaluator()); 
colorAnimator.start(); 
2

Bắt đầu từ CẤP API> 21 tác dụng tương tự có thể được thực hiện với phương pháp tĩnh ObjectAnimator.ofArgb như thế này:

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
    private void animateText(TextView text) { 
     ObjectAnimator animator = ObjectAnimator.ofArgb(text, "textColor", Color.WHITE, Color.RED); 
     animator.setDuration(500); 
     animator.setRepeatCount(3); 
     animator.setRepeatMode(ValueAnimator.REVERSE); 
     animator.start(); 
    } 
Các vấn đề liên quan