Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. private void zoomImageFromThumb(final View thumbView, Drawable imageResBitmap) {
  2. // If there's an animation in progress, cancel it
  3. // immediately and proceed with this one.
  4. if (currentAnimator != null) {
  5. currentAnimator.cancel();
  6. }
  7.  
  8. // Load the high-resolution "zoomed-in" image.
  9. final ImageView expandedImageView = (ImageView) findViewById(R.id.expanded_image);
  10. expandedImageView.setImageDrawable(imageResBitmap);
  11.  
  12. // Calculate the starting and ending bounds for the zoomed-in image.
  13. // This step involves lots of math. Yay, math.
  14. final Rect startBounds = new Rect();
  15. final Rect finalBounds = new Rect();
  16. final Point globalOffset = new Point();
  17.  
  18. // The start bounds are the global visible rectangle of the thumbnail,
  19. // and the final bounds are the global visible rectangle of the container
  20. // view. Also set the container view's offset as the origin for the
  21. // bounds, since that's the origin for the positioning animation
  22. // properties (X, Y).
  23. thumbView.getGlobalVisibleRect(startBounds);
  24. findViewById(R.id.container).getGlobalVisibleRect(finalBounds, globalOffset);
  25. startBounds.offset(-globalOffset.x, -globalOffset.y);
  26. finalBounds.offset(-globalOffset.x, -globalOffset.y);
  27. //Log.i("-globalOffset.x, -globalOffset.y", -globalOffset.x + "" + -globalOffset.y);
  28.  
  29. // Adjust the start bounds to be the same aspect ratio as the final
  30. // bounds using the "center crop" technique. This prevents undesirable
  31. // stretching during the animation. Also calculate the start scaling
  32. // factor (the end scaling factor is always 1.0).
  33. float startScale;
  34. //Log.i("(float) finalBounds.width() / finalBounds.height()", (float) finalBounds.width() / finalBounds.height() + "");
  35. //Log.i("(float) startBounds.width() / startBounds.height()", (float) startBounds.width() / startBounds.height() + "");
  36. if ((float) finalBounds.width() / finalBounds.height()
  37. > (float) startBounds.width() / startBounds.height()) {
  38. // Extend start bounds horizontally
  39. startScale = (float) startBounds.height() / finalBounds.height();
  40. //Log.i("startScale", startScale + "");
  41. float startWidth = startScale * finalBounds.width();
  42. //Log.i("startWidth", startWidth + "");
  43. float deltaWidth = (startWidth - startBounds.width()) / 2;
  44. //Log.i("deltaWidth", deltaWidth + "");
  45. startBounds.left -= deltaWidth;
  46. startBounds.right += deltaWidth;
  47. } else {
  48. // Extend start bounds vertically
  49. startScale = (float) startBounds.width() / finalBounds.width();
  50. //Log.i("startScale", startScale + "");
  51. float startHeight = startScale * finalBounds.height();
  52. //Log.i("startHeight", startHeight + "");
  53. float deltaHeight = (startHeight - startBounds.height()) / 2;
  54. //Log.i("deltaHeight", deltaHeight + "");
  55. startBounds.top -= deltaHeight;
  56. startBounds.bottom += deltaHeight;
  57. }
  58.  
  59. // Hide the thumbnail and show the zoomed-in view. When the animation
  60. // begins, it will position the zoomed-in view in the place of the
  61. // thumbnail.
  62. thumbView.setAlpha(0f);
  63. expandedImageView.setVisibility(View.VISIBLE);
  64.  
  65. // Set the pivot point for SCALE_X and SCALE_Y transformations
  66. // to the top-left corner of the zoomed-in view (the default
  67. // is the center of the view).
  68. expandedImageView.setPivotX(0f);
  69. expandedImageView.setPivotY(0f);
  70.  
  71. // Construct and run the parallel animation of the four translation and
  72. // scale properties (X, Y, SCALE_X, and SCALE_Y).
  73. AnimatorSet set = new AnimatorSet();
  74. set
  75. .play(ObjectAnimator.ofFloat(expandedImageView, View.X,
  76. startBounds.left, finalBounds.left))
  77. .with(ObjectAnimator.ofFloat(expandedImageView, View.Y,
  78. startBounds.top, finalBounds.top))
  79. .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X,
  80. startScale, 1f)).with(ObjectAnimator.ofFloat(expandedImageView,
  81. View.SCALE_Y, startScale, 1f));
  82. set.setDuration(mShortAnimationDuration);
  83. set.setInterpolator(new DecelerateInterpolator());
  84. set.addListener(new AnimatorListenerAdapter() {
  85. @Override
  86. public void onAnimationEnd(Animator animation) {
  87. currentAnimator = null;
  88. }
  89.  
  90. @Override
  91. public void onAnimationCancel(Animator animation) {
  92. currentAnimator = null;
  93. }
  94. });
  95. set.start();
  96. currentAnimator = set;
  97.  
  98. // Upon clicking the zoomed-in image, it should zoom back down
  99. // to the original bounds and show the thumbnail instead of
  100. // the expanded image.
  101. final float startScaleFinal = startScale;
  102. expandedImageView.setOnClickListener(new View.OnClickListener() {
  103. @Override
  104. public void onClick(View view) {
  105. if (currentAnimator != null) {
  106. currentAnimator.cancel();
  107. }
  108.  
  109. // Animate the four positioning/sizing properties in parallel,
  110. // back to their original values.
  111. AnimatorSet set = new AnimatorSet();
  112. //Log.i("startBounds.left, startBounds.top", startBounds.left + ", " + startBounds.top);
  113. set.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.SCALE_X, startScaleFinal))
  121. .with(ObjectAnimator
  122. .ofFloat(expandedImageView,
  123. View.SCALE_Y, startScaleFinal));
  124. set.setDuration(mShortAnimationDuration);
  125. set.setInterpolator(new DecelerateInterpolator());
  126. set.addListener(new AnimatorListenerAdapter() {
  127. @Override
  128. public void onAnimationEnd(Animator animation) {
  129. thumbView.setAlpha(1f);
  130. expandedImageView.setVisibility(View.GONE);
  131. currentAnimator = null;
  132. }
  133.  
  134. @Override
  135. public void onAnimationCancel(Animator animation) {
  136. thumbView.setAlpha(1f);
  137. expandedImageView.setVisibility(View.GONE);
  138. currentAnimator = null;
  139. }
  140. });
  141. set.start();
  142. currentAnimator = set;
  143. }
  144. });
  145. }
  146.  
  147. <com.polites.android.GestureImageView
  148. android:id="@+id/expanded_image"
  149. android:layout_width="match_parent"
  150. android:layout_height="match_parent"
  151. android:visibility="gone"
  152. gesture-image:max-scale="10.0"
  153. gesture-image:min-scale="0.1"
  154. gesture-image:strict="false" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement