4

在 Material Design 视频中,视图的扩展和收缩非常顺畅。在过去,我尝试使用旧的 Android API 来实现这一目标,但它确实成本高昂且速度缓慢。

Android-L 中是否有任何新的 API 可以实现这些效果?

具有http://www.youtube.com/watch?v=Q8TXgCzxEnw#t=26效果的视频

4

1 回答 1

8

您可以使用 android-L 中的新动画 API 轻松创建您在此视频中看到的大部分效果。

显示效果
您可以为剪裁圆设置动画以显示或隐藏视图。这是您在视频中单击“播放”按钮时看到的内容。该按钮展开并显示媒体播放器。

显示隐藏视图的代码(源代码

// 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)
ValueAnimator anim =
    ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
anim.start();

如果您按照上面的链接,您可以找到隐藏视图的代码。

活动进入和退出转换
您可以指定视图如何进入或退出场景。

当前支持的过渡是爆炸、滑动和淡入淡出。android.transition.Visibility也支持任何扩展的转换。

在整个视频中可以看到许多示例。

爆炸退出过渡的代码(来源

// inside your activity
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);

// set an exit transition
getWindow().setExitTransition(new Explode());

活动共享元素转换
您可以指定在两个活动之间共享的元素如何在它们之间转换。

支持的转换是:

  • changeBounds - 动画目标视图布局边界的变化。
  • changeClipBounds - 动画目标视图剪辑边界的变化。
  • changeTransform - 对目标视图的缩放和旋转变化进行动画处理。
  • changeImageTransform - 动画显示图像视图的大小和比例类型的变化。

要进行共享元素转换,您需要执行以下操作:(source

  • 以您的风格启用窗口内容转换。
  • 在您的样式中指定共享元素过渡。
  • 将您的转换定义为 XML 资源。
  • 使用 android:viewName 属性为两个布局中的共享元素分配一个通用名称。
  • 使用ActivityOptions.makeSceneTransitionAnimation方法。

    // get the element that receives the click event
    final View imgContainerView = findViewById(R.id.img_container);
    
    // get the common element for the transition in this activity
    final View androidRobotView = findViewById(R.id.image_small);
    
    // define a click listener
    imgContainerView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(this, Activity2.class);
            // create the transition animation - the images in the layouts
            // of both activities are defined with android:viewName="robot"
            ActivityOptions options = ActivityOptions
                .makeSceneTransitionAnimation(this, androidRobotView, "robot");
            // start the new activity
            startActivity(intent, options.toBundle());
        }
    });
    

我还没有弄清楚的一个动画是同一活动中的展开/收缩动画,可以在日历应用程序中看到。

我唯一能说的是他们使用了高程,因为我们可以看到阴影。我会尝试破解它,看看我是否可以重新创建它。

于 2014-07-03T23:09:52.930 回答