2012-04-30 14 views
3

背景色が赤から青に変わるような画面を作成したいと考えています。何らかの理由で、ValueAnimatorをインスタンス化しようとすると常にクラッシュします。私は私のコードで間違って何見当がつかないValueAnimatorで背景色を変更する

はありがとう

Animationクラス

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行をコメントアウトしてから、メイン画面が正常に表示されました。上記の行のコメントを外すと、クラッシュします。 – Hazaart

2

あなたは背景色に変更するObjectAnimatorを使用することができます:APIについては

を> = 21:

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(); 
} 
:API 16使用このからの後方支援のために

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

これを試してください、これがうまくいくと思います