package com.naman14.playanimations; import android.app.SharedElementCallback; import android.content.Context; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Parcelable; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.ImageView; import java.util.List; /** * Created by naman on 29/05/15. */ public class DetailActivity extends ActionBarActivity { Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayShowTitleEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(true); setEnterSharedElementCallback(new SharedElementCallback() { @Override public View onCreateSnapshotView(Context context, Parcelable snapshot) { View view = new View(context); view.setBackground(new BitmapDrawable((Bitmap) snapshot)); return view; } @Override public void onSharedElementStart(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) { ImageView sharedElement = (ImageView) findViewById(R.id.cover); for (int i = 0; i < sharedElements.size(); i++) { if (sharedElements.get(i) == sharedElement) { View snapshot = sharedElementSnapshots.get(i); Drawable snapshotDrawable = snapshot.getBackground(); sharedElement.setBackground(snapshotDrawable); sharedElement.setImageAlpha(0); forceSharedElementLayout(); break; } } } private void forceSharedElementLayout() { ImageView sharedElement = (ImageView) findViewById(R.id.cover); int widthSpec = View.MeasureSpec.makeMeasureSpec(sharedElement.getWidth(), View.MeasureSpec.EXACTLY); int heightSpec = View.MeasureSpec.makeMeasureSpec(sharedElement.getHeight(), View.MeasureSpec.EXACTLY); int left = sharedElement.getLeft(); int top = sharedElement.getTop(); int right = sharedElement.getRight(); int bottom = sharedElement.getBottom(); sharedElement.measure(widthSpec, heightSpec); sharedElement.layout(left, top, right, bottom); } @Override public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) { ImageView sharedElement = (ImageView) findViewById(R.id.cover); sharedElement.setBackground(null); sharedElement.setImageAlpha(255); } }); } }