详解Android Material Design自定义动画的编写

2019-12-10 18:30:06于海丽

在多数情况下,你需要在view的xml定义中,定义它的背景:

  • android:attr/selectableItemBackground                              有界限的波纹   
  • android:attr/selectableItemBackgroundBorderless             延伸到view之外的波纹     note:该属性为api21添加

    或者,你可以用xml定义一个RippleDrawable类型的资源,并使用波纹属性。

    你可以指定一个颜色给RippleDrawable对象,以改变它的默认触摸反馈颜色,使用主题的android:colorControlHighlight属性。
    Use the Reveal Effect  使用展现效果
    ViewAnimationUtils.createCircularReveal()方法使您能够激活一个循环显示或隐藏一个视图。
    显示:

    // previously invisible view
    View myView = findViewById(R.id.my_view);
    
    // get the center for the clipping circle
    int cx = (myView.getLeft() + myView.getRight()) / 2;
    int cy = (myView.getTop() + myView.getBottom()) / 2;
    
    // get the final radius for the clipping circle
    int finalRadius = myView.getWidth();
    
    // create and start the animator for this view
    // (the start radius is zero)
    Animator anim =
      ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
    anim.start();
    隐藏
    // previously visible view
    final View myView = findViewById(R.id.my_view);
    
    // get the center for the clipping circle
    int cx = (myView.getLeft() + myView.getRight()) / 2;
    int cy = (myView.getTop() + myView.getBottom()) / 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();
    
    

    Customize Activity Transitions  定义Activity的过渡动画