2016-10-05 12 views
-1

アニメーションで何か助けが必要です。Androidのアニメーション:背景色が時間によって変わります

私はユーザーが数学的な仕事を解決する単純なゲームに取り組んでいます。各ラウンドには60秒があり、私はCountDownTimerを使って時間をチェックしています。残っている時間をテキストで表現するのではなく、背景色を変更したい。たとえば、リンクを確認してください。

私は青い RelativeLayout今持っている、と私はこのレイアウト (または多分ちょうど左に移動する)の色を変更したい

GIF example of animation

の残り時間に応じました。ゲームが始まったばかりのレイアウト全体が青色になり、レイアウトの半分が暗くなると、これはgifの例から明らかです。

重要:ユーザーは「スーパーパワー」を使用してラウンドの時間を長くすることができます。ユーザーは10秒で終了すると、 "スーパーパワー"を使用して+30秒を追加できます。これはバックグラウンドアニメーションに影響します。

この効果をどのように作り出せるか教えてください。

答えて

0

ちょっと考えて、お互いに重なり合う2つの別々の背景/レイアウトを使ってこれを実現することができます。

CountDownTimerを使用すると、ObjectAnimatorを使用してバックグラウンドの1つを水平に翻訳し、変換値の時間を渡すことができます。これにより、時間が増加しているか減少しているかに基づいて、バックグラウンドがいずれの方向にも変化します。

0

ここに行く...

私は左から右にビューの背景色を変更するコードを実装しています。私は10秒間した。後で必要に応じて増やすことができます。

私は幅と高さが親と一致する別のビューを作成しました。また、ビューのアニメーションを取得するには、 "TranslateAnimation" APIを使用してビューを翻訳する "Animation"クラスを使用しました。

また、アクティビティマニフェストでは、優先度の最初のビューを取得するために、「elevation」属性を使用しています。

以下のコードをご覧ください。

  1. activity_main.xml

`

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.golevr.changebackgroundcolor.MainActivity" 
    android:id="@+id/constraint"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello World!" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:elevation="10dp"/> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/view" 
     android:elevation="0dp"></View> 

</android.support.constraint.ConstraintLayout> 

`

  • MainActivity.java
  • `

    public class MainActivity extends AppCompatActivity { 
    
        ConstraintLayout constraintLayout; 
        View view; 
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.activity_main); 
         constraintLayout = (ConstraintLayout) findViewById(R.id.constraint); 
         view = (View)findViewById(R.id.view); 
    
         view.startAnimation(inFromLeftAnimation()); 
         constraintLayout.bringToFront(); 
    
        } 
    
        private Animation inFromLeftAnimation() { 
    
         Animation inFromRight = new TranslateAnimation(
           Animation.RELATIVE_TO_PARENT, -1.0f, 
           Animation.RELATIVE_TO_PARENT, 0.0f, 
           Animation.RELATIVE_TO_PARENT, 0.0f, 
           Animation.RELATIVE_TO_PARENT, 0.0f); 
         view.setBackgroundColor(Color.BLUE); 
         inFromRight.setDuration(10000); 
         inFromRight.setInterpolator(new AccelerateInterpolator()); 
         return inFromRight; 
        } 
    
    } 
    

    関連する問題