安卓:如何通过编程实现涟漪背景

15

我目前正在创建一款Android应用程序,用户可以输入他们的姓名,按下按钮,然后只是将他们的姓名输出回来。

我想要实现的一个效果是,在他们按下按钮之后,输入和按钮会消失(完成到这一步),然后MainActivity视图的背景颜色将产生涟漪效果(从中心开始)并且变成新的颜色,最终改变整个背景颜色。

由于我只能找到关于在按下按钮时添加涟漪效果的教程,那么我该如何在代码中实现这个效果呢?


3小时内设置赏金。 - SysVoid
你能展示一下你尝试过什么吗? - KishuDroid
什么都不会。我不知道从哪里开始。我是一个Android方面的初学者。 - SysVoid
所以我想首先你需要尝试使用按钮来理解涟漪效应的概念。 - KishuDroid
3个回答

9

编辑:

我通过制作一个小应用程序进行了测试。

首先隐藏您想要在此动画中显示的视图。

该视图可以来自相同的布局,并且在xml中其可见性应为不可见,以便动画将其显示

如果您想创建全屏动画,则可以将视图高度和宽度设置为match parent

将原始视图和要显示的视图都放在帧布局中。

在我的情况下,我使用了这个:

 <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView android:text="Hello World!"                    
                android:layout_width="wrap_content"
                android:textSize="20sp"
                android:layout_height="wrap_content" />
            <LinearLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimaryDark"
            android:id="@+id/revealiew"
            android:visibility="invisible"
            >
</FrameLayout>

然后在您的活动中,当按钮点击或某些事件发生时,请执行以下操作:

    fab.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {
                // previously invisible view
                View myView = findViewById(R.id.revealview);

// get the center for the clipping circle
                int cx = myView.getWidth() / 2;
                int cy = myView.getHeight() / 2;

// get the final radius for the clipping circle
                int finalRadius = Math.max(myView.getWidth(), myView.getHeight());

// create the animator for this view (the start radius is zero)
                Animator anim =
                        ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);

                //Interpolator for giving effect to animation
                anim.setInterpolator(new AccelerateDecelerateInterpolator());
                // Duration of the animation
                anim.setDuration(1000);

// make the view visible and start the animation
                myView.setVisibility(View.VISIBLE);
                anim.start();
            }
        });
    }

此处附有GIF图片,以供参考

您可以在此处查看官方文档的详细信息: http://developer.android.com/training/material/animations.html


你测试过这个吗?你能在赏金中包含我请求的动画gif吗? - SysVoid
很遗憾,它不再让我选择你的答案作为赏金获胜者。甚至不让我给你+25声望。 - SysVoid
我会联系工作人员并寻求帮助。 - SysVoid
@VishavjeetSingh 怎样才能在按钮上启动中心的圆形揭示效果? - Kevan Aghera
只需将cx,cy替换为您的按钮的枢轴x,y坐标: cx = myButton.getPivotX(); cy = myButton.getPivotY(); - Vishavjeet Singh

7

您所描述的是背景上的揭示效果

从官方文档中,您可以找到现成的示例:

1)以下是如何使用揭示效果来显示先前不可见的视图:

 // previously invisible view
 View myView = findViewById(R.id.my_view);

 // get the center for the clipping circle
 int cx = myView.getWidth() / 2;
 int cy = myView.getHeight() / 2;

 // get the final radius for the clipping circle
 int finalRadius = Math.max(myView.getWidth(), myView.getHeight());

 // create the animator for this view (the start radius is zero)
 Animator anim =
     ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);

 // make the view visible and start the animation
 myView.setVisibility(View.VISIBLE);
 anim.start();

2) 这是如何使用揭示效果隐藏先前可见的视图:

 // previously visible view
 final View myView = findViewById(R.id.my_view);

 // get the center for the clipping circle
 int cx = myView.getWidth() / 2;
 int cy = myView.getHeight() / 2;

 // get the initial radius for the clipping circle
 int initialRadius = myView.getWidth();

 // create the animation (the final radius is zero)
 Animator anim =
     ViewAnimationUtils.createCircularReveal(myView, cx, cy, initialRadius, 0);

 // make the view invisible when the animation is done
 anim.addListener(new AnimatorListenerAdapter() {
     @Override
     public void onAnimationEnd(Animator animation) {
         super.onAnimationEnd(animation);
         myView.setVisibility(View.INVISIBLE);
     }
 });

 // start the animation
 anim.start();

在您的应用程序中,您可以使用一个带有彩色背景层(一开始是不可见的),然后在其上使用揭示效果。


非常好的答案。今晚我会尝试一下(当我可以访问AS时),如果它有效,我会将其标记为答案。 - SysVoid
我正在积极尝试,悬赏将在接下来的10-15分钟内颁发。 - SysVoid
似乎没有动画效果,而只是让它出现。在这种情况下,你的XML是怎样的呢? - SysVoid
@SysVoid 你可能做错了什么。从一个空的活动开始尝试这个,使用两个嵌套的FrameLayout,并使用揭示效果显示/隐藏内部的一个。在这里看一下 https://github.com/googlesamples/android-RevealEffectBasic - bonnyz
@SysVoid 可能存在一些问题,例如动画持续时间,过早调用getWidth等,请在此处查看:https://dev59.com/gIbca4cB1Zd3GeqPRyPs - bonnyz
当按钮被点击时,我正在获取宽度。 - SysVoid

-1

被踩了。你从未尝试遵守悬赏的规则。 - SysVoid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接