java - Android:以编程方式使背景波纹化

标签 java android xml user-interface

我目前正在创建一个 Android 应用程序,人们可以在其中输入他们的名字,按下一个按钮,然后它只会将他们的名字输出给他们。

我想用它实现的一个效果是,在他们按下按钮后,输入和按钮将消失(到目前为止完成这一点),然后 MainActivity View 的背景颜色将做一个波纹(从中心开始)具有新颜色,最终改变整个背景颜色。

我将如何以编程方式执行此操作,因为我只能找到有关在按下按钮时向按钮添加波纹的教程?

最佳答案

编辑:

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

首先隐藏你想在这个动画中显示的 View 。

View 可以来自相同的布局,并且在 xml 中它的可见性应该是不可见的,这样动画就会显示它。

如果您想创建一个全屏动画,您可以将 View 的高度和宽度设置为匹配父级...

框架布局中获取原始 View 和显示 View

在我的例子中,我用过这个:

 <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>

然后在您的 button click Activity 或某些事件中执行此操作:

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

Attached GIF for your reference

您可以在这里详细查看官方文档: http://developer.android.com/training/material/animations.html

关于java - Android:以编程方式使背景波纹化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33358183/

相关文章:

java - 自定义 Grails 异常处理

java - 将正确的 xml 转换为不正确的 xml

java - android:从List<HashMap>中将数据存储在Sqlite中

java - 给定斐波那契方法/函数,实现它并使其高效

Java spring application.properties 值没有被应用程序使用

Android支持库、编译SDK版本和最低SDK版本

java - 无法运行程序 "adb": error=2, 通过 Eclipse 执行时没有这样的文件或目录

android - GetView方法和convertView的误解?

php - 转发: Creating a RSS feed with PHP

继承自两个父主题的 Android XML 主题?