使用Android共享元素过渡动画时,背景屏幕出现了一小部分时间。

huangapple go评论66阅读模式
英文:

Background screen appearing for a fraction of second when using android shared element transition animation

问题

我正在尝试构建我的第一个Android应用程序,其中有两个活动(第一个是闪屏屏幕,第二个是登录屏幕),我在从第一个活动切换到第二个活动时使用了共享元素过渡。但是我做错了一些事情,因此过渡不平滑,有一瞬间,两个活动之间会出现背景屏幕。请参考GIF以更好地理解。

我能够捕获出现在活动之间的帧。它看起来像下面这样,

您可以看到第一个活动的ImageView仍然可见在帧中。甚至包含"Hello, Welcome back"的textView也可见。

我在下面分享活动布局和相应的Java文件,

activity_main.xml(闪屏屏幕)

<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity"
    android:background="#1f1f1d"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/splashLogo"
        android:layout_width="180dp"
        android:layout_height="80dp"
        android:background="@drawable/splash"
        android:transitionName="logo_anim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_login.xml(登录屏幕)

<LinearLayout 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"
    android:background="#1f1f1d"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="160dp"
        android:layout_height="90dp"
        android:layout_marginTop="30dp"
        android:layout_marginStart="20dp"
        android:transitionName="logo_anim"
        app:srcCompat="@drawable/splash" />

    <TextView
        android:id="@+id/textViewWelcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:fontFamily="@font/bungee"
        android:includeFontPadding="false"
        android:paddingTop="0dp"
        android:layout_marginTop="0dp"
        android:text="Hello,\nWelcome Back"
        android:textColor="#BFAD0F"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25dp"
        android:layout_marginTop="40dp"
        android:fontFamily="@font/antic"
        android:text="Login to continue"
        android:textColor="#CCC588"
        android:textSize="18sp" />

    <!-- 此部分包含登录表单和按钮,由于上下文中似乎不需要,因此不添加它们 -->
</LinearLayout>

main_activity.java(闪屏屏幕)

private static int SPLASH_TIMEOUT = 3000;

Animation splashAnimation;
View splashView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().setExitTransition(null);
    setContentView(R.layout.activity_main);

    splashAnimation = AnimationUtils.loadAnimation(this, R.anim.splash_animation);
    splashView = findViewById(R.id.splashLogo);
    splashView.setAnimation(splashAnimation);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(MainActivity.this, login.class);
            ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, splashView, ViewCompat.getTransitionName(splashView));
            startActivity(intent, optionsCompat.toBundle());

            finish();
        }
    }, SPLASH_TIMEOUT);
}

login.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    getWindow().setStatusBarColor(Color.parseColor("#1f1f1d"));
    getWindow().setEnterTransition(null);
    setContentView(R.layout.activity_login);
}

@Override
public void onBackPressed() {
    finish();
}

我尝试了很多关于共享元素过渡闪烁/闪烁的不同Stack Overflow问题中提到的解决方案,但都没有奏效。有谁可以指出我在这里做错了什么?

提前感谢。

英文:

I am trying to build my first android application where I have two activities (first one is the splash screen and second one is login screen) and I am using shared element transition when moving from first activity to the second one. But I am doing something wrong because of which the transition is not smooth and for a split second of time, the background screen is appearing between the two activities. Refer to the GIF for better understanding

使用Android共享元素过渡动画时,背景屏幕出现了一小部分时间。

I was able to capture the frame that was appearing between the activities. It looks like following,

使用Android共享元素过渡动画时,背景屏幕出现了一小部分时间。

You can see the ImageView of first activity is still visible in the frame. Even the textView containing "Hello, Welcome back" is also visible.

I am sharing the activity layout and corresponding java files below,

activity_main.xml (splash screen)

&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;
    android:background=&quot;#1f1f1d&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;ImageView
        android:id=&quot;@+id/splashLogo&quot;
        android:layout_width=&quot;180dp&quot;
        android:layout_height=&quot;80dp&quot;
        android:background=&quot;@drawable/splash&quot;
        android:transitionName=&quot;logo_anim&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

activity_login.xml (login screen)

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:background=&quot;#1f1f1d&quot;

    android:orientation=&quot;vertical&quot;&gt;

    &lt;ImageView
        android:id=&quot;@+id/imageView2&quot;
        android:layout_width=&quot;160dp&quot;
        android:layout_height=&quot;90dp&quot;
        android:layout_marginTop=&quot;30dp&quot;
        android:layout_marginStart=&quot;20dp&quot;
        android:transitionName=&quot;logo_anim&quot;
        app:srcCompat=&quot;@drawable/splash&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/textViewWelcome&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;25dp&quot;
        android:fontFamily=&quot;@font/bungee&quot;
        android:includeFontPadding=&quot;false&quot;
        android:paddingTop=&quot;0dp&quot;
        android:layout_marginTop=&quot;0dp&quot;
        android:text=&quot;Hello,\nWelcome Back&quot;
        android:textColor=&quot;#BFAD0F&quot;
        android:textSize=&quot;24sp&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/textView2&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;25dp&quot;
        android:layout_marginTop=&quot;40dp&quot;
        android:fontFamily=&quot;@font/antic&quot;
        android:text=&quot;Login to continue&quot;
        android:textColor=&quot;#CCC588&quot;
        android:textSize=&quot;18sp&quot; /&gt;

    &lt;LinearLayout
        // This part has the login form and buttons,
        // not adding them here as they seems to be 
        // unnecessary in this context
    &lt;/LinearLayout&gt;
&lt;/LinearLayout&gt;

main_activity.java (splash)

private static int SPLASH_TIMEOUT = 3000;

Animation splashAnimation;
View splashView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().setExitTransition(null);
    setContentView(R.layout.activity_main);

    splashAnimation = AnimationUtils.loadAnimation(this, R.anim.splash_animation);
    splashView = findViewById(R.id.splashLogo);
    splashView.setAnimation(splashAnimation);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(MainActivity.this, login.class);
            ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, splashView, ViewCompat.getTransitionName(splashView));
            startActivity(intent, optionsCompat.toBundle());

            finish();
        }
    }, SPLASH_TIMEOUT);
}

login.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    getWindow().setStatusBarColor(Color.parseColor(&quot;#1f1f1d&quot;));
    getWindow().setEnterTransition(null);
    setContentView(R.layout.activity_login);
}

@Override
public void onBackPressed() {
    finish();
}

I have tried solution stated in lot of different stackoverflow questions about blinking/flashing shared element transition, nothing worked. Can anyone point out what I am doing wrong here ?

Thanks in advance.

答案1

得分: 1

发生这种情况是因为您在调用finish()时,所要调用的活动尚未准备好。

您可以通过在onStop方法中添加finish来解决此问题。

@Override
public void onStop() {
   super.onStop();
   finish(); 
}

您可以在此答案中找到更多详细信息:https://stackoverflow.com/a/29663116

英文:

It happens because the activity you're trying to call is not ready when you're calling finish()

You can solve it by adding the finish in onStop method

@Override
public void onStop() {
   super.onStop();
   finish(); 
}

you can find more detail on this answer https://stackoverflow.com/a/29663116

huangapple
  • 本文由 发表于 2020年8月12日 01:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63363850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定