android – 如何在2个活动之间启动共享元素转换?

android – 如何在2个活动之间启动共享元素转换?,第1张

概述我刚学习 Android中的共享元素,我有一个图像,其中一个在ActivitySplashScreen中,另一个在工具栏中,我想使用共享元素将此图像从ActivitySplashScreen移动到ActivityMain 经过一些搜索并尝试实现这种简单的方法使这个功能对我的代码不起作用,例如: style.xml: <style name="AppTheme" parent="BaseTheme" @H_419_1@ 我刚学习 Android中的共享元素,我有一个图像,其中一个在ActivitySplashScreen中,另一个在工具栏中,我想使用共享元素将此图像从ActivitySplashScreen移动到ActivityMain

经过一些搜索并尝试实现这种简单的方法使这个功能对我的代码不起作用,例如:

style.xml:

<style name="Apptheme" parent="Basetheme">    <item name="androID:windowContentTransitions">true</item></style><style name="Basetheme" parent="theme.AppCompat.light.NoActionbar">    <item name="colorPrimary">@color/colorPrimary</item>    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>    ...</style>

ActivitySplashScreen.java:

@OverrIDeprotected voID onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_splash_screen);    ButterKnife.bind(this);    new Handler().postDelayed(new Runnable() {        @OverrIDe        public voID run() {            Intent intent = new Intent(ActivitySplashScreen.this,MainActivity.class);            ActivityOptionsCompat options = ActivityOptionsCompat.                    makeSceneTransitionAnimation(ActivitySplashScreen.this,app_logo,VIEwCompat.getTransitionname(app_logo));            startActivity(intent,options.toBundle());            finish();        }    },3000);}

ActivityMain.java:

@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    supportPostponeEnterTransition();}

我的ActivitySplashScreen和ActivityMain xml布局上的ImageVIEw小部件和:

<ImageVIEw    androID:ID="@+ID/instagram_add_story"    androID:layout_wIDth="40dp"    androID:layout_height="40dp"    androID:layout_margintop="8dp"    androID:layout_marginEnd="16dp"    androID:layout_marginBottom="8dp"    androID:src="@drawable/img_wizard_1"    androID:Transitionname="app_logo"    app:layout_constraintBottom_toBottomOf="parent"    app:layout_constraintEnd_toEndOf="parent"    app:layout_constrainttop_totopOf="parent"    tools:ignore="VectorDrawableCompat" />

这段代码不能正常工作,我不确定究竟是什么问题

更新

ActivitySplashScreen xml:

<?xml version="1.0" enCoding="utf-8"?><androID.support.constraint.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent">    <linearLayout        androID:ID="@+ID/alachiq_header_animation"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:layout_gravity="center"        androID:background="@drawable/instagram_animation_gradIEnt_List"        androID:gravity="center"        androID:orIEntation="vertical"        androID:paddingtop="40dp">        <ImageVIEw            androID:ID="@+ID/app_logo"            androID:layout_wIDth="150dp"            androID:layout_height="150dp"            androID:Transitionname="app_logo"            androID:tint="@androID:color/white"            app:srcCompat="@drawable/img_wizard_1" />        <TextVIEw            androID:ID="@+ID/Title"                        androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:layout_marginBottom="8dp"            androID:FontFamily="@Font/iran_sans_bold"            androID:gravity="center"            androID:text="@string/app_name"            androID:textcolor="@color/mdtp_white" />        <TextVIEw                        androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:FontFamily="@Font/iran_sans_light"            androID:gravity="center"            androID:textcolor="@color/mdtp_white" />    </linearLayout></androID.support.constraint.ConstraintLayout>

MainActivity xml:

<?xml version="1.0" enCoding="utf-8"?><androID.support.design.Widget.CoordinatorLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:slIDingLayer="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:background="@color/grey_5"    androID:orIEntation="vertical">    <androID.support.design.Widget.AppbarLayout        androID:ID="@+ID/appbar"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:elevation="5dp"        androID:theme="@style/Apptheme.AppbarOverlay">        <androID.support.v7.Widget.Toolbar            androID:ID="@+ID/toolbar"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:background="@androID:color/white"            app:contentInsetStartWithNavigation="0dp"            app:popuptheme="@style/themeOverlay.AppCompat.light"            app:theme="@style/Toolbar.light">            <androID.support.constraint.ConstraintLayout                androID:layout_wIDth="match_parent"                androID:layout_height="match_parent">                <TextVIEw                    androID:ID="@+ID/activityTitle"                                        androID:layout_wIDth="90dp"                    androID:layout_height="30dp"                    androID:layout_margintop="8dp"                    androID:layout_marginEnd="8dp"                    androID:layout_marginBottom="8dp"                    androID:FontFamily="@Font/iran_sans_bold"                    androID:gravity="center|right"                    androID:text="@string/app_name"                    app:layout_constraintBottom_toBottomOf="parent"                    app:layout_constraintEnd_toStartOf="@+ID/application_logo"                    app:layout_constrainttop_totopOf="parent" />                <ImageVIEw                    androID:ID="@+ID/application_logo"                    androID:layout_wIDth="40dp"                    androID:layout_height="40dp"                    androID:layout_margintop="8dp"                    androID:layout_marginEnd="8dp"                    androID:scaleType="centerCrop"                    androID:layout_marginBottom="8dp"                    androID:src="@drawable/ic_app_logo"                    androID:Transitionname="app_logo"                    app:layout_constraintBottom_toBottomOf="parent"                    app:layout_constraintEnd_toStartOf="@+ID/drawerMenu"                    app:layout_constrainttop_totopOf="parent"                    app:layout_constraintVertical_bias="0.0"                    tools:ignore="VectorDrawableCompat" />            </androID.support.constraint.ConstraintLayout>        </androID.support.v7.Widget.Toolbar>    </androID.support.design.Widget.AppbarLayout></androID.support.design.Widget.CoordinatorLayout>
解决方法 我可以这样使用这里是根据你的场景我的解决方案.

