MaterialDesign相关效果实现

来源:互联网 发布:linux jdk8安装教程 编辑:程序博客网 时间:2024/06/07 05:04
一:水波纹默认的风格实现
    只需要在xml文件中为控件设置背景bacground为:?android:attr/selectableItemBackground

二:还有一种默认的风格是:?android:attr/selectableItemBackgroundBorderless

三:就是通过drawable来实现了。
    该实现方式类似于我们之前经常用的状态选择器。在MaterialDesign中叫RippleDrawable类做background,水波纹效果在不同的状态之间做过渡。
    RippleDrawable(ColorStateList color,Drawable content,Drawable mask0)
    所谓content内容意思是:默认显示的效果。
    下面介绍具体的实现。
    在项目的drawable文件夹下创建一个xml文件,这里我们假设名字为ripple1.xml,下面在该xml文件中写如下内容:
    第一种内容为:
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="#ffffff00"
        <item android:drawable="@android:color/holo_green_dark" />
        <item android:id="@android:id/mask" android:drawable="" />
    </ripple>
    解释:上面的android:color="#ffffff00"代表的就是水波纹的颜色
          <item android:drawable="@android:color/holo_green_dark" />对应的是RippleDrawable中的content内容
          <item android:id="@android:id/mask" android:drawable="" />对应的是RippleDrawable中的mask,其中这里的id是系统的。后面的drawable我没有写,可以是颜色,也可以是shape,也可以是图片。但是有个问题是:只要这里的drawable对应的值不是图片,你改成任何东西,其都没有效果。淡一点的颜色变化,还是有color来决定的。
    第二种内容为椭圆形效果:
        <ripple xmlns:android="http://schemas.android.com/apk/res/android"
            android:color="#ffffff00"
            <item >
                <shape android:shape="oval">
                    <solid android:color="@android:color/holo_green?dark"/>
                </shape>
            < /item>
            
        </ripple>
        解释:这里的item就是RippleDrawable中的content内容,

四:这里介绍的不同于上面介绍的水波纹背景,这里介绍的是Reveal effect物体出现或消失的动画效果
    ViewAnimationUtils.createCircularReveal(View v,int centerX,int centerY,float startRadius,float endRadius)=>Animator
    1)进入效果
       Animator animatior = ViewAnimationUtils.createCircularReveal(btn,btn.getWidth()/2,btn.getHeight()/2,0,btn.getWidth());
       animatior.setInterpolator(new LinearInterpolator());
       animator.setDuration(6000);
       animator.addListener(new AnimatorListenerAdapter(){ //这里只需要实现需要的逻辑即可,比如只实现开始或者结束  });
       animator.start();
    2)消失效果
       Animator animatior = ViewAnimationUtils.createCircularReveal(btn,btn.getWidth()/2,btn.getHeight()/2,btn.getWidth(),0);
       animatior.setInterpolator(new LinearInterpolator());
       animator.setDuration(6000);
       animator.addListener(new AnimatorListenerAdapter(){ //这里只需要实现需要的逻辑即可,比如只实现开始或者结束  });
       animator.start();
    进入和消失的动画,唯一的区别点就在于Animator的最后两个参数上。
五:过渡动画Activity Transitions
    activity的过渡动画:进入,退出,共享元素下的进,出
    进出支持这些transition效果:Explode,Slide和Fade
    共享元素transition效果:changeBounds,changeClipBounds,changeTransform,changeImageTransform
    用Activity.finishAfterTransition()方法,而不是Activit.finish()

    具体的使用,看下面2张代码截图。

                  第二张截图

六:还有一些效果,如属性动画的曲线动画,矢量动画,有时间在整理。

0 0
原创粉丝点击