文本动画和背景更改

时间:2017-10-04 21:07:17

标签: java android animation

我被困住了,需要帮助。

enter image description here

如何在图片上添加动画。我希望在应用程序启动后1秒背景颜色和文本平滑自动更改 1秒。并且像一个循环。同时图标是冲动的

2 个答案:

答案 0 :(得分:0)

您可以使用属性动画来更改颜色

mvn help:effective-pom

使用alpha动画淡入和淡出文本更改所以重复是当它重新淡入时,以便您可以在那时更新文本

int colorFrom = getResources().getColor(R.color.red);
int colorTo = getResources().getColor(R.color.blue);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(500); // milliseconds
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animator) {
        textView.setBackgroundColor((int) animator.getAnimatedValue());
    }

});
colorAnimation.start();

您可以在位图或imageView上使用相同的动画来进行脉冲输入和输出。

AlphaAnimation anim = new AlphaAnimation(1.0f, 0.0f);
        anim.setDuration(500);
        anim.setRepeatCount(1);
        anim.setRepeatMode(Animation.REVERSE);
        txtLabel.startAnimation(anim);

        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }
            @Override
            public void onAnimationEnd(Animation animation) {

            }
            @Override
            public void onAnimationRepeat(Animation animation) {
                txtLabel.setText("my next Text"); //for fading back in
            }
        });

然后当然只是放入你的

AlphaAnimation animPulse = new AlphaAnimation(1.0f, 0.0f);
    animPulse.setDuration(500);
    animPulse.setRepeatCount(Animation.INFINITE);
    animPulse.setRepeatMode(Animation.REVERSE);

    imgPulse.startAnimation(animPulse);

然后简单地将所有动画归结为方法并从doAnimations中调用它们。祝你好运。

答案 1 :(得分:0)

你去..

我做了改变背景布局和文字颜色的代码。目前我做的是随机颜色,如果你需要预定义的颜色,请告诉我。以下是我的代码

  1. activity_main.xml中
  2. `

    <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.background_animation.MainActivity"
        android:id="@+id/constraint">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:textSize="30dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    `

    1. MainActivity.java
    2. `

      public class MainActivity extends AppCompatActivity {
      
                  // Move your declarations here if you intend on using them after the onCreate() method
                  ConstraintLayout layout;
                  TextView textView;
      
                  @Override
                  protected void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.activity_main);
      
                      // Inflate your objects
                      layout = (ConstraintLayout) findViewById(R.id.constraint);
                      textView = (TextView) findViewById(R.id.textView);
      
                      // We change the color to RED for the first time as the program loads
                      layout.setBackgroundColor(Color.RED);
      
                      // We change the color to GREEN for the first time as the program loads
                      textView.setTextColor(Color.GREEN);
      
                      // Create the timer object which will run the desired operation on a schedule or at a given time
                      Timer timer = new Timer();
      
                      // Create a task which the timer will execute.  This should be an implementation of the TimerTask interface.
                      // I have created an inner class below which fits the bill.
                      MyTimer myTimer = new MyTimer();
      
                      // We schedule the timer task to run after 1000 ms and continue to run every 1000 ms.
                      timer.schedule(myTimer,1000,1000);
                  }
      
                  // An inner class which is an implementation of the TImerTask interface to be used by the Timer.
                  class MyTimer extends TimerTask{
                      @Override
                      public void run() {
      
                          // This runs in a background thread.
                          // We cannot call the UI from this thread, so we must call the main UI thread and pass a runnable
                          runOnUiThread(new Runnable() {
                              @Override
                              public void run() {
                                  Random rand = new Random();
      
                                  // The random generator creates values between [0,256) for use as RGB values used below to create a random color
                                  // We call the RelativeLayout object and we change the color.  The first parameter in argb() is the alpha.
                                  layout.setBackgroundColor(Color.argb(255, rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)));
                                  textView.setTextColor(Color.argb(222,rand.nextInt(222),rand.nextInt(222),rand.nextInt(222)));
                              }
                          });
                      }
                  }
              }
      

      `

      这是你想要的吗?