package com.example.jianming.Utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * Created by Jianming on 2015/3/20.
 */
public class YImageView extends ImageView {
    public YImageView(Context context) {
        super(context);
    }

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

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

    int start_top, start_left, start_right, start_bottom;
    int screamH, screamW;
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
        screamH = display.getHeight();
        screamW = display.getWidth();

        start_top = top;
        start_left = left;
        start_right = right;
        start_bottom = bottom;
        setFrame(0, 0, bitmap_W, bitmap_H);
        Log.i("onLayout", getTop() + " " + getLeft() + " " + getRight() + " " + getBottom());
        //Log.i("onLayout", top + " " + left + " " + right + " " + bottom);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                onTouchDown(event);

                break;
            case MotionEvent.ACTION_MOVE:
                onTouchMove(event);
                break;
        }

        return true;
    }

    int clicked = 0;

    void onTouchDown(MotionEvent event) {
        //mode = MODE.DRAG;

        current_x = (int) event.getRawX();
        current_y = (int) event.getRawY();

//        start_x = (int) event.getX();
//        start_y = current_y - this.getTop();
    }

    int current_x, current_y, start_x, start_y;

    void onTouchMove(MotionEvent event) {
        currLeft = this.getLeft();
        currTop = this.getTop();
        currBottom = this.getBottom();
        currRight = this.getRight();

        newX = (int) event.getRawX();
        newY = (int) event.getRawY();

        int diffX = newX - current_x;
        int diffY = newY - current_y;

        int newLeft = currLeft + diffX * 2;
        int newRight = currRight + diffX * 2;
        int newTop = currTop + diffY * 2;
        int newBottom = currBottom + diffY * 2;
        if (newLeft > 0 || newRight < screamW) {
            newLeft = currLeft;
            newRight = currRight;
        }
        if (newTop > 0 || newBottom <screamH) {
            newTop = currTop;
            newBottom = currBottom;
        }

        this.setFrame(newLeft, newTop, newRight, newBottom);

        current_x = (int) event.getRawX();
        current_y = (int) event.getRawY();
    }

    int currLeft, currTop, currBottom, currRight, newX, newY;

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        bitmap_W = bm.getWidth();
        bitmap_H = bm.getHeight();
        Log.i ("setImageBitmap", bitmap_H + " " + bitmap_W);
    }

    int bitmap_W, bitmap_H;

    public String picSize() {
        return bitmap_W + " * " + bitmap_H;
    }
}

@@ -244,16 +244,53 @@
 nEvent;%0A
+import android.view.VelocityTracker;%0A
 import a
@@ -1514,32 +1514,67 @@
 nEvent event) %7B%0A
+        addVelocityTracker(event);%0A
         switch (
@@ -1616,24 +1616,25 @@
 ION_MASK) %7B%0A
+%0A
             
@@ -1828,67 +1828,859 @@
 ak;%0A
-        %7D%0A%0A        return true;%0A    %7D%0A%0A    int clicked = 0;
+%0A            case MotionEvent.ACTION_UP:%0A                int velocityX = getScrollVelocity();%0A                recycleVelocityTracker();%0A                onTouchMove(event);%0A                break;%0A        %7D%0A%0A        return true;%0A    %7D%0A%0A    private VelocityTracker velocityTracker;%0A    private int getScrollVelocity() %7B%0A        velocityTracker.computeCurrentVelocity(1000);%0A        int velocity = (int) velocityTracker.getXVelocity();%0A        return velocity;%0A    %7D%0A%0A    private void addVelocityTracker(MotionEvent event) %7B%0A        if (velocityTracker == null) %7B%0A            velocityTracker = VelocityTracker.obtain();%0A        %7D%0A%0A        velocityTracker.addMovement(event);%0A    %7D%0A%0A    private void recycleVelocityTracker() %7B%0A        if (velocityTracker != null) %7B%0A            velocityTracker.recycle();%0A            velocityTracker = null;%0A        %7D%0A    %7D
 %0A%0A  

add velocityTracker