0

I am working on android shared element transition. I tried the code below only second activity opens with a blink. I have passed same transition name in both imageview but still not working.

 imageview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            View imageView = findViewById(R.id.imageView);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                Intent intent = new Intent(StartActivity.this, EndActivity.class);
                Pair<View, String> pair1 = Pair.create(imageView, imageView.getTransitionName());

                ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(StartActivity.this, pair1);
                startActivity(intent, options.toBundle());
            } else {
                Intent intent = new Intent(StartActivity.this, EndActivity.class);
                startActivity(intent);
            }


        }
    });

1 Answers1

0

You need to compress and pass the image in a bundle from one activity to the other. Make sure both the imageviews (in activity A and activity B) have same android:transitionName.

Do this in Activity A inside onClick method of imageview:

Intent intent = new Intent(MainActivity.this, DetailActivity.class);
            ActivityOptionsCompat options = ActivityOptionsCompat.
                    makeSceneTransitionAnimation(MainActivity.this,
                            imageView,
                            "trans");
            imageView.buildDrawingCache();
            Bitmap image = Bitmap.createScaledBitmap(imageView.getDrawingCache(), 300, 300, true);
            Bundle extras = new Bundle();
            extras.putParcelable("imagebitmap", image);
            intent.putExtras(extras);
            startActivity(intent, options.toBundle());

Give a transition name to the imageview. Here I have given android:transitionName = "trans".

<ImageView
            android:id="@+id/country_photo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:contentDescription="@string/action_settings"
            android:transitionName="trans"
            tools:ignore="UnusedAttribute"
             />

Then in the Activity B's onCreate() method:

        imageView = findViewById(R.id.iv_detail_image);
        Bundle extras = getIntent().getExtras();
        Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
        imageView.setImageBitmap(bmp);

Make sure the android:transitionName attribute of the imageview in Activity B is same. That means android:transistionName should be "trans" for the imageview in Activity B.

 <ImageView
        android:id="@+id/iv_detail_image"
        android:transitionName="trans"
        android:scaleType="fitCenter"
        android:layout_width="match_parent"
        tools:ignore="UnusedAttribute"
        android:layout_height="380dp"
       />
VIVEK CHOUDHARY
  • 468
  • 5
  • 8