<commit_before>/*
 * Copyright 1999-2101 Alibaba Group.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 	http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.akita.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

/**
 * Created with IntelliJ IDEA.
 * Date: 12-6-1
 * Time: 下午6:24
 *
 * @author zhe.yangz
 */

public class PinchZoomImageView extends ImageView
{
    enum Mode {
        NONE,
        DRAGING,   // 拖动中
        ZOOMING    // 缩放中
    }

    enum ZoomFlag {
        BIGGER,   // 放大ing
        SMALLER   // 缩小ing
    }

    private Mode mode = Mode.NONE; // 当前的事件

    private float beforeLenght;    // 两触点距离
    private float afterLenght;     // 两触点距离
    private float scale = 0.03f;   // 缩放的比例 X Y方向都是这个值 越大缩放的越快

    // 处理拖动 变量
    private int start_x;
    private int start_y;
    private int stop_x ;
    private int stop_y ;

    private TranslateAnimation trans; // 处理超出边界的动画


    public PinchZoomImageView(Context context, AttributeSet attributes) {
        super(context, attributes);    //defaults
    }

    public PinchZoomImageView(Context context) {
        super(context);    //defaults
    }

    /**
     * distance of 2 points
     */
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }

    /**
     * handle touch event
     */
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                mode = Mode.DRAGING;
                stop_x = (int) event.getRawX();
                stop_y = (int) event.getRawY();
                start_x = (int) event.getX();
                start_y = stop_y - this.getTop();

                if(event.getPointerCount()==2)
                    beforeLenght = spacing(event);
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                if (spacing(event) > 10f) {
                    mode = Mode.ZOOMING;
                    beforeLenght = spacing(event);
                }
                break;
            case MotionEvent.ACTION_UP:
                // handle and process if the image out of range
                int deltaXfrom=0, deltaXto=0;
                int deltaYfrom=0, deltaYto=0;
                View parentView = (View) getParent();

                if (getTop() < 0) { // must down
                    int topHide = -getTop();
                    int bottomLeave = (parentView.getHeight()-(getHeight()+getTop()));
                    if (bottomLeave <= 0) {
                        // no op
                    } else if (bottomLeave > topHide) { // 下面空间比上面遮挡部分大
                        deltaYfrom = getTop();<commit_msg>trick to drag when using ViewPager 5<commit_after>/*
 * Copyright 1999-2101 Alibaba Group.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 	http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.akita.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

/**
 * Created with IntelliJ IDEA.
 * Date: 12-6-1
 * Time: 下午6:24
 *
 * @author zhe.yangz
 */

public class PinchZoomImageView extends ImageView
{
    enum Mode {
        NONE,
        DRAGING,   // 拖动中
        ZOOMING    // 缩放中
    }

    enum ZoomFlag {
        BIGGER,   // 放大ing
        SMALLER   // 缩小ing
    }

    private Mode mode = Mode.NONE; // 当前的事件

    private float beforeLenght;    // 两触点距离
    private float afterLenght;     // 两触点距离
    private float scale = 0.03f;   // 缩放的比例 X Y方向都是这个值 越大缩放的越快

    // 处理拖动 变量
    private int start_x;
    private int start_y;
    private int stop_x ;
    private int stop_y ;

    private TranslateAnimation trans; // 处理超出边界的动画


    public PinchZoomImageView(Context context, AttributeSet attributes) {
        super(context, attributes);    //defaults
    }

    public PinchZoomImageView(Context context) {
        super(context);    //defaults
    }

    /**
     * distance of 2 points
     */
    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }

    /**
     * handle touch event
     */
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                mode = Mode.DRAGING;
                stop_x = (int) event.getRawX();
                stop_y = (int) event.getRawY();
                start_x = (int) event.getX();
                start_y = stop_y - this.getTop();

                try{
                    if (getLeft() >= 0 && (stop_x - event.getRawX()) < 0) {
                        getParent().getParent().requestDisallowInterceptTouchEvent(false);
                    } else if (getRight() <= (((View)getParent()).getWidth())
                            && (stop_x - event.getRawX()) > 0 ) {
                        getParent().getParent().requestDisallowInterceptTouchEvent(false);
                    } else {
                        getParent().getParent().requestDisallowInterceptTouchEvent(true);
                    }
                } catch (Exception e) {e.printStackTrace();};

                if(event.getPointerCount()==2)
                    beforeLenght = spacing(event);
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                if (spacing(event) > 10f) {
                    mode = Mode.ZOOMING;
                    beforeLenght = spacing(event);
                }
                break;
            case MotionEvent.ACTION_UP:
                // handle and process if the image out of range
                int deltaXfrom=0, deltaXto=0;
                int deltaYfrom=0, deltaYto=0;
                View parentView = (View) getParent();

                if (getTop() < 0) { // must down
                    int topHide = -getTop();
                    int bottomLeave = (parentView.getHeight()-(getHeight()+getTop()));
                    if (bottomLeave <= 0) {
                        // no op
                    } else if (bottomLeave > topHide) { // 下面空间比上面遮挡部分大
                        deltaYfrom = getTop();
{'fraction_non_alphanumeric': 0.09733930453108536, 'fraction_numerical': 0.010010537407797681, 'mean_word_length': 1.8374439461883407, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1086703', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2218, 'n_words': 597}