天天看點

Android 下拉圖檔放大縮小

Android 下拉圖檔放大縮小

MainActivity

package com.example.pice;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}
      

HeadZoomScrollView

package com.example.pice;

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;

/**
 * 姓名:${user}
 * 時間:${date}
 */

public class HeadZoomScrollView extends ScrollView {
    private View mZoomView;
    private int mZoomViewWidth;
    private int mZoomViewHeight;


    private float firstPosition;//記錄第一次按下的位置
    private boolean isScrolling;//是否正在縮放
    private float mScrollRate = 1.4f;//縮放系數,縮放系數越大,變化的越大
    private float mReplyRate = 0.5f;//回調系數,越大,回調越慢

    public HeadZoomScrollView(Context context) {
        super(context);
    }

    public HeadZoomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HeadZoomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setmZoomView(View mZoomView) {
        this.mZoomView = mZoomView;
    }

    public void setmScrollRate(float mScrollRate) {
        this.mScrollRate = mScrollRate;
    }

    public void setmReplyRate(float mReplyRate) {
        this.mReplyRate = mReplyRate;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    private void init() {
        setOverScrollMode(OVER_SCROLL_NEVER);
        if (getChildAt(0) != null) {
            ViewGroup vg = (ViewGroup) getChildAt(0);
            if (vg.getChildAt(0) != null) {
                mZoomView = vg.getChildAt(0);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mZoomViewWidth <= 0 || mZoomViewHeight <= 0) {
            mZoomViewWidth = mZoomView.getMeasuredWidth();
            mZoomViewHeight = mZoomView.getMeasuredHeight();
        }
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                //手指離開後恢複圖檔
                isScrolling = false;
                replyImage();
                break;
            case MotionEvent.ACTION_MOVE:
                if (!isScrolling) {
                    if (getScrollY() == 0) {
                        firstPosition = ev.getY();// 滾動到頂部時記錄位置,否則正常傳回
                    } else {
                        break;
                    }
                }
                int distance = (int) ((ev.getY() - firstPosition) * mScrollRate); // 滾動距離乘以一個系數
                if (distance < 0) { // 目前位置比記錄位置要小,正常傳回
                    break;
                }

                // 處理放大
                isScrolling = true;
                setZoom(distance);
                return true; // 傳回true表示已經完成觸摸事件,不再處理
        }
        return true;
    }

    //回彈動畫
    private void replyImage() {
        float distance = mZoomView.getMeasuredWidth() - mZoomViewWidth;
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(distance, 0f).setDuration((long) (distance * mReplyRate));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                setZoom((Float) animation.getAnimatedValue());
            }
        });
        valueAnimator.start();
    }

    public void setZoom(float zoom) {
        if (mZoomViewWidth <= 0 || mZoomViewHeight <= 0) {
            return;
        }
        ViewGroup.LayoutParams lp = mZoomView.getLayoutParams();
        lp.width = (int) (mZoomViewWidth + zoom);
        lp.height = (int) (mZoomViewHeight * ((mZoomViewWidth + zoom) / mZoomViewWidth));
        ((MarginLayoutParams) lp).setMargins(-(lp.width - mZoomViewWidth) / 2, 0, 0, 0);
        mZoomView.setLayoutParams(lp);
    }

}
      

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.pice.HeadZoomScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.pice.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_show"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:src="@mipmap/as"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/><TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清明上河圖"/>




    </LinearLayout>


</com.example.pice.HeadZoomScrollView>