SoFunction
Updated on 2025-03-11

Android realizes clicking thumbnail magnification effect

This article shares the specific code for the magnification effect of Android click thumbnails for your reference. The specific content is as follows

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .;
import ;
import ;
import ;

public class MainActivity extends AppCompatActivity {

  // Hold a reference to this animation so that it can cancel the animation while it is executed  private Animator mCurrentAnimator;

  private int mShortAnimationDuration;

  private View imageView1;
  private View imageView2;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    initView();

    (new () {
      @Override
      public void onClick(View v) {
        zoomImageFromThumb(imageView1,.ic_launcher);
      }
    });
    (new () {
      @Override
      public void onClick(View v) {
        zoomImageFromThumb(imageView2,.ic_launcher);
      }
    });

    // The system default short animation execution time is 200    mShortAnimationDuration = getResources().getInteger(
        .config_shortAnimTime);
  }

  private void initView() {
    imageView1 = (ImageView) findViewById(.imageView1);
    imageView2 = (ImageView) findViewById(.imageView2);
  }
  private void zoomImageFromThumb(final View thumbView, int imageResId) {
    // If there is an animation running, cancel the animation    if (mCurrentAnimator != null) {
      ();
    }

    // Load the ImageView showing the large image    final ImageView expandedImageView = (ImageView) findViewById(
        .expanded_image);
    (imageResId);

    // Calculate the boundary position of the initial small graph and the boundary position of the final large graph.    final Rect startBounds = new Rect();
    final Rect finalBounds = new Rect();
    final Point globalOffset = new Point();

    // The boundary of the small image is the boundary of the small ImageView. Because the boundary of the large image is covered with the full screen, it is the boundary of the entire layout.    // Then get the correct coordinates based on the offset.    (startBounds);
    findViewById(.imageView1).getGlobalVisibleRect(finalBounds, globalOffset);
    (-, -);
    (-, -);

    // Calculate the initial scaling.  The final scaling is 1.  And adjust the zoom direction so that the look is coordinated.    float startScale=0;
    if ((float) () / ()
        > (float) () / ()) {
      // Horizontal zoom      float startWidth = startScale * ();
      float deltaWidth = (startWidth - ()) / 2;
       -= deltaWidth;
       += deltaWidth;
    } else {
      // Vertical zoom      float startHeight = startScale * ();
      float deltaHeight = (startHeight - ()) / 2;
       -= deltaHeight;
       += deltaHeight;
    }

    // Hide small images and display large images    (0f);
    ();

    // Move the zoom center point of the large image to the upper left corner.  By default, zoom from the center    (0f);
    (0f);

    //Animate the zoom image    AnimatorSet set = new AnimatorSet();
    ((expandedImageView, , , ))
        .with((expandedImageView, , , ))
        .with((expandedImageView, View.SCALE_X, startScale, 1f))
        .with((expandedImageView, View.SCALE_Y, startScale, 1f));
    (mShortAnimationDuration);
    (new DecelerateInterpolator());
    (new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
        mCurrentAnimator = null;
      }

      @Override
      public void onAnimationCancel(Animator animation) {
        mCurrentAnimator = null;
      }
    });
    ();
    mCurrentAnimator = set;

    // When clicking on the large image, zoom in in reverse, then hide the large image and display the small image.    final float startScaleFinal = startScale;
    (new () {
      @Override
      public void onClick(View view) {
        if (mCurrentAnimator != null) {
          ();
        }

        AnimatorSet set = new AnimatorSet();
        (ObjectAnimator
            .ofFloat(expandedImageView, , ))
            .with(ObjectAnimator
                .ofFloat(expandedImageView,
                    ,))
            .with(ObjectAnimator
                .ofFloat(expandedImageView,
                    View.SCALE_X, startScaleFinal))
            .with(ObjectAnimator
                .ofFloat(expandedImageView,
                    View.SCALE_Y, startScaleFinal));
        (mShortAnimationDuration);
        (new DecelerateInterpolator());
        (new AnimatorListenerAdapter() {
          @Override
          public void onAnimationEnd(Animator animation) {
            (1f);
            ();
            mCurrentAnimator = null;
          }

          @Override
          public void onAnimationCancel(Animator animation) {
            (1f);
            ();
            mCurrentAnimator = null;
          }
        });
        ();
        mCurrentAnimator = set;
      }
    });
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.