Advertisement
Guest User

Xamarin tap to zoom

a guest
Jul 25th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.40 KB | None | 0 0
  1. public async void zoomImageFromThumb(View thumbView, int position)
  2.         {
  3.             // If there's an animation in progress, cancel it
  4.             // immediately and proceed with this one.
  5.             if (mCurrentAnimator != null)
  6.             {
  7.                 mCurrentAnimator.Cancel();
  8.             }
  9.             ImageView expandedImageView = owner.FindViewById<ImageView>(Resource.Id.expanded_image);
  10.             // Load the high-resolution "zoomed-in" image.
  11.             try
  12.             {
  13.                 string pic = photos[position].Picture;
  14.                 if (pic != null && pic.Length > 0)
  15.                     expandedImageView.SetImageBitmap(await eOMIS.Droid.Imaging.Utils.Base64ToBitMap(pic));
  16.             }
  17.             catch (System.Exception)
  18.             {
  19.                 Console.WriteLine("Error loading image in offender list adapter");
  20.             }
  21.  
  22.             // calculate the starting and ending bounds for the zoomed-in image.
  23.             //this step involves lots of math. Yay, math.
  24.             Rect startBounds = new Rect();
  25.             Rect finalBounds = new Rect();
  26.             Point globalOffset = new Point();
  27.  
  28.             // The start bounds are the global visible rectangle of the thumbnail,
  29.             // and the final bounds are the global visible rectangle of the container
  30.             // view. Also set the container view's offset as the origin for the
  31.             // bounds, since that's the origin for the positioning animation
  32.             // properties (X, Y).
  33.             thumbView.GetGlobalVisibleRect(startBounds);
  34.  
  35.             owner.FindViewById<FrameLayout>(Resource.Id.container).GetGlobalVisibleRect(finalBounds, globalOffset);
  36.             startBounds.Offset(-globalOffset.X, -globalOffset.Y);
  37.             finalBounds.Offset(-globalOffset.X, -globalOffset.Y);
  38.  
  39.             // Adjust the start bounds to be the same aspect ratio as the final
  40.             // bounds using the "center crop" technique. This prevents undesirable
  41.             // stretching during the animation. Also calculate the start scaling
  42.             // factor (the end scaling factor is always 1.0).
  43.             float startScale;
  44.             if ((float)finalBounds.Width() / finalBounds.Height()
  45.                 > (float)startBounds.Width() / startBounds.Height())
  46.             {
  47.                 // Extend start bounds horizontally
  48.                 startScale = (float)startBounds.Height() / finalBounds.Height();
  49.                 float startWidth = startScale * finalBounds.Width();
  50.                 float deltaWidth = (startWidth - startBounds.Width()) / 2;
  51.                 startBounds.Left -= (int)deltaWidth;
  52.                 startBounds.Right += (int)deltaWidth;
  53.             }
  54.             else {
  55.                 // Extend start bounds vertically
  56.                 startScale = (float)startBounds.Width() / finalBounds.Width();
  57.                 float startHeight = startScale * finalBounds.Height();
  58.                 float deltaHeight = (startHeight - startBounds.Height()) / 2;
  59.                 startBounds.Top -= (int)deltaHeight;
  60.                 startBounds.Bottom += (int)deltaHeight;
  61.             }
  62.  
  63.             // Hide the thumbnail and show the zoomed-in view. When the animation
  64.             // begins, it will position the zoomed-in view in the place of the
  65.             // thumbnail.
  66.             //thumbView.Alpha = 0f;
  67.             expandedImageView.Visibility = ViewStates.Visible;
  68.  
  69.             // Set the pivot point for SCALE_X and SCALE_Y transformations
  70.             // to the top-left corner of the zoomed-in view (the default
  71.             // is the center of the view).
  72.             expandedImageView.PivotX = 0f;
  73.             expandedImageView.PivotY = 0f;
  74.  
  75.             // Construct and run the parallel animation of the four translation and
  76.             // scale properties (X, Y, SCALE_X, and SCALE_Y).
  77.             AnimatorSet set = new AnimatorSet();
  78.             set
  79.                 .Play(ObjectAnimator.OfFloat(expandedImageView, View.X,
  80.                             startBounds.Left, finalBounds.Left))
  81.                 .With(ObjectAnimator.OfFloat(expandedImageView, View.Y,
  82.                                              startBounds.Top, finalBounds.Top))
  83.                 .With(ObjectAnimator.OfFloat(expandedImageView, View.ScaleXs,
  84.                                              startScale, 1f)).With(ObjectAnimator.OfFloat(expandedImageView,
  85.                                                                                           View.ScaleYs, startScale, 1f));
  86.             set.SetDuration(mShortAnimationDuration);
  87.             set.SetInterpolator(new DecelerateInterpolator());
  88.             set.AnimationEnd += (object sender, EventArgs e) =>
  89.             {
  90.                 mCurrentAnimator = null;
  91.             };
  92.             set.AnimationCancel += (object sender, EventArgs e) =>
  93.             {
  94.                 mCurrentAnimator = null;
  95.             };
  96.             set.Start();
  97.             mCurrentAnimator = set;
  98.  
  99.             // Upon clicking the zoomed-in image, it should zoom back down
  100.             // to the original bounds and show the thumbnail instead of
  101.             // the expanded image.
  102.             float startScaleFinal = startScale;
  103.             expandedImageView.Click += (object sender, EventArgs e) =>
  104.             {
  105.                 if (mCurrentAnimator != null)
  106.                 {
  107.                     mCurrentAnimator.Cancel();
  108.                 }
  109.  
  110.                 // Animate the four positioning/sizing properties in parallel,
  111.                 // back to their original values.
  112.                 AnimatorSet set2 = new AnimatorSet();
  113.                 set2.Play(ObjectAnimator
  114.                           .OfFloat(expandedImageView, View.X, startBounds.Left))
  115.                             .With(ObjectAnimator
  116.                                     .OfFloat(expandedImageView,
  117.                                             View.Y, startBounds.Top))
  118.                     .With(ObjectAnimator
  119.                           .OfFloat(expandedImageView,
  120.                                    View.ScaleXs, startScaleFinal))
  121.                             .With(ObjectAnimator
  122.                                   .OfFloat(expandedImageView,
  123.                                            View.ScaleYs, startScaleFinal));
  124.                 set2.SetDuration(mShortAnimationDuration);
  125.                 set2.SetInterpolator(new DecelerateInterpolator());
  126.                 set2.AnimationEnd += (object sender2, EventArgs e2) =>
  127.                 {
  128.                     thumbView.Alpha = 1f;
  129.                     expandedImageView.Visibility = ViewStates.Gone;
  130.                     mCurrentAnimator = null;
  131.                 };
  132.                 set2.AnimationCancel += (object sesnder3, EventArgs e3) =>
  133.                 {
  134.                     thumbView.Alpha = 1f;
  135.                     expandedImageView.Visibility = ViewStates.Gone;
  136.                     mCurrentAnimator = null;
  137.                 };
  138.                 set2.Start();
  139.                 mCurrentAnimator = set2;
  140.             };
  141.  
  142.  
  143.  
  144.  
  145.  
  146.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement