Reputation: 813
I want to zoom an Image into a new Activity when I click on it. To undestand what Im trying to achive I uploaded some pictures in a row. If it is hard to understand it through a picture I could try to upload a video. The only thing I found was this . But its only for a View. I want to start a complete new Activity or Dialog whatever.
Any suggestions how I could do it on the best way?
Upvotes: 0
Views: 1633
Reputation: 2626
What that you want is called 'Shared Element Activity Transition'.
A shared elements transition determines how views that are shared between two activities transition between these activities. For example, if two activities have the same image in different positions and sizes, the changeImageTransform shared element transition translates and scales the image smoothly between these activities.
It's require Android 5.0 (API level 21) and above and will be ignored for any lower API versions. Great example to understand how to implement it on your sample see here.
Solution before API 21 you need to use third-party libraries. An example is this lib that @Markaos mentioned.
Upvotes: 1
Reputation: 659
I believe this library does exactly what you want: https://github.com/albinmathew/PreLollipopTransition (yes, what you want is part of Android API, but only from Lollipop up - you probably want to support lower APIs too, and that is what this library is for).
EDIT: original https://github.com/takahirom/PreLollipopTransition seems to be more up-to-date, so you probably want to use this instead.
With it, you can basically do just this in one Activity:
findViewById(R.id.imageView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent = new Intent(MainActivity.this, SubActivity.class);
ActivityTransitionLauncher.with(MainActivity.this).from(v).launch(intent);
}
});
... and this in the second activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
ActivityTransition.with(getIntent()).to(findViewById(R.id.sub_imageView)).start(savedInstanceState);
}
According to http://guides.codepath.com/android/shared-element-activity-transition, you can simply add <item name="android:windowContentTransitions">true</item>
to your Activity style and, define elements with android:transitionName="<some name>"
in both Activities (transitionName must be the same in both of them), then start Activity like this:
Intent intent = new Intent(this, DetailsActivity.class);
// Pass data object in the bundle and populate details activity.
intent.putExtra(DetailsActivity.EXTRA_CONTACT, contact);
ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(this, (View)ivProfile, "<transition name>");
startActivity(intent, options.toBundle());
(taken directly from the link). But note that this will work only on L+ devices, while the solution above will run on anything with API 14+. I personally think there's no point in using native API if a good wrapper providing backward compatibility is available, but you asked for it, so here it is.
Hope this helps you, comment if you have any questions
Upvotes: 4