AnimationSet性能问题 [英] AnimationSet performance issue

查看:80
本文介绍了AnimationSet性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在Android中处理动画,并希望正确进行。因此,基本上,我想实现以下内容:

I started working with animation in Android and want to do it correctly. So, basically, I want to implement something like this:

我要这样做的代码:

frg_wave.xml

frg_wave.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/content"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

<View
    android:id="@+id/wave_view1"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:background="@drawable/wave_circle"
    android:layout_gravity="center"

    tools:alpha="0.2"
    />

<View
    android:id="@+id/wave_view2"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:background="@drawable/wave_circle"
    android:layout_gravity="center"
    />

<View
    android:id="@+id/wave_view3"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:background="@drawable/wave_circle"
    android:layout_gravity="center"
    />

<View
    android:id="@+id/wave_view4"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_gravity="center"
    android:background="@drawable/wave_circle"
    />

<View
    android:id="@+id/wave_view5"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_gravity="center"
    android:background="@drawable/wave_circle"
    />

在Fragment I have

and in Fragment I have

 private View mWave1;
private View mWave2;
private View mWave3;
private View mWave4;
private View mWave5;

private AnimatorSet setAnimation;



PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat(View.SCALE_X, 10F);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 10F);
PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat(View.ALPHA, 1F, 0.05F);

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frg_wave, container, false);
    mWave1 = view.findViewById(R.id.wave_view1);
    mWave2 = view.findViewById(R.id.wave_view2);
    mWave3 = view.findViewById(R.id.wave_view3);
    mWave4 = view.findViewById(R.id.wave_view4);
    mWave5 = view.findViewById(R.id.wave_view5);
    setupAnimatorSet();
    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setAnimation.start();
}

private void setupAnimatorSet(){
    setAnimation = new AnimatorSet();

    setAnimation.play(waveAnimation(mWave1));

    setAnimation.play(waveAnimation(mWave2)).after(1000L);

    setAnimation.play(waveAnimation(mWave3)).after(2000L);

    setAnimation.play(waveAnimation(mWave4)).after(3000L);

    setAnimation.play(waveAnimation(mWave5)).after(4000L);

}



private ObjectAnimator waveAnimation(View view){
    ObjectAnimator scaleAnimation =
            ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, alpha);
    scaleAnimation.setRepeatCount(ValueAnimator.INFINITE);
    scaleAnimation.setDuration(5000L);
    return scaleAnimation;
}

和wave_circle.xml是

and wave_circle.xml is

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid android:color="@color/colorAccent"/>

似乎不错,但我启用了GPU渲染,发现我的执行效果不是很好。我非常接近极限,如果添加其他动画(例如视图旋转),我将超过16毫秒的极限。

It seems to work fine but I enable GPU rendering and see that I have not very good implementation. I very close to limit, and if I add other animation (for example rotation of view) I'll be over the limit of 16 ms.

有人知道我在做什么错吗?
P.S我也尝试使用 android:hardwareAccelerated = true 进行操作,但对我没有太大帮助。

Does anyone know, what I'm doing wrong? P.S I also tried to do with android:hardwareAccelerated="true" and it's not helped me a lot.

推荐答案

多比例动画会给 RenderAnimator 增添负担,这就是为什么FPS下降。您还可以为 ObjectAnimator 解析对象的属性释放一些fps,而可以尝试使用 ViewPropertyAnimator

Multiple scale animations burden the RenderAnimator, that is why you are experiencing FPS drop. You also loose some fps for the ObjectAnimator resolve properties of the object, instead you could try to use ViewPropertyAnimator.

您可以通过对 ImageView Matrix 进行动画处理来改善此效果 setScale(xScale,yScale)。这意味着您必须使用某些 ValueAnimator 仅对比例因子进行动画处理,并应用 Matrix 比例因子并将其设置为您的 ImageView s:

You can improve this by animating ImageView Matrix, it has setScale(xScale, yScale). Which means you have to use some ValueAnimator to only animate the scale factors, apply the Matrix scale factors and set it to your ImageViews:

Matrix matrix = new Matrix();
matrix.setScale(scale, scale, mPivotX, mPivotY);
mTargetScalingView.setImageMatrix(matrix);

但是,如果您在画布,这是相对容易的事情,然后仅设置圆的半径动画,这将模仿您尝试实现的缩放比例。我想您想获得tinder应用程序具有的动画,这基本上是增加一个圆的半径并减小纯色的alpha值,并在颜色达到完全透明所需的一半时间内生成一个新的圆(是的) ,这就是我所看到的)。如果您想达到60fps, Canvas 动画就很棒。

But best performance you will achieve, if you draw the circles on Canvas, which is relatively easy thing to do, and then only animate the radius of the circles, which will mimic the scaling you are trying to achieve. I guess that you want to achieve animation that tinder app has, which basically is increasing the radius of a circle and decreasing the alpha of the solid color and spawns new circle to the half of the time it takes for the color reach full transparency (yeah, this is just what I see). Canvas animations are great if yo want to achieve 60fps.

请注意,请勿执行任何繁重的迭代/此处的操作,否则您将无法获得60fps。

Be careful you don't perform any heavy iterations/operations here, otherwise you will not get 60fps.

这会将视图层次结构缩减为只有一个视图,而不是为您要设置动画的wave数量,这是另一个胜利!

This will cut the view hierarchy down to having only one view, instead of having for the number of waves you are trying to animate, which is another win!

这篇关于AnimationSet性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