样式:

<resources>    <!-- Base application theme. -->    <style name="Apptheme" parent="theme.AppCompat.light.NoActionbar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>    </style></resources>

Splash.XML

<?xml version="1.0" enCoding="utf-8"?><androID.support.constraint.ConstraintLayout     xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent">    <linearLayout        androID:ID="@+ID/alachiq_header_animation"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:layout_gravity="center"        androID:background="@drawable/ic_launcher_background"        androID:gravity="center"        androID:orIEntation="vertical"        androID:paddingtop="40dp">        <ImageVIEw            androID:ID="@+ID/app_logo"            androID:layout_wIDth="150dp"            androID:layout_height="150dp"            androID:tint="@androID:color/black"            app:srcCompat="@androID:drawable/ic_dialog_email" />        <TextVIEw            androID:ID="@+ID/Title"                        androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:layout_marginBottom="8dp"            androID:FontFamily="sans-serif"            androID:gravity="center"            androID:text="@string/app_name"            androID:textcolor="@androID:color/background_dark" />        <TextVIEw                        androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:gravity="center"            androID:FontFamily="sans-serif"            androID:textcolor="@androID:color/background_dark" />    </linearLayout></androID.support.constraint.ConstraintLayout>

Splash.Java

public class SplashActivity extends AppCompatActivity {    @BindVIEw(R.ID.app_logo)    ImageVIEw app_logo;    Activity mActivity;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_splash);        ButterKnife.bind(this);        mActivity = this;        VIEwCompat.setTransitionname(app_logo,"app_logo");        new Handler().postDelayed(new Runnable() {            @RequiresAPI(API = Build.VERSION_CODES.JELLY_BEAN)            @OverrIDe            public voID run() {                Intent intent = new Intent(SplashActivity.this,MainActivity.class);                intent.putExtra("Transition_name",VIEwCompat.getTransitionname(app_logo));                ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(mActivity,VIEwCompat.getTransitionname(app_logo));                startActivity(intent,options.toBundle());                finish();            }        },3000);    }}

MainActivity.XML

<?xml version="1.0" enCoding="utf-8"?><androID.support.design.Widget.CoordinatorLayout     xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:slIDingLayer="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:background="@androID:color/darker_gray"    androID:orIEntation="vertical">    <androID.support.design.Widget.AppbarLayout        androID:ID="@+ID/appbar"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:elevation="5dp">        <androID.support.v7.Widget.Toolbar            androID:ID="@+ID/toolbar"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:background="@androID:color/white"            app:contentInsetStartWithNavigation="0dp"            >            <androID.support.constraint.ConstraintLayout                androID:layout_wIDth="match_parent"                androID:layout_height="match_parent">                <TextVIEw                    androID:ID="@+ID/activityTitle"                    androID:layout_wIDth="90dp"                    androID:layout_height="30dp"                    androID:layout_margintop="8dp"                    androID:layout_marginEnd="8dp"                    androID:layout_marginBottom="8dp"                    androID:gravity="center|right"                    androID:text="@string/app_name"                    app:layout_constraintBottom_toBottomOf="parent"                    app:layout_constraintEnd_toStartOf="@+ID/application_logo"                    app:layout_constrainttop_totopOf="parent"                    androID:layout_marginRight="8dp" />                <ImageVIEw                    androID:ID="@+ID/application_logo"                    androID:layout_wIDth="40dp"                    androID:layout_height="40dp"                    androID:layout_margintop="8dp"                    androID:layout_marginEnd="8dp"                    androID:scaleType="centerCrop"                    androID:layout_marginBottom="8dp"                    androID:tint="@androID:color/black"                    app:srcCompat="@androID:drawable/ic_dialog_email"                    app:layout_constraintBottom_toBottomOf="parent"                    app:layout_constrainttop_totopOf="parent"                    app:layout_constraintVertical_bias="0.0"                    androID:layout_marginRight="8dp" />            </androID.support.constraint.ConstraintLayout>        </androID.support.v7.Widget.Toolbar>    </androID.support.design.Widget.AppbarLayout></androID.support.design.Widget.CoordinatorLayout>

MainActivity.Java

public class MainActivity extends AppCompatActivity {    @BindVIEw(R.ID.toolbar)    Toolbar mToolbar;    Activity mActivity;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        ButterKnife.bind(this);        mActivity = this;        setSupportActionbar(mToolbar);        ActivityCompat.postponeEnterTransition(this);        ImageVIEw application_logo = (ImageVIEw) mToolbar.findVIEwByID(R.ID.application_logo);        if (getIntent() != null) {            Bundle extras = getIntent().getExtras();            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LolliPOP) {                String imageTransitionname = extras.getString("Transition_name");                application_logo.setTransitionname(imageTransitionname);                ActivityCompat.startPostponedEnterTransition(mActivity);            }        }    }}

以下是根据您的方案的SharedElementTransition的示例代码.每当你使用某种风格总是写在样式文件中然后使用它.

Note: import androID.support.v7.Widget.Toolbar; for Toolbar in Java file. This is working fine.

总结

以上是内存溢出为你收集整理的android – 如何在2个活动之间启动共享元素转换?全部内容,希望文章能够帮你解决android – 如何在2个活动之间启动共享元素转换?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1126233.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存