首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我所有的Android动画都被跳过了

我所有的Android动画都被跳过了
EN

Stack Overflow用户
提问于 2016-10-08 23:24:38
回答 1查看 399关注 0票数 2

我对我在Android上开发的一款游戏有问题。

我的游戏是与许多ObjectAnimatorValueAnimatorThread合作。

一些用户尝试了一个使游戏不可玩的问题:所有的动画都跳过了

当激活耐力模式(在我的Xperia Z3上)时,我可以复制这个bug,但是一些用户也使用S7边缘来复制这个bug。关于信息,耐力模式是一个电池保护程序包括在所有Xperia的手机。

我怎么才能防止这种虫子?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-09 03:53:02

我的动画也有类似的问题。动画是昂贵的电池,所以在电池保护模式下,动画不执行,应用程序跳过状态到结束状态。我曾经使用并确实呈现动画的一种可能性是用自定义实现替换ObjectAnimator和ValueAnimator。下面是一个自定义圆圈视图的例子,我把它按逆时针方向填充。这个动画发生在电池保护模式。

基本上,我找出了动画的持续时间,然后我知道每秒钟完成绘图的百分比,然后画出画布,然后在下一秒画出绘图的百分比。当我不清除画布并保留结束状态时,这会发生直到我到达结束状态为止。主要部分是onDraw方法。

代码语言:javascript
复制
public class AnimatedCircleView extends View {
  private Interpolator INTERPOLATOR = new AccelerateDecelerateInterpolator();

  @IntDef({START, END, ANIMATING})
  @Retention(RetentionPolicy.SOURCE)
  public @interface State {
  }

  private static final int START = 0;
  private static final int END = 1;
  private static final int ANIMATING = 2;

  private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  private final RectF mRect = new RectF();

  @State private int mState = START;
  private long mAnimationStartTime = 0;
  private long mDuration = 0;
  private Listener mListener = null;

  public AnimatedCircleView(Context context) {
    super(context);
  }

  public AnimatedCircleView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public void startCircleAnimation(long durationMillis, Listener listener) {
    mDuration = durationMillis;
    mAnimationStartTime = System.currentTimeMillis();
    mState = ANIMATING;
    mListener = listener;
    invalidate();
  }

  @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mRect.left = 0;
    mRect.right = w;
    mRect.top = 0;
    mRect.bottom = h;
  }

  @Override protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mState == START) {
      // Draw nothing.
      return;
    }

    float percentToDraw;
    if (mState == ANIMATING) {
      float currentAnimationTime = System.currentTimeMillis() - mAnimationStartTime;//Get how much animation time is remaining
      float percentAnimationTime = currentAnimationTime / mDuration;//So now you how much percent you need to draw
      if (percentAnimationTime >= 1f) { //If percent is greater than 100%, you have reached the end
        percentToDraw = 1f;
        mState = END;
        if (mListener != null) mListener.onAnimationEnd();
      } else {
        percentToDraw = INTERPOLATOR.getInterpolation(percentAnimationTime);//Else apply the interpolator
      }
    } else {
      // END state
      percentToDraw = 1f;
    }
    float sweepValue = 360f * percentToDraw; //Multiply the final value of the filled circle with percentToDraw, so you know how much to draw.
    float sweepAngle = -sweepValue; //Draw anti clockwise
    canvas.drawArc(mRect, -90, sweepAngle, false, mPaint); //Draw on canvas

    if (mState == ANIMATING) {
      postInvalidateOnAnimation(); // Clear the drawing as animation is happening
    }
  }

  public interface Listener {
    void onAnimationEnd();
  }
}

在xml中包括此自定义视图如下:

代码语言:javascript
复制
<com.example.sunny.BatteryCircle.AnimatedCircleView
        android:id="@+id/cvAnimatedCircleView"
        android:layout_centerVertical="true"
        android:layout_width="45dp"
        android:layout_height="45dp" />

现在,您可以从活动中调用动画,如下所示:

代码语言:javascript
复制
AnimatedCircleView animatedCircleView = (AnimatedCircleView) findViewById(R.id.cvAnimatedCircleView);
        animatedCircleView.startCircleAnimation(2000, new AnimatedCircleView.Listener() {
            @Override public void onAnimationEnd() {
            }
        });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39938418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档