Android高级UI开发(二十六)Material Design动画--水波纹与揭露效果

举报
yd_57386892 发表于 2020/12/29 01:07:39 2020/12/29
【摘要】 今天我们来介绍下Material Design按钮自带的一些动画:水波纹与揭露效果。 源码下载地址:https://download.csdn.net/download/gaoxiaoweiandy/11088325 1. 水波纹效果   1.1  布局文件 <?xml version="1.0" encoding="utf-8"?><LinearLayout xm...

今天我们来介绍下Material Design按钮自带的一些动画:水波纹与揭露效果。

源码下载地址:https://download.csdn.net/download/gaoxiaoweiandy/11088325

1. 水波纹效果

 

1.1  布局文件


  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:gravity="center_horizontal"
  7. android:orientation="vertical"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. android:paddingLeft="@dimen/activity_horizontal_margin"
  10. android:paddingRight="@dimen/activity_horizontal_margin"
  11. android:paddingTop="@dimen/activity_vertical_margin"
  12. tools:context=".LoginActivity">
  13. <Button
  14. android:layout_width="wrap_content"
  15. android:layout_height="100dp"
  16. android:text="按钮默认自带水波纹" />
  17. <TextView
  18. android:layout_width="wrap_content"
  19. android:layout_height="100dp"
  20. android:clickable="true"
  21. android:background="?attr/selectableItemBackground"
  22. android:text="TextView设置clikable后自带水波纹" />
  23. <Button
  24. android:layout_width="wrap_content"
  25. android:layout_height="100dp"
  26. android:background="?attr/selectableItemBackgroundBorderless"
  27. android:text="无边界水波纹" />
  28. <Button
  29. android:id="@+id/btReveal"
  30. android:layout_width="wrap_content"
  31. android:layout_height="100dp"
  32. android:text="揭露效果" />
  33. </LinearLayout>

 

第一个Button默认水波纹效果,第二个Textview需要设置clickable=true才能显示水波纹效果,第三个Button设置backgound来实现无边界水波纹效果。第四个Button id=btReveal将在第2节揭露效果里展示。

目前的运行效果图是:

其中按钮的默认颜色与水波纹颜色可以在style中自定义colorControlHighLight与colorButtonNormal.

 


  
  1. <!-- Base application theme. -->
  2. <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  3. <!-- Customize your theme here. -->
  4. <item name="colorPrimary">@color/colorPrimary</item>
  5. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  6. <item name="colorAccent">@color/colorAccent</item>
  7. <item name="colorControlHighlight">@color/colorPrimary</item>
  8. <item name="colorButtonNormal">@color/material_blue_grey_800</item>
  9. </style>

2. 揭露效果

 


  
  1. package com.example.myapplication
  2. import android.support.v7.app.AppCompatActivity
  3. import android.os.Bundle
  4. import android.view.View
  5. import android.view.ViewAnimationUtils
  6. import android.view.animation.AccelerateInterpolator
  7. import android.widget.Toast
  8. import kotlinx.android.synthetic.main.activity_login.*
  9. /**
  10. */
  11. class MainActivity : AppCompatActivity(){
  12. override fun onCreate(savedInstanceState: Bundle?) {
  13. super.onCreate(savedInstanceState)
  14. setContentView(R.layout.activity_login)
  15. btReveal.setOnClickListener(View.OnClickListener {
  16. Toast.makeText(this,"揭露",Toast.LENGTH_SHORT).show();
  17. var animator = ViewAnimationUtils.createCircularReveal(btReveal, 0, 0, 0f,
  18. Math.hypot(btReveal.getWidth().toDouble(), btReveal.getHeight().toDouble()).toFloat()
  19. );
  20. animator.setDuration(1000);
  21. animator.setInterpolator( AccelerateInterpolator());
  22. animator.start();
  23. /*从按钮中心开始揭露START
  24. var animator1 = ViewAnimationUtils.createCircularReveal(btReveal, btReveal.getWidth()/2, btReveal.getHeight()/2,
  25. 0F,
  26. btReveal.getHeight().toFloat()
  27. );
  28. animator1.setDuration(1000);
  29. animator1.setInterpolator( AccelerateInterpolator());
  30. animator1.start();从按钮中心开始揭露END*/
  31. })
  32. }
  33. }

我们为btReveal定义一个单击函数,然后执行一个揭露动画。使用ViewAnimationUtils调用createCircualReveal来实现一个圆形揭露动画,我们来看一下这个函数的参数:

createCircularReveal(btReveal, 0, 0, 0f,
                Math.hypot(btReveal.getWidth().toDouble(), btReveal.getHeight().toDouble()).toFloat()
            );

第一个参数:btReveal为要显示揭露动画的控件;

第二个参数与第三个参数(0,0)是圆心,这里以按钮的左上角为圆心进行扩散。

第三个参数是,揭露的起始半径。

第四个参数是,揭露的最大半径。

 

 

 

文章来源: blog.csdn.net,作者:冉航--小虾米,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/gaoxiaoweiandy/article/details/89018248

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。