使用Material Design在Android Lollipop中的ListView中的活动过渡动画

问题描述 投票:2回答:1

我正在使用Master / Detail模式,目前正在转向Android Lollipop。如果我点击ListView中的某个项目,我想拥有一个new activity transistions。动画正在运行,但我不知道如何在共享元素(在我的情况下是ImageView)之间制作某个动画。

如果我单击自定义ListView中的一行(带有图像和文本),则转换应切换到DetailActivtiy中的图像。它应该在这个视频中看起来像:http://youtu.be/RhiPJByIMrM?t=2m41s或者这个视频:http://youtu.be/XkWI1FKKrs4

我已将此代码添加到我的两个ImageView中:

<ImageView
            android:transitionName="@string/transition_title_image"/>

我的ListActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= 21) {
        //To enable window content transitions in your code instead, call the Window.requestFeature() method:
        getWindow().requestFeature(android.view.Window.FEATURE_CONTENT_TRANSITIONS);
        Transition ts_enter = new Slide();  //Slide(); //Explode();
        Transition ts_exit = new Explode();

        ts_enter.setDuration(2000);
        ts_exit.setDuration(2000);
        /*
        If you have set an enter transition for the second activity,
        the transition is also activated when the activity starts.
        */
        getWindow().setEnterTransition(ts_enter);
        getWindow().setExitTransition(ts_exit);
    }
    super.onCreate(savedInstanceState);

使用此方法调用我的DetailActivity:

    if (Build.VERSION.SDK_INT >= 21) {
        Intent intent = new Intent(ArticleListActivity.this, ArticleDetailActivity.class);
        intent.putExtra("pos", id);
        intent.putExtra("articleList", articleList);
        String transitionName = getString(R.string.transition_title_image);
        ImageView article_thumb = (ImageView) findViewById(R.id.article_thumb);

        ActivityOptionsCompat options =
                ActivityOptionsCompat.makeSceneTransitionAnimation(ArticleListActivity.this,
                        article_thumb,   // The view which starts the transition
                        transitionName    // The transitionName of the view we’re transitioning to
                );
        ActivityCompat.startActivity(ArticleListActivity.this, intent, options.toBundle());
    }

我的DetailActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= 21) {
        //To enable window content transitions in your code instead, call the Window.requestFeature() method:
        getWindow().requestFeature(android.view.Window.FEATURE_CONTENT_TRANSITIONS);
        Transition ts_enter = new Slide();  //Slide(); //Explode();
        Transition ts_exit = new Explode();  //Slide(); //Explode();

        ts_enter.setDuration(2000);
        ts_exit.setDuration(2000);

        getWindow().setEnterTransition(ts_enter);
        getWindow().setExitTransition(ts_exit);
    }
    super.onCreate(savedInstanceState)

;

android android-listview android-5.0-lollipop material-design activity-transition
1个回答
0
投票

试试这个:

  1. 首先,确保在第一个活动中为每个ImageView提供一个唯一的转换名称。如果所有图像视图具有相同的转换名称,则框架将不知道动画开始时选择哪一个,并且转换将无法正常运行。
  2. 单击ImageView时,将其唯一的转换名称作为Intent extra传递给详细信息活动。
  3. 在详细信息活动的onCreate()方法中,从意图包中检索名称,并将其设置为ImageView的转换名称。
© www.soinside.com 2019 - 2024. All rights reserved.