2012-04-30 253 views
3

我想創建一個背景顏色不斷從紅色變爲藍色的屏幕。出於某種原因,當我嘗試實例化ValueAnimator時,它總是崩潰。我不知道這有什麼錯我的代碼ValueAnimator改變背景顏色

謝謝

動畫類

public BackgroundAnimation(Context context){ 
    super(context); 
    ValueAnimator colorAnim = ObjectAnimator.ofInt(R.anim.animator, "backgroundColor", Color.RED, Color.BLUE); 
    colorAnim.setDuration(3000); 
    colorAnim.setEvaluator(new ArgbEvaluator()); 
    colorAnim.setRepeatCount(ValueAnimator.INFINITE); 
    colorAnim.setRepeatMode(ValueAnimator.REVERSE); 
    colorAnim.start(); 

} 

animator.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <objectAnimator 
     android:propertyName="backgroundColor"/> 
</set> 

主類

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.menu); 

    LinearLayout container = (LinearLayout) findViewById(R.id.container); 
    container.addView(new BackgroundAnimation(this)); 

} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 

<TextView 
    android:id="@+id/TextView01" 
    android:gravity="center" 
    android:textSize="20sp" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 

<TextView 
    android:id="@+id/TextView02" 
    android:gravity="center" 
    android:textSize="20sp" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 

<ImageView 
    android:id="@+id/ImageView01" 
    android:layout_width="fill_parent" 
    android:layout_height="300sp"/> 

</LinearLayout> 

回答

2

沒有在LinearLayout中XML文件的ID參數。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:id="@+id/container"> 
+0

這部分工作正常。崩潰發生在 ValueAnimator colorAnim = ObjectAnimator.ofInt(R.anim.animator,「backgroundColor」,Color.RED,Color.BLUE); – Hazaart

+0

你確定工作正常嗎?可以顯示你的日誌? – goodm

+0

我不知道如何在我的手機上進行調試。我試着評論這6行關於valueanimator的內容,然後主屏幕顯示正常。當我取消註釋該行時,它會崩潰。 – Hazaart

2

可以使用ObjectAnimator改變背景顏色:

對於API> = 21:

ObjectAnimator colorAnimator = ObjectAnimator.ofArgb(travelersListView.getBackground().mutate(), "tint", mCurrentBackground, mFadeColor); 
colorAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); 
colorAnimator.start(); 

對於從API 16使用該向後支持:

ObjectAnimator colorAnimator = ObjectAnimator.ofObject(travelersListView.getBackground().mutate(), "tint", new ArgbEvaluator(), mCurrentBackground, mFadeColor); 
colorAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); 
colorAnimator.start(); 
0
@Override 
public void onCreate(Bundle icicle) { 
super.onCreate(icicle); 
setContentView(R.layout.menu); 

LinearLayout container = (LinearLayout) findViewById(R.id.container); 

changeBackground(container, "#F44336", "#2196F3"); //#F44336 : Red , #2196F3 : Blue 

} 

public void changeBackground(final View view, String color1, String color2) { 
    ValueAnimator anim = new ValueAnimator(); 
    anim.setIntValues(Color.parseColor(color1), Color.parseColor(color2)); 
    anim.setEvaluator(new ArgbEvaluator()); 
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator valueAnimator) { 

      view.setBackgroundColor((Integer) valueAnimator.getAnimatedValue()); 
     } 
    }); 

    anim.setDuration(2000); 
    anim.setRepeatCount(ValueAnimator.INFINITE); 
    anim.setRepeatMode(ValueAnimator.REVERSE); 
    anim.start(); 
} 

試試這個,希望對您有所幫助