简体   繁体   English

在Android中实现用于搜索的动画

[英]Implement animation for searching in Android

Had been trying to implement a simple animation while one of my process is doing work in the background. 我一直在尝试在后台执行工作时实现简单的动画。

The requirement of animation is to create a ripple effect around a circular image view in the center of screen. 动画的要求是在屏幕中央的圆形图像视图周围创建波纹效果。

Examples can be seen in Google hangouts video session when there is some growing circle animation around profile circle or in apps like Tinder where it searches for profiles. 在个人视频社交圈周围有一些不断增长的动画或在诸如Tinder之类的应用中搜索社交媒体的应用中,可以在Google环聊视频会话中看到示例。

I have tried to put some 4 drawables of circles one over other and alter their visibility in a for loop: 我试着将大约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();
    }

But when I run this, the app is crashing. 但是当我运行此程序时,该应用程序崩溃了。 Is there a better way to do this? 有一个更好的方法吗?

Thanks in advance. 提前致谢。

Is this being executed on a separate thread? 这是在单独的线程上执行的吗? If not, you should move it to one as Thread.sleep() should NEVER be used on the main thread. 如果没有,则应将其移至1,因为Thread.sleep() 切勿在主线程上使用。

With that said, you cannot execute UI related work (ie. setVisibility() ) off of the main UI thread. 话虽如此,您不能在主UI线程之外执行与UI相关的工作(即setVisibility() )。 To use your current approach, you will need to add a runOnUiThread() around each of your setVisibility() calls. 要使用当前方法,您将需要在每个setVisibility()调用周围添加一个runOnUiThread()

If you would rather not do that, you can modify your code to the following: 如果您不想这样做,可以将代码修改为以下内容:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM