【问题标题】:Android circular reveal animation too fastAndroid循环显示动画太快
【发布时间】:2023-03-11 16:55:02
【问题描述】:

我在我的项目中使用了循环显示动画,但它现在可以正常工作。问题是显示发生得很快,你几乎看不到显示,因为它会立即扩展。我尝试设置 anim.setDuration() 但这并没有改变任何东西。我使用了谷歌示例中的代码。

这是我的代码: 查看 myView = getActivity().findViewById(R.id.view_to_expand);

int cx = myView.getRight();
int cy = myView.getBottom();

int finalRadius = Math.max(myView.getWidth(), myView.getHeight());

Animator anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);

myView.setVisibility(View.VISIBLE);
anim.start();

view_to_expand 是一个简单的相对布局,不要以为问题出在哪里。另外,如何将圆形显示应用于动画过渡?

【问题讨论】:

  • 我有类似的问题,原来我在开发者选项中关闭了动画。

标签: android animation view circularreveal


【解决方案1】:

瞬时扩展是因为除了你的动画之外,主线程上还有一些其他繁重的工作(数据更新、其他视图/片段隐藏、渲染刷新等)。

最好的办法是将它包装成一个可运行文件并将其添加到堆栈中。当有可用的 CPU 周期时将调用它并显示您的动画。

以下是创建和使用 runnable 的正确方法。尽量不要发布匿名的,因为有可能用户返回并且您的 runnable 挂起并暴露内存泄漏和/或引发运行时异常。

我在这里假设你的视图所在的类是一个活动,而你的 myView 是你活动中的一个实例引用

public final ApplicationActivity extends Activity {
    private View myView;

    private final Runnable revealAnimationRunnable = new Runnable() {
        @Override
        public void run() {
            int cx = myView.getRight();
            int cy = myView.getBottom();

            int finalRadius = Math.max(myView.getWidth(), myView.getHeight());
            Animator animator = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
            animator.start();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        myView = findViewById(R.id.view_to_expand);
        myView.setVisibility(View.VISIBLE);
        myView.post(revealAnimationRunnable);
        // alternatively, in case load is way too big, you can post with delay
        // i.e. comment above line and uncomment the one below
        // myView.postDelayed(revealAnimationRunnable, 200);
    }

    @Override
    protected void onDestroy() {
        ...
        myView.removeCallbacks(revealAnimationRunnable);
    }
}

【讨论】:

  • 谢谢,会试试看。 @Overrid 中也有一个错字:)
  • @Dimitar Genov 我试过了,这是第一次。但从第二次开始,它又跑得很快。先生有什么办法可以解决这个问题。
  • @user2217535 您是否尝试过稍有延迟,即myView.postDelayed(revealAnimationRunnable, 200)?我需要一些资料来查看,有太多的场景会导致失败 - 例如显示/隐藏您的片段,而不是第二次重新创建它,您的可运行文件在您第二次打开片段时未执行,您的视图仍未附加,依此类推
  • @Dimitar Genov 我尝试延迟但它不起作用,实际上我一直在尝试使用此动画切换两个线性布局。从您的评论中我知道它可以使用片段修复并更改了我的布局碎片............现在效果很好......,谢谢您的支持先生。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多