java Activity/Fragment Transitions 与 Pre-Lollipop 设备兼容吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26567377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:09:27  来源:igfitidea点击:

Are Activity/Fragment Transitions compatible with pre-Lollipop devices?

javaandroidmaterial-designactivity-transitionshared-element-transition

提问by Cheborra

I'm trying to make an Activity Transition using Shared Elements on a pre-Lollipop device (4.x). Is it possible? So far, I'm trying this:

我正在尝试在 Lollipop 之前的设备 (4.x) 上使用共享元素进行活动转换。是否可以?到目前为止,我正在尝试这个:

public class RewardDetail extends ActionBarActivity {
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        ...

        ViewCompat.setTransitionName(imageView, TRANSITION_NAME);
    }

    ...

    public static void launch(ActionBarActivity activity, View transitionView, WelcomeReward detailData) {
        ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, transitionView, TRANSITION_NAME);
        Intent intent = new Intent(activity, RewardDetail.class);
        intent.putExtra(PARAM_DATA, detailData);
        ActivityCompat.startActivity(activity, intent, options.toBundle());
    }
}

called by:

调用者:

@Override
public void onClick(final View v) {
    int position = recyclerView.getChildPosition(v);
    WelcomeReward welcomeReward = data.get(position);
    RewardDetail.launch(WelcomeRewardActivity.this, v.findViewById(R.id.reward_view), welcomeReward);
}

But it results in a "regular" transition (no shared element). Any ideas?

但它会导致“常规”转换(没有共享元素)。有任何想法吗?

EDIT

编辑

According to this video, it could be done:

根据这个视频,它可以做到:

https://www.youtube.com/watch?v=RhiPJByIMrM&index=8&list=WL

https://www.youtube.com/watch?v=RhiPJByIMrM&index=8&list=WL

Is there a library already implementing this for pre Lollipop ?

是否有图书馆已经在 Lollipop 之前实现了这个?

回答by Alex Lockwood

No, Activity/Fragment Transitions are not possible on pre-Lollipop devices. According to the documentation:

不,活动/片段转换在棒棒糖之前的设备上是不可能的。根据文档

Start an activity with additional launch information, if able.

In Android 4.1+ additional options were introduced to allow for more control on activity launch animations. Applications can use this method along with ActivityOptionsCompat to use these animations when available. When run on versions of the platform where this feature does not exist the activity will be launched normally.

如果可以的话,用额外的启动信息开始一个活动。

在 Android 4.1+ 中引入了其他选项,以允许对活动启动动画进行更多控制。应用程序可以将此方法与 ActivityOptionsCompat 一起使用,以在可用时使用这些动画。当在不存在此功能的平台版本上运行时,活动将正常启动。

See also George Mount's answer to this StackOverflow question.

另请参阅 George Mount对此 StackOverflow 问题的回答。

回答by Albin Mathew

You can check out this library for activity and fragment transitions for pre lollipop devices

您可以查看此库以了解预棒棒糖设备的活动和片段转换

dependencies {
        compile 'com.albinmathew:PreLollipopTransition:1.1.2'
}

https://github.com/albinmathew/PreLollipopTransition

https://github.com/albinmathew/PreLollipopTransition

回答by Dave Jensen

Although the fancy Lollipop Activity/Fragment transitions are not available pre-Lollipop (without the use of a 3rd party library), you can still override the animation used to transition between activities.

虽然花哨的 Lollipop Activity/Fragment 过渡在 Lollipop 之前不可用(不使用 3rd 方库),但您仍然可以覆盖用于在 Activity 之间过渡的动画。

Just before/after you start invoke startActivity() you can make a call to [Activity.overridePendingTransition](http://developer.android.com/reference/android/app/Activity.html#overridePendingTransition(int, int)). When you leave your activity, call the same method.

在开始调用 startActivity() 之前/之后,您可以调用 [Activity.overridePendingTransition]( http://developer.android.com/reference/android/app/Activity.html#overridePendingTransition(int, int))。当您离开活动时,调用相同的方法。

Similarly you can use ActivityOptionsCompat to define a custom animation to use during a transition.

同样,您可以使用 ActivityOptionsCompat 来定义要在过渡期间使用的自定义动画。

ActivityOptionsCompat opts =
    ActivityOptionsCompat.makeCustomAnimation(getActivity(), R.anim.in, R.anim.out);
startActivity(intent, opts.toBundle());

回答by Mdlc

There is a support library, but it does not support (all) transitions on Android versions below 5.0. There are however some alternatives:

有一个支持库,但它不支持 Android 5.0 以下版本的(所有)转换。然而,有一些替代方案:

Unofficial Compatibility libraries
https://github.com/andkulikov/transitions-everywhere
https://github.com/takahirom/PreLollipopTransition
https://github.com/lgvalle/Material-Animations

Android KitKat
http://www.doubleencore.com/2013/11/new-transitions-framework/and a sample found in your SDK samples folder.

非官方兼容性库
https://github.com/andkulikov/transitions-everywhere
https://github.com/takahirom/PreLollipopTransition
https://github.com/lgvalle/Material-Animations

Android KitKat
http://www.doubleencore.com/2013/11/new-transitions-framework/以及在您的 SDK 示例文件夹中找到的示例。

Posted earlier to a duplicate of this question here: https://stackoverflow.com/a/27344471/1683141

早些时候发布到这里问题的副本:https: //stackoverflow.com/a/27344471/1683141