Android:从viewpager到新活动的共享元素转换

时间:2018-06-11 09:44:14

标签: android android-viewpager android-transitions shared-element-transition

请帮助!!!

我试图在用户点击图像时为图像设置动画,viewpager中Activity1上的小图像应该使用缩放动画移动到Activity2。但我正在从屏幕右侧获得幻灯片动画。

这是我的代码段!

V21 / styles.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowContentTransitions">true</item>
  </style>

viewpager_item.xml

 <ImageView>
...
android:id="@+id/pagerItem"
android:transitionName="@string/transition_name"
</ImageView>

在Activity1.class中:

    @Override
    public void onProjectItemClick(int position, View imageToShare) {
    Intent intent = new Intent(context, Activity2.class);
    intent.putExtra("uri", imgUri);
    ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), imageToShare,ViewCompat.getTransitionName(imageToShare));
startActivity(intent, options.toBundle());
      }

activity2.xml

    <ImageView>
        ...
android:id="@+id/imageView"
android:transitionName="@string/transition_name"
</ImageView>

在Activity2.class中:

onCreate() {
supportPostponeEnterTransition();
showImage();
}

private void showImage() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ViewCompat.setTransitionName(imageView,getResources().getString(R.string.transition_name));
      GlideApp.with(this)
          .load(getIntent().getExtras().get("uri"))
          .dontAnimate()
          .diskCacheStrategy(DiskCacheStrategy.ALL)
          .listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(
                @Nullable
                    GlideException e, Object model, Target<Drawable> target,
                boolean isFirstResource) {
              supportStartPostponedEnterTransition();
              return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target,
                DataSource dataSource, boolean isFirstResource) {
              supportStartPostponedEnterTransition();
              return false;
            }
          })
          .into(imageView);
}
}

0 个答案:

没有答案