English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Android Custom ScrollView Implementation of Top Image Pull-down Enlargement

This article shares the specific code for implementing the scrollView's top image pull-down zoom effect, for your reference, the specific content is as follows

The previous scrollView's top image pull-down zoom effect has been used several times in subsequent projects, but writing it in the Activity each time is麻烦 and not convenient for reuse. These days, I have some free time, so I have re-implemented this effect using a custom scrollView method. The principle is basically the same as before, so I won't go into detail and will directly show the code.

package com.example.myapplication.dropzoom; 
import android.animation.ObjectAnimator; 
import android.animation.ValueAnimator; 
import android.content.Context; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ScrollView; 
/** 
 * Created by Liujinhua on 2016/3/25. 
 * Pull down to zoom in on scrollView 
 */ 
public class DropZoomScrollView extends ScrollView implements View.OnTouchListener { 
  // Record the first press position 
  private float mFirstPosition = 0; 
  // Is the zooming in progress 
  private Boolean mScaling = false; 
  private View dropZoomView; 
  private int dropZoomViewWidth; 
  private int dropZoomViewHeight; 
  public DropZoomScrollView(Context context) { 
    super(context); 
  } 
  public DropZoomScrollView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
  } 
  public DropZoomScrollView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
  } 
  @Override 
  protected void onFinishInflate() { 
    super.onFinishInflate(); 
    init(); 
  } 
  @Override 
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
  } 
  private void init() { 
    setOverScrollMode(OVER_SCROLL_NEVER); 
    if (getChildAt(0) != null) { 
      ViewGroup vg = (ViewGroup) getChildAt(0); 
      if (vg.getChildAt(0) != null) { 
        dropZoomView = vg.getChildAt(0); 
        setOnTouchListener(this); 
      } 
    } 
  } 
  @Override 
  public boolean onTouch(View v, MotionEvent event) { 
    if (dropZoomViewWidth <= 0 || dropZoomViewHeight <= 0) { 
      dropZoomViewWidth = dropZoomView.getMeasuredWidth(); 
      dropZoomViewHeight = dropZoomView.getMeasuredHeight(); 
    } 
    switch (event.getAction()) { 
      case MotionEvent.ACTION_UP: 
        //Restore the image after the finger leaves 
        mScaling = false; 
        replyImage(); 
        break; 
      case MotionEvent.ACTION_MOVE: 
        if (!mScaling) { 
          if (getScrollY() == 0) { 
            mFirstPosition = event.getY();// Record the position when scrolling to the top, otherwise return normally 
          } 
            break; 
          } 
        } 
        int distance = (int) ((event.getY() - mFirstPosition) * 0.6); // The scrolling distance is multiplied by a coefficient 
        if (distance < 0) { // The current position is smaller than the recorded position, return normally 
          break; 
        } 
        // Handle zooming 
        mScaling = true; 
        setZoom(1 + distance); 
        return true; // Return true indicates that the touch event has been completed and will no longer be processed 
    } 
    return false; 
  } 
  // Rebound animation (using property animation) 
  public void replyImage() { 
    final float distance = dropZoomView.getMeasuredWidth() - dropZoomViewWidth; 
    // Set animation 
    ValueAnimator anim = ObjectAnimator.ofFloat(0.0F, 1.0F).setDuration((long) (distance * 0.7)); 
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      @Override 
      public void onAnimationUpdate(ValueAnimator animation) { 
        float cVal = (Float) animation.getAnimatedValue(); 
        setZoom(distance - ((distance) * cVal)); 
      } 
    }); 
    anim.start(); 
  } 
  //Zoom 
  public void setZoom(float s) { 
    if (dropZoomViewHeight <= 0 || dropZoomViewWidth <= 0) { 
      return; 
    } 
    ViewGroup.LayoutParams lp = dropZoomView.getLayoutParams(); 
    lp.width = (int) (dropZoomViewWidth + s); 
    lp.height = (int) (dropZoomViewHeight * ((dropZoomViewWidth + s) / dropZoomViewWidth)); 
    dropZoomView.setLayoutParams(lp); 
  } 
} 

It is also very simple to use

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical"> 
  <com.example.myapplication.dropzoom.DropZoomScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:src="@drawable/bg" /> 
      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:src="@drawable/home_bg" /> 
      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:src="@drawable/home_bg" /> 
      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:src="@drawable/home_bg" /> 
      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:src="@drawable/home_bg" /> 
    </LinearLayout> 
  </com.example.myapplication.dropzoom.DropZoomScrollView> 
</LinearLayout> 

That's all for this article. I hope it will be helpful to everyone's learning and that everyone will support the呐喊 tutorial more.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like