Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public async void zoomImageFromThumb(View thumbView, int position)
- {
- // If there's an animation in progress, cancel it
- // immediately and proceed with this one.
- if (mCurrentAnimator != null)
- {
- mCurrentAnimator.Cancel();
- }
- ImageView expandedImageView = owner.FindViewById<ImageView>(Resource.Id.expanded_image);
- // Load the high-resolution "zoomed-in" image.
- try
- {
- string pic = photos[position].Picture;
- if (pic != null && pic.Length > 0)
- expandedImageView.SetImageBitmap(await eOMIS.Droid.Imaging.Utils.Base64ToBitMap(pic));
- }
- catch (System.Exception)
- {
- Console.WriteLine("Error loading image in offender list adapter");
- }
- // calculate the starting and ending bounds for the zoomed-in image.
- //this step involves lots of math. Yay, math.
- Rect startBounds = new Rect();
- Rect finalBounds = new Rect();
- Point globalOffset = new Point();
- // The start bounds are the global visible rectangle of the thumbnail,
- // and the final bounds are the global visible rectangle of the container
- // view. Also set the container view's offset as the origin for the
- // bounds, since that's the origin for the positioning animation
- // properties (X, Y).
- thumbView.GetGlobalVisibleRect(startBounds);
- owner.FindViewById<FrameLayout>(Resource.Id.container).GetGlobalVisibleRect(finalBounds, globalOffset);
- startBounds.Offset(-globalOffset.X, -globalOffset.Y);
- finalBounds.Offset(-globalOffset.X, -globalOffset.Y);
- // Adjust the start bounds to be the same aspect ratio as the final
- // bounds using the "center crop" technique. This prevents undesirable
- // stretching during the animation. Also calculate the start scaling
- // factor (the end scaling factor is always 1.0).
- float startScale;
- if ((float)finalBounds.Width() / finalBounds.Height()
- > (float)startBounds.Width() / startBounds.Height())
- {
- // Extend start bounds horizontally
- startScale = (float)startBounds.Height() / finalBounds.Height();
- float startWidth = startScale * finalBounds.Width();
- float deltaWidth = (startWidth - startBounds.Width()) / 2;
- startBounds.Left -= (int)deltaWidth;
- startBounds.Right += (int)deltaWidth;
- }
- else {
- // Extend start bounds vertically
- startScale = (float)startBounds.Width() / finalBounds.Width();
- float startHeight = startScale * finalBounds.Height();
- float deltaHeight = (startHeight - startBounds.Height()) / 2;
- startBounds.Top -= (int)deltaHeight;
- startBounds.Bottom += (int)deltaHeight;
- }
- // Hide the thumbnail and show the zoomed-in view. When the animation
- // begins, it will position the zoomed-in view in the place of the
- // thumbnail.
- //thumbView.Alpha = 0f;
- expandedImageView.Visibility = ViewStates.Visible;
- // Set the pivot point for SCALE_X and SCALE_Y transformations
- // to the top-left corner of the zoomed-in view (the default
- // is the center of the view).
- expandedImageView.PivotX = 0f;
- expandedImageView.PivotY = 0f;
- // Construct and run the parallel animation of the four translation and
- // scale properties (X, Y, SCALE_X, and SCALE_Y).
- AnimatorSet set = new AnimatorSet();
- set
- .Play(ObjectAnimator.OfFloat(expandedImageView, View.X,
- startBounds.Left, finalBounds.Left))
- .With(ObjectAnimator.OfFloat(expandedImageView, View.Y,
- startBounds.Top, finalBounds.Top))
- .With(ObjectAnimator.OfFloat(expandedImageView, View.ScaleXs,
- startScale, 1f)).With(ObjectAnimator.OfFloat(expandedImageView,
- View.ScaleYs, startScale, 1f));
- set.SetDuration(mShortAnimationDuration);
- set.SetInterpolator(new DecelerateInterpolator());
- set.AnimationEnd += (object sender, EventArgs e) =>
- {
- mCurrentAnimator = null;
- };
- set.AnimationCancel += (object sender, EventArgs e) =>
- {
- mCurrentAnimator = null;
- };
- set.Start();
- mCurrentAnimator = set;
- // Upon clicking the zoomed-in image, it should zoom back down
- // to the original bounds and show the thumbnail instead of
- // the expanded image.
- float startScaleFinal = startScale;
- expandedImageView.Click += (object sender, EventArgs e) =>
- {
- if (mCurrentAnimator != null)
- {
- mCurrentAnimator.Cancel();
- }
- // Animate the four positioning/sizing properties in parallel,
- // back to their original values.
- AnimatorSet set2 = new AnimatorSet();
- set2.Play(ObjectAnimator
- .OfFloat(expandedImageView, View.X, startBounds.Left))
- .With(ObjectAnimator
- .OfFloat(expandedImageView,
- View.Y, startBounds.Top))
- .With(ObjectAnimator
- .OfFloat(expandedImageView,
- View.ScaleXs, startScaleFinal))
- .With(ObjectAnimator
- .OfFloat(expandedImageView,
- View.ScaleYs, startScaleFinal));
- set2.SetDuration(mShortAnimationDuration);
- set2.SetInterpolator(new DecelerateInterpolator());
- set2.AnimationEnd += (object sender2, EventArgs e2) =>
- {
- thumbView.Alpha = 1f;
- expandedImageView.Visibility = ViewStates.Gone;
- mCurrentAnimator = null;
- };
- set2.AnimationCancel += (object sesnder3, EventArgs e3) =>
- {
- thumbView.Alpha = 1f;
- expandedImageView.Visibility = ViewStates.Gone;
- mCurrentAnimator = null;
- };
- set2.Start();
- mCurrentAnimator = set2;
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement