2016-04-25 17 views
0

私はアンドロイドでいくつかのアニメーションで簡単なアプリを作成しました。私のレイアウトでは、ImageViewにsourceImageとボタンがあります。ボタンをクリックすると、同じ時間に移動してサイズを変更します。 アニメーションを作成するためにObjectAnimatorとValueAnimatorを使用し、animatorSetと一緒に再生しました。私の問題は、私のボタンがスムーズに動かないことです。 Transitions EverywhereやAPIDemosのようなアニメーションライブラリをいくつか調べました。これらのライブラリでは、ImageBackgroundを設定すると、ビューがスムーズに移動できず、 とそのすべてに問題があります。 誰でもこの問題の解決に手伝ってもらえますか?Androidのアニメーションが滑らかでないBackgroundImage

私のレイアウトコード:

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context=".MainActivity"> 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/imageview" 
    android:fitsSystemWindows="true" 
    android:src="@drawable/image" 
    android:scaleType="fitXY" /> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/frame"> 

    <Button 
     android:layout_width="100dp" 
     android:layout_height="45dp" 
     android:id="@+id/btn_start" 
     android:layout_gravity="center_horizontal|bottom" 
     android:gravity="center_vertical|center_horizontal" 
     android:textAllCaps="false" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="Start" /> 
// .................... 
</FrameLayout> 
</android.support.design.widget.CoordinatorLayout> 

私のアニメーションコード:

btn_start.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      ButtonAnimation(); 
    } 
}); 

private void ButtonAnimation() { 
    Animator moveButtonAnimY = ObjectAnimator.ofFloat(btn_start, "translationY", 0, 200) 
      .setDuration(500); 
    final ValueAnimator valueAnimator = ValueAnimator.ofFloat(width_btn_start, width_btn_start * 2.0f); 
    valueAnimator.setDuration(500); 
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     public void onAnimationUpdate(ValueAnimator animation) { 
      Integer value = (Integer) animation.getAnimatedValue(); 
      btn_start.getLayoutParams().width = value.intValue(); 
      btn_start.requestLayout(); 
     } 
    }); 

    AnimatorSet animatorSet = new AnimatorSet(); 
    animatorSet.playTogether(moveButtonAnimY, valueAnimator); 
    animatorSet.start(); 
}  

答えて

0

ウィキペディアによると:

アニメーション手段によって運動と変化の錯覚を作成するプロセスであります互いに最小限の異なる静止画像のシーケンスの迅速な表示を可能にする。

したがって、アニメーションはデバイスの処理にかなり重い作業であり、参照したときの滑らかさはデバイスの設定によって異なります。

まず、アニメーションをスムーズに実行するには、デバイスに十分なパワーがあるかどうかを確認します。

第2に、コードはボタンをクリックするたびにアニメーションを初期化します。アニメーションを一度初期化し、アニメーションを開始するたびにそのインスタンスを使用する必要があります。あなたのコードの

より良いバージョンは次のようになります。

btn_start.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     ButtonAnimation(); 
    } 
}); 
Animator moveButtonAnimY = ObjectAnimator.ofFloat(btn_start,"translationY", 0, 200).setDuration(500); 
ValueAnimator valueAnimator = ValueAnimator.ofFloat(width_btn_start, width_btn_start * 2.0f); 
valueAnimator.setDuration(500); 
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
    public void onAnimationUpdate(ValueAnimator animation) { 
     Integer value = (Integer) animation.getAnimatedValue(); 
     btn_start.getLayoutParams().width = value.intValue(); 
     btn_start.requestLayout(); 
    } 
}); 
AnimatorSet animatorSet = new AnimatorSet(); 
animatorSet.playTogether(moveButtonAnimY, valueAnimator); 

private void ButtonAnimation() { 
    animatorSet.start(); 
} 
+0

alok、私は私のLG G3のコードをテストが、アニメーションのスピードの変化を見ていません! – MohsenTi

関連する問題