1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
| public class GestureDemoView extends View {
GestureDetector mGestureDetector; ScaleGestureDetector mScaleGestureDetector;
private Matrix mCanvasMatrix = new Matrix();
private Matrix mInvertMatrix = new Matrix();
private Matrix mUserMatrix = new Matrix();
private Bitmap mBitmap;
private float mBaseScale; private float mBaseTranslateX; private float mBaseTranslateY;
private Paint mPaint;
public GestureDemoView(Context context) { super(context); }
public GestureDemoView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); mPaint = new Paint(); mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test); initGesture(context); }
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh);
if (mBitmap.getWidth() * 1.0f / mBitmap.getHeight() > w * 1.0f / h) { mBaseScale = w * 1.0f / mBitmap.getWidth(); mBaseTranslateX = 0; mBaseTranslateY = (h - mBitmap.getHeight() * mBaseScale) / 2; } else { mBaseScale = h * 1.0f / mBitmap.getHeight() * 1.0f; mBaseTranslateX = (w - mBitmap.getWidth() * mBaseScale) / 2; mBaseTranslateY = 0; }
}
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(6); canvas.translate(mBaseTranslateX, mBaseTranslateY); canvas.scale(mBaseScale, mBaseScale);
canvas.save(); canvas.concat(mUserMatrix);
mCanvasMatrix = canvas.getMatrix(); mCanvasMatrix.invert(mInvertMatrix);
canvas.drawBitmap(mBitmap, 0, 0, mPaint); canvas.restore(); }
private void initGesture(Context context) { mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { float scale = getMatrixValue(MSCALE_X, mCanvasMatrix); mUserMatrix.preTranslate(-distanceX / scale, -distanceY / scale); invalidate(); return true; }
@Override public boolean onDoubleTap(MotionEvent e) { if (!mUserMatrix.isIdentity()) { mUserMatrix.reset(); } else { float[] points = mapPoint(e.getX(), e.getY(), mInvertMatrix); mUserMatrix.postScale(MAX_SCALE, MAX_SCALE, points[0], points[1]); } fixTranslate(); invalidate(); return true; } });
mScaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.SimpleOnScaleGestureListener() { @Override public boolean onScale(ScaleGestureDetector detector) { float scaleFactor = detector.getScaleFactor(); float fx = detector.getFocusX(); float fy = detector.getFocusY(); float[] points = mapPoint(fx, fy, mInvertMatrix); scaleFactor = getRealScaleFactor(scaleFactor); mUserMatrix.preScale(scaleFactor, scaleFactor, points[0], points[1]); fixTranslate(); invalidate(); return true; }
}); }
private void fixTranslate() { Matrix viewMatrix = getMatrix(); viewMatrix.preTranslate(mBaseTranslateX, mBaseTranslateY); viewMatrix.preScale(mBaseScale, mBaseScale); viewMatrix.preConcat(mUserMatrix); Matrix invert = new Matrix(); viewMatrix.invert(invert); Rect rect = new Rect(); getGlobalVisibleRect(rect);
float userScale = getMatrixValue(MSCALE_X, mUserMatrix); float scale = getMatrixValue(MSCALE_X, viewMatrix);
float[] center = mapPoint(mBitmap.getWidth() / 2.0f, mBitmap.getHeight() / 2.0f, viewMatrix); float distanceX = center[0] - getWidth() / 2.0f; float distanceY = center[1] - getHeight() / 2.0f; float[] wh = mapVectors(mBitmap.getWidth(), mBitmap.getHeight(), viewMatrix);
if (userScale <= 1.0f) { mUserMatrix.preTranslate(-distanceX / scale, -distanceY / scale); } else { float[] lefttop = mapPoint(0, 0, viewMatrix); float[] rightbottom = mapPoint(mBitmap.getWidth(), mBitmap.getHeight(), viewMatrix);
if (wh[0] < getWidth()) { mUserMatrix.preTranslate(distanceX / scale, 0); } else { if (lefttop[0] > 0) { mUserMatrix.preTranslate(-lefttop[0] / scale, 0); } else if (rightbottom[0] < getWidth()) { mUserMatrix.preTranslate((getWidth() - rightbottom[0]) / scale, 0); }
} if (wh[1] < getHeight()) { mUserMatrix.preTranslate(0, -distanceY / scale); } else { if (lefttop[1] > 0) { mUserMatrix.preTranslate(0, -lefttop[1] / scale); } else if (rightbottom[1] < getHeight()) { mUserMatrix.preTranslate(0, (getHeight() - rightbottom[1]) / scale); } } } invalidate(); }
@Override public boolean onTouchEvent(MotionEvent event) { mGestureDetector.onTouchEvent(event); mScaleGestureDetector.onTouchEvent(event); if (event.getActionMasked() == MotionEvent.ACTION_UP) { fixTranslate(); } return true; }
private float[] mapPoint(float x, float y, Matrix matrix) { float[] temp = new float[2]; temp[0] = x; temp[1] = y; matrix.mapPoints(temp); return temp; }
private float[] mapVectors(float x, float y, Matrix matrix) { float[] temp = new float[2]; temp[0] = x; temp[1] = y; matrix.mapVectors(temp); return temp; }
private float[] matrixValues = new float[9]; private static final int MSCALE_X = 0, MSKEW_X = 1, MTRANS_X = 2; private static final int MSKEW_Y = 3, MSCALE_Y = 4, MTRANS_Y = 5; private static final int MPERSP_0 = 6, MPERSP_1 = 7, MPERSP_2 = 8;
@IntDef({MSCALE_X, MSKEW_X, MTRANS_X, MSKEW_Y, MSCALE_Y, MTRANS_Y, MPERSP_0, MPERSP_1, MPERSP_2}) @Retention(RetentionPolicy.SOURCE) private @interface MatrixName {}
private float getMatrixValue(@MatrixName int name, Matrix matrix) { matrix.getValues(matrixValues); return matrixValues[name]; }
private static final float MAX_SCALE = 4.0f; private static final float MIN_SCALE = 0.5f;
private float getRealScaleFactor(float currentScaleFactor) { float realScale = 1.0f; float userScale = getMatrixValue(MSCALE_X, mUserMatrix); float theoryScale = userScale * currentScaleFactor;
if (currentScaleFactor > 1.0f && theoryScale > MAX_SCALE) { realScale = MAX_SCALE / userScale; } else if (currentScaleFactor < 1.0f && theoryScale < MIN_SCALE) { realScale = MIN_SCALE / userScale; } else { realScale = currentScaleFactor; } return realScale; } }
|