Example usage for android.graphics Rect flattenToString

List of usage examples for android.graphics Rect flattenToString

Introduction

In this page you can find the example usage for android.graphics Rect flattenToString.

Prototype

public String flattenToString() 

Source Link

Document

Return a string representation of the rectangle in a well-defined format.

Usage

From source file:com.example.SmartBoard.DrawingView.java

public boolean onTouchDragEvent(MotionEvent event) {

    boolean handled = false;

    JSONObject mObjectTouched;/*from   w  w  w.j a v a2  s  .c  o 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

public void onDrawText(Canvas canvas) {

    if (finished) {

        Bitmap bm = textToBitmap(textBoxData, drawPaint.getColor(), textPosX, textPosY, textViewSize);

        // create a rectangle to define the boundary

        Rect region = new Rect((int) textPosX - 5, (int) textPosY - 5, (int) textPosX + bm.getWidth() + 5,
                (int) textPosY + bm.getHeight() + 5);

        JSONObject textJson = new JSONObject();
        try {//from   w  w w  . j  a  v  a2  s .c  o  m

            String key = UUID.randomUUID().toString();
            textJson.put("type", "Text");
            textJson.put("id", key);
            textJson.put("clientId", client.getClientId());
            textJson.put("x", textPosX);
            textJson.put("y", textPosY);
            textJson.put("region", region.flattenToString());
            textJson.put("color", drawPaint.getColor());
            textJson.put("size", textViewSize);
            textJson.put("textBitmap", mqtt.bitmapToString(bm));
            textJson.put("text", textBoxData);
            // objectDrawables.put(key, textJson);
            textObjects.put(key, textJson);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mqtt.publishtext(textJson);

        finished = false;
        placed = true;

    }

}

From source file:com.example.SmartBoard.DrawingView.java

public void onDrawRectangle(Canvas canvas) {
    Paint paint = drawPaint;/*  w  ww .  ja  v  a2 s . c o  m*/
    if (points[3] == null) //point4 null when user did not touch and move on screen.
        return;
    int left, top, right, bottom;
    left = points[0].x;
    top = points[0].y;
    right = points[0].x;
    bottom = points[0].y;
    for (int i = 1; i < points.length; i++) {
        left = left > points[i].x ? points[i].x : left;
        top = top > points[i].y ? points[i].y : top;
        right = right < points[i].x ? points[i].x : right;
        bottom = bottom < points[i].y ? points[i].y : bottom;
    }

    //draw stroke
    paint.setStyle(Paint.Style.STROKE);
    // paint.setColor(Color.parseColor("#AADB1255"));
    paint.setStrokeWidth(5);

    if (finished) {

        Rect rect = new Rect(left + colorballs.get(0).getWidthOfBall() / 2,
                top + colorballs.get(0).getWidthOfBall() / 2, right + colorballs.get(2).getWidthOfBall() / 2,
                bottom + colorballs.get(2).getWidthOfBall() / 2);

        JSONObject objectProperties = new JSONObject();
        String key = UUID.randomUUID().toString();

        try {
            objectProperties.put("type", "Rectangle");
            objectProperties.put("clientId", client.getClientId());
            objectProperties.put("id", key);
            objectProperties.put("color", drawPaint.getColor());
            objectProperties.put("size", 5);
            objectProperties.put("dimens", rect.flattenToString());

        } catch (JSONException e) {

        }

        objectDrawables.put(key, objectProperties);

        mqtt.publishRectangle(objectProperties);
        //reset to start drawing again
        points = new Point[4];
        colorballs.clear();
        return;
    }

    //temporary canvas drawing on resize mode
    canvas.drawRect(left + colorballs.get(0).getWidthOfBall() / 2, top + colorballs.get(0).getWidthOfBall() / 2,
            right + colorballs.get(2).getWidthOfBall() / 2, bottom + colorballs.get(2).getWidthOfBall() / 2,
            paint);

    //draw the corners
    BitmapDrawable bitmap = new BitmapDrawable();
    // draw the balls on the canvas
    paint.setTextSize(18);
    paint.setStrokeWidth(0);

    for (int i = 0; i < colorballs.size(); i++) {
        ColorBall ball = colorballs.get(i);
        canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), paint);
        //  System.out.println("RectMode");

        canvas.drawText("" + (i + 1), ball.getX(), ball.getY(), paint);
    }
}