【问题标题】:Implement animation for searching in Android在 Android 中实现搜索动画
【发布时间】:2014-05-17 21:03:39
【问题描述】:

当我的一个进程在后台工作时,我一直在尝试实现一个简单的动画。

动画的要求是围绕屏幕中心的圆形图像视图创建波纹效果。

当个人资料圈周围有一些不断增长的圈子动画时,可以在 Google 环聊视频会话中看到示例,或者在搜索个人资料的 Tinder 等应用程序中。

我试图将大约 4 个可绘制的圆圈放在另一个上,并在 for 循环中改变它们的可见性:

ImageView one = (ImageView) rootView.findViewById(R.id.imageView1);
    ImageView two = (ImageView) rootView.findViewById(R.id.imageView2);
    ImageView three = (ImageView) rootView.findViewById(R.id.imageView3);
    ImageView four = (ImageView) rootView.findViewById(R.id.imageView4);

    try {
        for(int i=1; i<10; i++){
            one.setVisibility(one.VISIBLE);
            Thread.sleep(1000);

            two.setVisibility(two.VISIBLE);
            Thread.sleep(1000);

            three.setVisibility(three.VISIBLE);
            Thread.sleep(1000);

            four.setVisibility(four.VISIBLE);
            Thread.sleep(1000);

            one.setVisibility(one.INVISIBLE);
            Thread.sleep(1000);

            two.setVisibility(two.INVISIBLE);
            Thread.sleep(1000);

            three.setVisibility(three.INVISIBLE);
            Thread.sleep(1000);

            four.setVisibility(four.INVISIBLE);
            Thread.sleep(1000);

        }
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

但是当我运行它时,应用程序崩溃了。有没有更好的方法来做到这一点?

提前致谢。

【问题讨论】:

    标签: android animation


    【解决方案1】:

    这是在单独的线程上执行的吗?如果没有,您应该将其移至 1,因为 Thread.sleep() 应该绝不在主线程上使用。

    话虽如此,您不能在主 UI 线程之外执行与 UI 相关的工作(即setVisibility())。要使用您当前的方法,您需要在每个 setVisibility() 调用周围添加一个 runOnUiThread()

    如果您不想这样做,您可以将代码修改为以下内容:

    ImageView one = (ImageView) rootView.findViewById(R.id.imageView1);
    ImageView two = (ImageView) rootView.findViewById(R.id.imageView2);
    ImageView three = (ImageView) rootView.findViewById(R.id.imageView3);
    ImageView four = (ImageView) rootView.findViewById(R.id.imageView4);
    
    
    final ImageView images[] = {one, two, three, four};
    new Thread() {
    
        public void run() {
    
            for(int i=1; i<10; i++){
                for (int j=0; j<images.length; j++) {
                    images[i].setAlpha(255);
                    images[i].postInvalidate();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
                for (int j=0; j<images.length; j++) {
                    images[i].setAlpha(0);
                    images[i].postInvalidate();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }.start();
    

    【讨论】:

    • 它仍然崩溃:(我也试图让它在 ui 线程上运行但没有帮助 - code getActivity().runOnUiThread(new Runnable() { @Override public void run() { 尝试 { for(int i=1; i
    • 但是我怎样才能在这些语句之间引入延迟。由于没有延迟,我看不到任何转换,for 循环立即结束。
    • 您是否尝试过使用我的代码?我认为使用postInvalidate 执行此操作更直观,因为卸载到 UI 线程已为您处理好了。本质上,唯一需要卸载到 UI 线程的部分是重绘视图。其他一切都可能发生在您的后台线程上。一切仍然按顺序发生。
    • 对不起!你能不能多说点……因为你的代码没有在 UI 线程上运行。此外,如果我使用具有 Thread.sleep() 的代码,则会导致应用程序崩溃。我也尝试使用 one.setAlpha(255);一.postInvalidate(); runOnUiThread 中没有 Thread.sleep() 但我仍然只看到空白屏幕,因为“for”循环已立即结束。
    • 现在看看我的回答。我感觉你没有将这部分卸载到新线程上,所以我也包括了那部分。我还对代码进行了一些清理,以便将来更容易添加/减去图像。
    猜你喜欢
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多