在 Material Design 视频中,视图的扩展和收缩非常顺畅。在过去,我尝试使用旧的 Android API 来实现这一目标,但它确实成本高昂且速度缓慢。
Android-L 中是否有任何新的 API 可以实现这些效果?
在 Material Design 视频中,视图的扩展和收缩非常顺畅。在过去,我尝试使用旧的 Android API 来实现这一目标,但它确实成本高昂且速度缓慢。
Android-L 中是否有任何新的 API 可以实现这些效果?
您可以使用 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());
活动共享元素转换
您可以指定在两个活动之间共享的元素如何在它们之间转换。
支持的转换是:
要进行共享元素转换,您需要执行以下操作:(source)
使用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());
}
});
我还没有弄清楚的一个动画是同一活动中的展开/收缩动画,可以在日历应用程序中看到。
我唯一能说的是他们使用了高程,因为我们可以看到阴影。我会尝试破解它,看看我是否可以重新创建它。