2

我正在使用 Master/Detail 模式,目前正在转向 Android Lollipop。如果我单击 ListView 中的一个项目,我希望有一个新的活动转换。动画正在运行,但我不知道如何在共享元素(在我的情况下为 ImageView)之间制作特定动画。

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

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

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

我的列表活动:

@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());
    }

我的详细活动:

@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)

;

4

1 回答 1

0

尝试这个:

  1. 首先,确保ImageView在第一个活动中为每个活动指定一个唯一的过渡名称。如果所有图像视图都具有相同的过渡名称,则框架将不知道在动画开始时选择哪一个,并且过渡将无法正常运行。

  2. 单击时ImageView,将其唯一的转换名称作为Intent额外的传递给详细信息活动。

  3. 在 details 活动的onCreate()方法中,从意图包中检索名称,并将其设置为ImageView的转换名称。

于 2014-12-15T22:04:17.320 回答