1

我正在创建一个 android 应用程序(棒棒糖版本)。应用程序在初始屏幕中间显示大徽标。登录屏幕顶部包含一个小尺寸的徽标。我使用 ActivityOptions.makeSceneTransitionAnimation() 将动画从初始屏幕的大徽标设置为登录屏幕的小徽标。

启动画面启动以启动应用程序。延迟几毫秒后,闪屏会为登录屏幕创建意图。还要设置过渡。然后它开始登录活动。它开始标志的过渡动画。并成功显示登录屏幕。到目前为止,一切都运行良好,动画也很流畅。

然后我添加了“finish();” 在启动屏幕中,以便登录屏幕上的后退按钮不会加载启动屏幕。现在过渡正在闪烁。

我尝试了以下方法,但仍然存在闪烁。

  1. 使用“finishAfterTransition();” 而不是“完成();”
  2. 为意图添加了 FLAG_ACTIVITY_CLEAR_TOP 和 FLAG_ACTIVITY_NEW_TASK 标志(这会导致连线行为)
  3. 在登录屏幕中覆盖 onBackPressed()。并添加
    完成();android.os.Process.killProcess(android.os.Process.myPid()); - 这会终止应用程序但会再次重新启动它。

在这里,我正在粘贴 splashScreen 的代码。此代码正在运行,但在转换时会出现闪烁。类末尾的gotoLoginScreen()方法是加载登录活动的地方。登录屏幕是来自 android studio 的基本活动模板。使用 com.android.support:appcompat-v7:21.0.2 库支持低端设备。

公共类 SplashActivity 扩展 ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_splash, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    ImageView imageView_logo;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_splash, container, false);
        imageView_logo = (ImageView) rootView.findViewById(R.id.imageview_logo);
        imageView_logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTimer();
            }
        });
        startTimer();
        return rootView;
    }

    private void startTimer() {
        new CountDownTimer(1000, 1000) {
            @Override
            public void onTick(long l) {
            }

            public void onFinish() {
                launchNextActivity();
            }
        }.start();
    }

    /**
     * base on session continuity the next activity will be decided and
     * launches next activity
     */
    private void launchNextActivity() {
        if (isSessionContinue()) {
            goToHomeScreen();
        } else {
            goToLoginScreen();
        }
    }

    /**
     * checks current user ID, null indicate terminated session
     *
     * @return true if session is continued and false of session is terminated
     */
    private boolean isSessionContinue() {
        return false;
    }

    /**
     * directly leads to home screen
     */
    private void goToHomeScreen() {
    //code to start home screen by skipping login
    }

    /**
     * leads to login screen.
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    **private void goToLoginScreen() {
        Intent loginIntent = new Intent(getActivity(), LoginActivity.class);
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(getActivity(), imageView_logo, getString(R.string.transition_logo));
            getActivity().startActivity(loginIntent, options.toBundle());
            getActivity().finishAfterTransition();
        } else {
            startActivity(loginIntent);
            getActivity().finish();
        }
    }**

}

}

有什么办法可以避免闪烁吗?这是美丽的污点。

4

1 回答 1

8

将此添加到您的活动中:

    private boolean shouldFinish = false;

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

并改变这个:

    private void goToLoginScreen() { 

      shouldFinish = true;

      Intent loginIntent = new Intent(getActivity(), LoginActivity.class);
      if (android.os.Build.VERSION.SDK_INT >= 21) {
        ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(getActivity(), imageView_logo,    getString(R.string.transition_logo));
        getActivity().startActivity(loginIntent, options.toBundle()); 
      } else { 
        startActivity(loginIntent); 
      } 
    } 
于 2015-08-11T03:14:35.137 回答