Back to project page Common-Library.
The source code is released under:
Apache License
If you think the Android project Common-Library listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.morgan.library.widget; /* w ww.j a va 2s. co m*/ import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import android.util.FloatMath; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.view.animation.TranslateAnimation; import android.widget.ImageView; /** * ???????????????????? * * @author Morgan.Ji */ public class TouchZoomImageView extends ImageView { // ?????????? static final int NONE = 0; // ???????????? static final int DRAG = 1; // ???????????? static final int ZOOM = 2; // mode?????????????? private int mMode = NONE; // ?????? static final int ZOOM_BIGGER = 3; // ??????? static final int ZOOM_SMALLER = 4; // ??????????? private float mBeforeLenght; // ??????????? private float mAfterLenght; // ????? private float mZoomScale = 0.04f; // ???????????linearLayout???(?????????) private int mParentW; private int mParentH; private boolean mInitParentSize = false; // ????? private int mStartX; private int mStartY; // ??????? private int mStopX; private int mStopY; // ???? private TranslateAnimation mAnimation; // ?????????????????(???????????????????????????????????????????) private boolean mIsMulti; // ?????????????????? private static final int MIN_LENGTH = 150; public TouchZoomImageView(Context context, AttributeSet attrs) { super(context, attrs); this.setPadding(0, 0, 0, 0); this.setScaleType(ScaleType.FIT_XY); this.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } // ??????2???????? @SuppressLint("FloatMath") 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); } @Override public boolean onTouchEvent(MotionEvent event) { if (!mInitParentSize) { View parentView = (View) getParent(); mParentW = parentView.getWidth(); mParentH = parentView.getHeight(); mInitParentSize = true; } // MotionEvent.ACTION_MASK???????? switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mMode = DRAG; // ????????????????????getX???????,?(int)event.getX()??,????????????????????????imageView????????. // ???????????????????????????????????getX()????????????? mStopX = (int) event.getRawX(); mStopY = (int) event.getRawY(); // ??????????????????????x?y?????start?stop??????????????????????????????????start?stop????????????????????getLeft??????? mStartX = mStopX - this.getLeft(); mStartY = mStopY - this.getTop(); // ???? mIsMulti = true; break; case MotionEvent.ACTION_POINTER_DOWN: // ????????? if (spacing(event) > 10f) { mMode = ZOOM; mBeforeLenght = spacing(event); // ?????????????????????????????????? if (event.getX(1) < getWidth() && event.getX(1) > 0 && event.getY(1) > 0 && event.getY(1) < getHeight()) { mIsMulti = true; } else { mIsMulti = false; } } break; case MotionEvent.ACTION_UP: // ?????????????????150????????????150???????? while (getHeight() < MIN_LENGTH || getWidth() < MIN_LENGTH) { setScale(mZoomScale, ZOOM_BIGGER); } int disX = 0; int disY = 0; if (getHeight() <= mParentH) { if (this.getTop() < 0) { disY = getTop(); this.layout(this.getLeft(), 0, this.getRight(), 0 + this.getHeight()); } else if (this.getBottom() > mParentH) { disY = getBottom() - mParentH; this.layout(this.getLeft(), mParentH - getHeight(), this.getRight(), mParentH); } } else { int Y1 = getTop(); int Y2 = getHeight() - mParentH + getTop(); if (Y1 > 0) { disY = Y1; this.layout(this.getLeft(), 0, this.getRight(), 0 + this.getHeight()); } else if (Y2 < 0) { disY = Y2; this.layout(this.getLeft(), mParentH - getHeight(), this.getRight(), mParentH); } } if (getWidth() <= mParentW) { if (this.getLeft() < 0) { disX = getLeft(); this.layout(0, this.getTop(), 0 + getWidth(), this.getBottom()); } else if (this.getRight() > mParentW) { disX = getWidth() - mParentW + getLeft(); this.layout(mParentW - getWidth(), this.getTop(), mParentW, this.getBottom()); } } else { int X1 = getLeft(); int X2 = getWidth() - mParentW + getLeft(); if (X1 > 0) { disX = X1; this.layout(0, this.getTop(), 0 + getWidth(), this.getBottom()); } else if (X2 < 0) { disX = X2; this.layout(mParentW - getWidth(), this.getTop(), mParentW, this.getBottom()); } } // ????disX?disY????????????????????????500??? if (disX != 0 || disY != 0) { mAnimation = new TranslateAnimation(disX, 0, disY, 0); mAnimation.setDuration(500); this.startAnimation(mAnimation); } mMode = NONE; break; case MotionEvent.ACTION_POINTER_UP: mMode = NONE; break; case MotionEvent.ACTION_MOVE: if (mMode == DRAG) { // ??????????????????? mStopX = (int) event.getRawX(); mStopY = (int) event.getRawY(); // ?????????????????????imageView???????????? this.setPosition(mStopX - mStartX, mStopY - mStartY, mStopX + this.getWidth() - mStartX, mStopY - mStartY + this.getHeight()); } else if (mMode == ZOOM && mIsMulti) { if (spacing(event) > 10f) { mAfterLenght = spacing(event); float gapLenght = mAfterLenght - mBeforeLenght; if (gapLenght == 0) { break; } // ???????????imageView?????70???????? else if (Math.abs(gapLenght) > 5f && getWidth() > 70) { if (gapLenght > 0) { this.setScale(mZoomScale, ZOOM_BIGGER); } else { this.setScale(mZoomScale, ZOOM_SMALLER); } mBeforeLenght = mAfterLenght; // ????????? } } } break; } return true; } private void setScale(float temp, int flag) { if (flag == ZOOM_BIGGER) { // setFrame(left , top, right,bottom)???????????view??????????? this.setFrame(this.getLeft() - (int) (temp * this.getWidth()), this.getTop() - (int) (temp * this.getHeight()), this.getRight() + (int) (temp * this.getWidth()), this.getBottom() + (int) (temp * this.getHeight())); } else if (flag == ZOOM_SMALLER) { this.setFrame(this.getLeft() + (int) (temp * this.getWidth()), this.getTop() + (int) (temp * this.getHeight()), this.getRight() - (int) (temp * this.getWidth()), this.getBottom() - (int) (temp * this.getHeight())); } } private void setPosition(int left, int top, int right, int bottom) { this.layout(left, top, right, bottom); } }