2

我正在遵循https://developer.android.com/training/material/animations.html上的说明并尝试在活动之间实现具有共享元素的动画,但这对我不起作用,我搜索了很多但可以没有找到答案,有没有人可以帮忙看看?太感谢了!

我的步骤:

1、在我的“res”目录下创建一个目录“transition”,然后在里面创建一个文件list_to_details.xml:

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <changeImageTransform/>
</transitionSet>

2、在我的“res”目录下创建一个目录“values-v21”,然后在里面创建一个文件styles.xml(设置上面第1步定义的transition):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="android:Theme.Material">
        <!-- enable window content transitions -->
        <item name="android:windowContentTransitions">true</item>

        <!-- specify enter and exit transitions -->
        <item name="android:windowSharedElementEnterTransition">@transition/list_to_detail</item>
        <item name="android:windowSharedElementExitTransition">@transition/list_to_detail</item>
    </style>
</resources>

3、将应用主题设置为新样式文件中定义的“AppTheme”

<application
        android:name="com.myapp.MyApplication"
        android:allowBackup="false"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

4、在Activity A和B的共享元素中添加transitionName:A:B:

5、在Activity A中添加启动Activity B的代码:

expandedImageView.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(DetailDemoActivity.this, v, "todetail");
    Intent intent = new Intent(DetailDemoActivity.this, AnotherDetailDemoActivity.class);
    intent.putExtra("original_url", originalUrl);
    ActivityCompat.startActivity(DetailDemoActivity.this, intent, options.toBundle());
  }
});

6、尝试app但没有播放动画,我猜可能是我在Activity B中有一个异步任务来加载图片,但是我尝试了推迟EnterTransition后它仍然不起作用,我也尝试了getSharedElementEnterTransition ,但调试器显示它为空,请参阅我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
  postponeEnterTransition();

  Intent intent = this.getIntent();
  originalUrl = intent.getStringExtra("original_url");

  GetBigImageAsyncTask  getBigImageTask = new GetBigImageAsyncTask();
  getBigImageTask.execute();

  super.onCreate(savedInstanceState);
  setContentView(R.layout.another_detail_demo);
  // I have also tried setTransitionName() here but it doesn't work too
}

private void scheduleStartPostponedTransition(final View sharedElement) {
  sharedElement.getViewTreeObserver().addOnPreDrawListener(
      new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
          sharedElement.getViewTreeObserver().removeOnPreDrawListener(this);
          startPostponedEnterTransition();
          return true;
        }
      });
}

private class GetBigImageAsyncTask extends AsyncTask<Object, Void, Bitmap> {
  @Override
  protected Bitmap doInBackground(Object... urls) {
    InputStream input = null;
    HttpURLConnection connection = null;
    try {
      URL url = new URL(originalUrl);
      connection = (HttpURLConnection) url.openConnection();
      connection.setDoInput(true);
      connection.connect();
      input = connection.getInputStream();
      Bitmap myBitmap = BitmapFactory.decodeStream(input);
      return myBitmap;
    } catch (Exception e) {
      return null;
    } finally {
      if (connection != null) {
        connection.disconnect();
      }

      if (input != null) {
        try {
          input.close();
        } catch (Exception e) {

        }
      }
    }
  }

  @Override
  protected void onPostExecute(Bitmap result) {
    ImageView expandedImageView =
        (ImageView) findViewById(R.id.another_demo_expanded_image);
    expandedImageView.setImageDrawable(new BitmapDrawable(result));
    scheduleStartPostponedTransition(expandedImageView);

    final Transition transition = AnotherDetailDemoActivity.this.getWindow().getSharedElementEnterTransition();

    // In fact here the object transition is null 
    if (transition != null) {
      // There is an entering shared element transition so add a listener to it
      transition.addListener(new Transition.TransitionListener() {
        @Override
        public void onTransitionEnd(Transition transition) {
          transition.removeListener(this);
        }

        @Override
        public void onTransitionStart(Transition transition) {
        }

        @Override
        public void onTransitionCancel(Transition transition) {
          // Make sure we remove ourselves as a listener
          transition.removeListener(this);
        }

        @Override
        public void onTransitionPause(Transition transition) {
          // No-op
        }

        @Override
        public void onTransitionResume(Transition transition) {
          // No-op
        }
      });
    }
  }
}

我的步骤有什么错误或遗漏吗?非常感谢任何帮助!

4

1 回答 1

4

刚刚让它工作。

影响的两个问题:

  1. 我需要使用 java 代码更改而不是 XML 更改(不知道为什么,我想我的 Android SDK 解析 XML 更改可能有问题)
  2. 我需要添加 window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS) 才能使其工作,Android 开发者网站中没有提到此功能,但有人在一些权益溢出问题中提到了它。
于 2015-04-15T08:38:41.730 回答