@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (Figure figure : mFigures) { figure.draw(canvas); } }
@Override public boolean onScale(ScaleGestureDetector detector) { if (mActiveIndex >= 0) { mScaleFactor *= detector.getScaleFactor(); mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f)); Figure f = mFigures.get(mActiveIndex); f.width = (int) (origWidth * mScaleFactor); f.height = (int) (origHeight * mScaleFactor); f.x = origX - (f.width - origWidth) / 2; f.y = origY - (f.height - origHeight) / 2; invalidate(); return true; } return false; }
public void addFigure(Figure figure, boolean invalidate) { int typeIndex; if (typeIndexes.containsKey(figure.type)) { typeIndex = typeIndexes.get(figure.type) + 1; } else { typeIndex = 0; } typeIndexes.put(figure.type, typeIndex); figure.setColorIndex(typeIndex); mFigures.add(figure); if (invalidate) invalidate(); }
@Override public boolean onTouchEvent(MotionEvent ev) { // return or not? mScaleDetector.onTouchEvent(ev); mGestureDetector.onTouchEvent(ev); final int action = MotionEventCompat.getActionMasked(ev); switch (action) { case MotionEvent.ACTION_DOWN: { final int pointerIndex = MotionEventCompat.getActionIndex(ev); final float x = MotionEventCompat.getX(ev, pointerIndex); final float y = MotionEventCompat.getY(ev, pointerIndex); int index = getActiveIndexByXY(x, y); if (index >= 0) { mActiveIndex = index; mLastTouchX = x; mLastTouchY = y; mActivePointerId = MotionEventCompat.getPointerId(ev, 0); } break; } case MotionEvent.ACTION_MOVE: if (mActivePointerId != INVALID_POINTER_ID) { final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = MotionEventCompat.getX(ev, pointerIndex); final float y = MotionEventCompat.getY(ev, pointerIndex); final float dx = x - mLastTouchX; final float dy = y - mLastTouchY; Figure figure = mFigures.get(mActiveIndex); figure.x += dx; figure.y += dy; mLastTouchX = x; mLastTouchY = y; invalidate(); } break; case MotionEvent.ACTION_UP: { if (mActiveIndex >= 0) { if (mUpdateFigureListener != null) { mUpdateFigureListener.onUpdate(mFigures.get(mActiveIndex)); } mActiveIndex = -1; } mActivePointerId = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_CANCEL: { mActivePointerId = INVALID_POINTER_ID; mActiveIndex = -1; break; } case MotionEvent.ACTION_POINTER_UP: final int pointerIndex = MotionEventCompat.getActionIndex(ev); final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); if (pointerId == mActivePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex); mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex); mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex); } break; } return true; }