如何在android中的onPause()和onResume()中暂停和恢复动画?

时间:2017-06-07 23:08:51

标签: android animation

我有一个活动,里面有16个动画按钮。最初它们是不可见的。 之后,我使用for循环每个按钮正在进行淡入淡出动画。 R.anim.fade xml文件位于 @ anim / fade

问题是我无法暂停并恢复它们。我喜欢在活动的 onPause() onResume()方法中暂停和恢复它们。

还有一种检查所有动画是否完整的方法吗?。那就是如何知道 b16按钮这是最后一个按钮完成了它的动画?

这是我的java代码。

package com.ash432.itguesses;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

 public class hints extends AppCompatActivity
 {

private int button_arr[] = {R.id.b1, R.id.b2, R.id.b3, R.id.b4, R.id.b5, 
R.id.b6, R.id.b7, R.id.b8, R.id.b9,
R.id.b10, R.id.b11, R.id.b12, R.id.b13, R.id.b14, R.id.b15, R.id.b16};

Animation anim;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hints);
    int i;
    for (i = 0; i < 16; i++)
        invisible(button_arr[i]);


   for(i=0;i<16;i++)
            anim_button(button_arr[i], 200 * (i + 1));

    }


private void invisible(int id)
{
    Button b = (Button) findViewById(id);
    b.setVisibility(View.INVISIBLE);
}

private void anim_button(int id, long duration)
{
    Button b = (Button) findViewById(id);
    anim = AnimationUtils.loadAnimation(this, R.anim.fade);
    anim.setStartOffset(duration);
    b.startAnimation(anim);
    b.setVisibility(View.VISIBLE);
}

@Override
protected void onResume()
{
   super.onResume();
}

@Override
protected void onPause()
{
    super.onPause();
}
} 

这是我的xml文件

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#040404"
            android:clickable="false"
            android:orientation="vertical"
            android:id="@+id/rel">

    <Button
    android:id="@+id/b1"

    android:textColor="#000000"
    android:textSize="35sp"

    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#252323"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_marginTop="19dp"
    android:gravity="center"
    android:layout_marginLeft="24dp"
    android:layout_marginStart="24dp"
    android:focusable="false"
    android:focusableInTouchMode="false"

    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:id="@+id/b2"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_marginLeft="17dp"
    android:layout_marginStart="16dp"
    android:layout_alignTop="@+id/b1"
    android:layout_toRightOf="@+id/b1"
    android:layout_toEndOf="@+id/b1"
    android:gravity="center" />

<Button
    android:id="@+id/b3"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_marginLeft="17dp"
    android:layout_marginStart="15dp"
    android:layout_alignTop="@+id/b2"
    android:layout_toRightOf="@+id/b2"
    android:layout_toEndOf="@+id/b2"
    android:gravity="center" />

<Button
    android:id="@+id/b4"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_marginLeft="17dp"
    android:layout_marginStart="17dp"
    android:layout_alignTop="@+id/b3"
    android:layout_toRightOf="@+id/b3"
    android:layout_toEndOf="@+id/b3"
    android:gravity="center" />

<Button
    android:id="@+id/b5"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_marginTop="12dp"
    android:layout_below="@+id/b1"
    android:layout_alignLeft="@+id/b1"
    android:layout_alignStart="@+id/b1"
    android:gravity="center" />

<Button
    android:id="@+id/b6"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_alignTop="@+id/b5"
    android:layout_alignLeft="@+id/b2"
    android:layout_alignStart="@+id/b2"
    android:gravity="center" />

<Button
    android:id="@+id/b7"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_alignTop="@+id/b6"
    android:layout_alignLeft="@+id/b3"
    android:layout_alignStart="@+id/b3"
    android:gravity="center" />

<Button
    android:id="@+id/b8"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_alignTop="@+id/b7"
    android:layout_alignLeft="@+id/b4"
    android:layout_alignStart="@+id/b4"
    android:gravity="center" />

<Button
    android:id="@+id/b9"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_marginTop="12dp"
    android:layout_below="@+id/b5"
    android:layout_alignLeft="@+id/b5"
    android:layout_alignStart="@+id/b5"
    android:gravity="center" />

<Button
    android:id="@+id/b10"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_alignTop="@+id/b9"
    android:layout_alignLeft="@+id/b6"
    android:layout_alignStart="@+id/b6"
    android:gravity="center" />

<Button
    android:id="@+id/b11"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_alignTop="@+id/b10"
    android:layout_alignLeft="@+id/b7"
    android:layout_alignStart="@+id/b7"
    android:gravity="center" />

<Button
    android:id="@+id/b12"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_alignTop="@+id/b11"
    android:layout_alignLeft="@+id/b8"
    android:layout_alignStart="@+id/b8"
    android:gravity="center" />

<Button
    android:id="@+id/b13"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_marginTop="12dp"
    android:layout_below="@+id/b9"
    android:layout_alignLeft="@+id/b9"
    android:layout_alignStart="@+id/b9"
    android:gravity="center" />

<Button
    android:id="@+id/b14"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_alignTop="@+id/b13"
    android:layout_alignLeft="@+id/b10"
    android:layout_alignStart="@+id/b10"
    android:gravity="center" />

<Button
    android:id="@+id/b15"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_alignTop="@+id/b14"
    android:layout_alignLeft="@+id/b11"
    android:layout_alignStart="@+id/b11"
    android:gravity="center"
    android:singleLine="false"
    android:longClickable="false" />

<Button
    android:id="@+id/b16"
    android:textColor="#000000"
    android:textSize="35sp"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:background="#FFFFFF"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_alignTop="@+id/b15"
    android:layout_alignLeft="@+id/b12"
    android:layout_alignStart="@+id/b12"
    android:gravity="center" />


   </RelativeLayout>

修改

这是我的 R.anim.fade xml文件。其实我正在尝试闪烁动画。

 <?xml version="1.0" encoding="utf-8"?>
 <set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator"
 >
 <alpha
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="200"
/>
<alpha
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="200"
/>
</set>

1 个答案:

答案 0 :(得分:0)

为了方便暂停/恢复动画,您应该Animator使用minSdkVersion 19。您可以看到here知道如何加载Animator

animator - &gt;的 fade_out.xml

<?xml version="1.0" encoding="UTF-8"?>
<objectAnimator
    android:propertyName="alpha"
    android:valueFrom="0"
    android:valueTo="1"
    xmlns:android="http://schemas.android.com/apk/res/android"/>

然后暂停并恢复所有动画,你可以这样做

List<Animator> mAnimatorList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    anim_button(R.id.btn1, 4000);
    anim_button(R.id.btn2, 6000);
}

private void anim_button(int id, long duration)
{
    Button b = (Button) findViewById(id);
    Animator animator = AnimatorInflater.loadAnimator(this, R.animator.fade_out);
    animator.setTarget(b);
    animator.setDuration(duration);
    animator.start();

    mAnimatorList.add(animator);
}

@Override
protected void onPause() {
    super.onPause();
    for (Animator animator: mAnimatorList) {
        animator.pause();
    }
}

@Override
protected void onResume() {
    super.onResume();
    for (Animator animator: mAnimatorList) {
        animator.resume();
    }
}