List of usage examples for android.graphics Rect unflattenFromString
public static Rect unflattenFromString(String str)
From source file:com.example.SmartBoard.DrawingView.java
@Override protected void onDraw(Canvas canvas) { drawCanvas.drawPath(drawPath, drawPaint); drawCanvas.drawPath(drawPathRecv, drawPaintSender); if (rectMode) { //draw rectangle drawPaint.setXfermode(null);// w w w .java 2 s . c o m onDrawRectangle(canvas); } else if (circleMode) { drawPaint.setXfermode(null); onDrawCircle(canvas); } else if (lineMode) { drawPaint.setXfermode(null); onDrawLine(canvas); } else if (textMode) { drawPaint.setXfermode(null); onDrawText(canvas); } else if (dragMode && !dragFinished) { drawPaint.setXfermode(null); onDragDraw(canvas, dragX, dragY); } else if (colorDropperMode) { drawPaint.setXfermode(null); onDrawColorDropper(canvas, dropperX, dropperY); } else if (textSizeMode) { drawPaint.setXfermode(null); } canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); //redraw objects Paint tempPaint = new Paint(); tempPaint.setStyle(Paint.Style.STROKE); for (String key : textObjects.keySet()) { if (key.compareTo("") == 0) { continue; } JSONObject o = textObjects.get(key); tempPaint.setColor(o.optInt("color")); tempPaint.setStrokeWidth(o.optInt("size")); canvas.drawBitmap(mqtt.stringToBitmap(o.optString("textBitmap")), o.optInt("x"), o.optInt("y"), tempPaint); } for (String key : objectDrawables.keySet()) { //hashtable problems no time to explain. But it creates a duplicate of last item I add to the table. //So dont print duplicates which have empty string key values if (key.compareTo("") == 0) { continue; } JSONObject o = objectDrawables.get(key); String objectType = o.optString("type"); tempPaint.setColor(o.optInt("color")); tempPaint.setStrokeWidth(o.optInt("size")); if (objectType.compareTo("Circle") == 0) { canvas.drawCircle(o.optInt("x"), o.optInt("y"), o.optInt("radius"), tempPaint); } else if (objectType.compareTo("Line") == 0) { canvas.drawLine(o.optInt("startx"), o.optInt("starty"), o.optInt("stopx"), o.optInt("stopy"), tempPaint); } else if (objectType.compareTo("Rectangle") == 0) { canvas.drawRect(Rect.unflattenFromString(o.optString("dimens")), tempPaint); } else if (objectType.compareTo("Text") == 0) { //canvas.drawRect(Rect.unflattenFromString(o.optString("region")), drawPaint); canvas.drawBitmap(mqtt.stringToBitmap(o.optString("textBitmap")), o.optInt("x"), o.optInt("y"), tempPaint); } } }
From source file:com.example.SmartBoard.DrawingView.java
public boolean onTouchDragEvent(MotionEvent event) { boolean handled = false; JSONObject mObjectTouched;/*from www . jav a 2s . co m*/ int X; int Y; int pointerId; int actionIndex = event.getActionIndex(); //get coordinates and make object transparent switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: dragFinished = false; //it's the first pointer, so clear all existing pointers data mObjectPointers.clear(); X = (int) event.getX(actionIndex); Y = (int) event.getY(actionIndex); dragX = X; dragY = Y; String objectType = ""; //check if we've touched inside some object mObjectTouched = getTouchedObject(X, Y); if (mObjectTouched == null) { return true; } else { objectType = getObjectType(mObjectTouched); } if (objectType.compareTo("Circle") == 0) { try { mObjectTouched.put("x", X); mObjectTouched.put("y", Y); } catch (JSONException e) { e.printStackTrace(); } } else if (objectType.compareTo("Rectangle") == 0) { Rect tempRect = Rect.unflattenFromString(mObjectTouched.optString("dimens")); tempRect.set(X, Y, X + tempRect.width(), Y + tempRect.height()); try { mObjectTouched.put("dimens", tempRect.flattenToString()); } catch (JSONException e) { } } else if (objectType.compareTo("Line") == 0) { if (lengthOfLine(X, Y, mObjectTouched.optInt("startx"), mObjectTouched.optInt("starty")) < lengthOfLine(X, Y, mObjectTouched.optInt("stopx"), mObjectTouched.optInt("stopy"))) { try { mObjectTouched.put("startx", X); mObjectTouched.put("starty", Y); mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("stopx"), mObjectTouched.optInt("stopy"))); } catch (JSONException e) { e.printStackTrace(); } dragLineStart = true; } else { try { mObjectTouched.put("stopx", X); mObjectTouched.put("stopy", Y); mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("startx"), mObjectTouched.optInt("starty"))); } catch (JSONException e) { e.printStackTrace(); } dragLineStart = false; } } else if (objectType.compareTo("Text") == 0) { try { mObjectTouched.put("x", X); mObjectTouched.put("y", Y); Rect tempRect = Rect.unflattenFromString(mObjectTouched.getString("region")); tempRect.set(X, Y, tempRect.width() + X, tempRect.height() + Y); mObjectTouched.put("region", tempRect.flattenToString()); } catch (JSONException e) { e.printStackTrace(); } } mObjectPointers.put(event.getPointerId(0), mObjectTouched); if (mObjectTouched != null) { mqtt.publishObject(mObjectTouched); } invalidate(); handled = true; break; case MotionEvent.ACTION_POINTER_DOWN: break; case MotionEvent.ACTION_MOVE: dragFinished = false; final int pointerCount = event.getPointerCount(); for (actionIndex = 0; actionIndex < pointerCount; actionIndex++) { //some pointer has moved, search it by pointer id pointerId = event.getPointerId(actionIndex); X = (int) event.getX(actionIndex); Y = (int) event.getY(actionIndex); dragX = X; dragY = Y; mObjectTouched = mObjectPointers.get(pointerId); if (mObjectTouched == null) continue; // if null no object was touched so skip if (mObjectTouched.optString("type").compareTo("Circle") == 0) { try { mObjectTouched.put("x", X); mObjectTouched.put("y", Y); } catch (JSONException e) { } } else if (mObjectTouched.optString("type").compareTo("Rectangle") == 0) { Rect tempRect = Rect.unflattenFromString(mObjectTouched.optString("dimens")); tempRect.set(X, Y, X + tempRect.width(), Y + tempRect.height()); try { mObjectTouched.put("dimens", tempRect.flattenToString()); } catch (JSONException e) { } } else if (mObjectTouched.optString("type").compareTo("Text") == 0) { try { mObjectTouched.put("x", X); mObjectTouched.put("y", Y); Rect tempRect = Rect.unflattenFromString(mObjectTouched.getString("region")); tempRect.set(X, Y, tempRect.width() + X, tempRect.height() + Y); mObjectTouched.put("region", tempRect.flattenToString()); } catch (JSONException e) { e.printStackTrace(); } } else if (mObjectTouched.optString("type").compareTo("Line") == 0) { if (dragLineStart) { try { mObjectTouched.put("startx", X); mObjectTouched.put("starty", Y); mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("stopx"), mObjectTouched.optInt("stopy"))); //mObjectTouched.put("stopx", tempStopX); // mObjectTouched.put("stopy", tempStopY); } catch (JSONException e) { e.printStackTrace(); } } else { try { mObjectTouched.put("stopx", X); mObjectTouched.put("stopy", Y); mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("startx"), mObjectTouched.optInt("starty"))); //mObjectTouched.put("stopx", tempStopX); // mObjectTouched.put("stopy", tempStopY); } catch (JSONException e) { e.printStackTrace(); } } } if (mObjectTouched != null) { mqtt.publishObject(mObjectTouched); } } invalidate(); handled = true; break; case MotionEvent.ACTION_UP: dragFinished = true; mObjectPointers.clear(); invalidate(); handled = true; break; case MotionEvent.ACTION_CANCEL: handled = true; break; default: // do nothing break; } return super.onTouchEvent(event) || handled; }
From source file:com.example.SmartBoard.DrawingView.java
private JSONObject getTouchedObject(final int xTouch, final int yTouch) { JSONObject touched = null;//from w ww . ja v a 2s .c o m //makes textObjects preferable on selection for drag //search for text object first for (JSONObject textObject : textObjects.values()) { Rect region = Rect.unflattenFromString(textObject.optString("region")); //System.out.println(region); if (region.contains(xTouch, yTouch)) { touched = textObject; return touched; } } for (JSONObject object : objectDrawables.values()) { if (object.optString("type").compareTo("Circle") == 0) { int x = object.optInt("x"); int y = object.optInt("y"); int radius = object.optInt("radius"); if ((x - xTouch) * (x - xTouch) + (y - yTouch) * (y - yTouch) <= radius * radius) { touched = object; break; } } else if (object.optString("type").compareTo("Rectangle") == 0) { Rect tempRect = Rect.unflattenFromString(object.optString("dimens")); if (tempRect.contains(xTouch, yTouch)) { touched = object; break; } } else if (object.optString("type").compareTo("Line") == 0) { double tempGradient = gradient(object.optInt("startx"), object.optInt("starty"), xTouch, yTouch); int startx = object.optInt("startx"); int stopx = object.optInt("stopx"); int starty = object.optInt("starty"); int stopy = object.optInt("stopy"); if (lengthOfLine(startx, starty, xTouch, yTouch) + lengthOfLine(stopx, stopy, xTouch, yTouch) <= object.optDouble("length") + 5) { touched = object; break; } } else if (object.optString("type").compareTo("Text") == 0) { Rect region = Rect.unflattenFromString(object.optString("region")); //System.out.println(region); if (region.contains(xTouch, yTouch)) { touched = object; break; } } } return touched; }