我想实现作为新材料的设计规格说明其中有共享元素片段之间的过渡。 我能找到的唯一方法是在ActivityOptionsCompat.makeSceneTransitionAnimation,我相信只有活动的作品。 我一直在寻找这相同的功能,但与/为碎片。
解决方案我有同样的问题,但有它从另一个片段添加新片段工作。 下面的链接是如何开始对这个非常有帮助的:https://developer.android.com/training/material/animations.html#Transitions
以下是我的code,它的工作原理。我的动画的ImageView
从一个片段到另一个。
确保查看
要进行动画处理具有相同的安卓transitionName
中的两个片段。
其他的内容并不重要。
作为一个测试,你可以复制这两种布局的XML文件。确保图像存在。
< ImageView的
机器人:transitionName =MyTransition
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:scaleType =centerCrop
机器人:SRC =@可绘制/ test_image/>
然后,我有我的 RES /转换
文件夹,命名为1文件的 change_image_transform.xml
< XML版本=1.0编码=UTF-8&GT?;
< transitionSet的xmlns:机器人=http://schemas.android.com/apk/res/android>
< changeImageTransform />
< / transitionSet>
现在你可以开始。比方说你有一个片段包含图像,并希望添加片段B。
运行这个片段中的一个:
@覆盖
公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.product_detail_image_click_area:
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.LOLLIPOP){
setSharedElementReturnTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_transform));
setExitTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.explode));
//创建新的片段添加(片段B)
片段片段=新ImageFragment();
fragment.setSharedElementEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_transform));
fragment.setEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.explode));
//我们共同的元素(在片段A)
mProductImage =(ImageView的)mLayout.findViewById(R.id.product_detail_image);
//添加片段B
FragmentTransaction英尺= getFragmentManager()的BeginTransaction()
.replace(R.id.container,片段)
.addToBackStack(交易)
.addSharedElement(mProductImageMyTransition);
ft.commit();
}
其他 {
// code在旧设备上运行
}
打破;
}
}
I am trying to implement transitions between fragments which have "shared elements" as described in the new material design specs. The only method I can find is the ActivityOptionsCompat.makeSceneTransitionAnimation, which I believe works on Activity only. I've been searching for this same functionality but with/for fragments.
解决方案I had the same problem but had it working by adding a new fragment from another fragment. The following link is very helpful in getting started on this: https://developer.android.com/training/material/animations.html#Transitions
Following is my code that works. I'm animating an ImageView
from one fragment to the other.
Make sure the View
you want to animate has the same android:transitionName
in both fragments.
The other content doesn't really matter.
As a test, you could copy this to both your layout xml files. Make sure the image exists.
<ImageView
android:transitionName="MyTransition"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/test_image" />
Then I have 1 file in my res/transition
folder, named change_image_transform.xml.
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeImageTransform />
</transitionSet>
Now you can get started. Lets say you have Fragment A containing the image and want to add Fragment B.
Run this in Fragment A:
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.product_detail_image_click_area:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setSharedElementReturnTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_transform));
setExitTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.explode));
// Create new fragment to add (Fragment B)
Fragment fragment = new ImageFragment();
fragment.setSharedElementEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_transform));
fragment.setEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.explode));
// Our shared element (in Fragment A)
mProductImage = (ImageView) mLayout.findViewById(R.id.product_detail_image);
// Add Fragment B
FragmentTransaction ft = getFragmentManager().beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack("transaction")
.addSharedElement(mProductImage, "MyTransition");
ft.commit();
}
else {
// Code to run on older devices
}
break;
}
}
相关推荐
最新文章