Example usage for android.view ScaleGestureDetector getScaleFactor

List of usage examples for android.view ScaleGestureDetector getScaleFactor

Introduction

In this page you can find the example usage for android.view ScaleGestureDetector getScaleFactor.

Prototype

public float getScaleFactor() 

Source Link

Document

Return the scaling factor from the previous scale event to the current event.

Usage

From source file:com.android.volley.ui.PhotoView.java

@Override
public boolean onScale(ScaleGestureDetector detector) {
    if (mTransformsEnabled) {
        mIsDoubleTouch = false;//from w ww.  jav a  2 s . c  om
        float currentScale = getScale();
        float newScale = currentScale * detector.getScaleFactor();
        scale(newScale, detector.getFocusX(), detector.getFocusY());
    }
    return true;
}

From source file:com.diegocarloslima.byakugallery.TouchImageView.java

public TouchImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    if (ANIMATION_DURATION == 0)
        ANIMATION_DURATION = context.getResources().getInteger(android.R.integer.config_shortAnimTime);

    final TouchGestureDetector.OnTouchGestureListener listener = new TouchGestureDetector.OnTouchGestureListener() {

        @Override//from   w  w  w  . j  a v a2  s . c o  m
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return performClick();
        }

        @Override
        public void onLongPress(MotionEvent e) {
            performLongClick();
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            loadMatrixValues();

            // 3 stage scaling
            float targetScale = mCropScale;
            if (mScale == mMaxScale)
                targetScale = mMinScale;
            else if (mScale >= mCropScale)
                targetScale = mMaxScale;

            // First, we try to keep the focused point in the same position when the animation ends
            final float desiredTranslationX = e.getX() - (e.getX() - mTranslationX) * (targetScale / mScale);
            final float desiredTranslationY = e.getY() - (e.getY() - mTranslationY) * (targetScale / mScale);

            // Here, we apply a correction to avoid unwanted blank spaces
            final float targetTranslationX = desiredTranslationX + computeTranslation(getMeasuredWidth(),
                    mDrawableIntrinsicWidth * targetScale, desiredTranslationX, 0);
            final float targetTranslationY = desiredTranslationY + computeTranslation(getMeasuredHeight(),
                    mDrawableIntrinsicHeight * targetScale, desiredTranslationY, 0);

            clearAnimation();
            final Animation animation = new TouchAnimation(targetScale, targetTranslationX, targetTranslationY);
            animation.setDuration(ANIMATION_DURATION);
            startAnimation(animation);

            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // Sometimes, this method is called just after an onScaleEnd event. In this case, we want to wait until we animate back our image
            if (mIsAnimatingBack) {
                return false;
            }

            loadMatrixValues();

            final float currentDrawableWidth = mDrawableIntrinsicWidth * mScale;
            final float currentDrawableHeight = mDrawableIntrinsicHeight * mScale;

            final float dx = computeTranslation(getMeasuredWidth(), currentDrawableWidth, mTranslationX,
                    -distanceX);
            final float dy = computeTranslation(getMeasuredHeight(), currentDrawableHeight, mTranslationY,
                    -distanceY);
            mMatrix.postTranslate(dx, dy);

            clearAnimation();
            ViewCompat.postInvalidateOnAnimation(TouchImageView.this);

            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            // Sometimes, this method is called just after an onScaleEnd event. In this case, we want to wait until we animate back our image
            if (mIsAnimatingBack) {
                return false;
            }

            loadMatrixValues();

            final float horizontalSideFreeSpace = (getMeasuredWidth() - mDrawableIntrinsicWidth * mScale) / 2F;
            final float minTranslationX = horizontalSideFreeSpace > 0 ? horizontalSideFreeSpace
                    : getMeasuredWidth() - mDrawableIntrinsicWidth * mScale;
            final float maxTranslationX = horizontalSideFreeSpace > 0 ? horizontalSideFreeSpace : 0;

            final float verticalSideFreeSpace = (getMeasuredHeight() - mDrawableIntrinsicHeight * mScale) / 2F;
            final float minTranslationY = verticalSideFreeSpace > 0 ? verticalSideFreeSpace
                    : getMeasuredHeight() - mDrawableIntrinsicHeight * mScale;
            final float maxTranslationY = verticalSideFreeSpace > 0 ? verticalSideFreeSpace : 0;

            // Using FlingScroller here. The results were better than the Scroller class
            // https://android.googlesource.com/platform/packages/apps/Gallery2/+/master/src/com/android/gallery3d/ui/FlingScroller.java
            mFlingScroller.fling(Math.round(mTranslationX), Math.round(mTranslationY), Math.round(velocityX),
                    Math.round(velocityY), Math.round(minTranslationX), Math.round(maxTranslationX),
                    Math.round(minTranslationY), Math.round(maxTranslationY));

            clearAnimation();
            final Animation animation = new FlingAnimation();
            animation.setDuration(mFlingScroller.getDuration());
            animation.setInterpolator(new LinearInterpolator());
            startAnimation(animation);

            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            mLastFocusX = null;
            mLastFocusY = null;

            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            loadMatrixValues();

            float currentDrawableWidth = mDrawableIntrinsicWidth * mScale;
            float currentDrawableHeight = mDrawableIntrinsicHeight * mScale;

            final float focusX = computeFocus(getMeasuredWidth(), currentDrawableWidth, mTranslationX,
                    detector.getFocusX());
            final float focusY = computeFocus(getMeasuredHeight(), currentDrawableHeight, mTranslationY,
                    detector.getFocusY());

            // Here, we provide the ability to scroll while scaling
            if (mLastFocusX != null && mLastFocusY != null) {
                final float dx = computeScaleTranslation(getMeasuredWidth(), currentDrawableWidth,
                        mTranslationX, focusX - mLastFocusX);
                final float dy = computeScaleTranslation(getMeasuredHeight(), currentDrawableHeight,
                        mTranslationY, focusY - mLastFocusY);

                if (dx != 0 || dy != 0) {
                    mMatrix.postTranslate(dx, dy);
                }
            }

            final float scale = computeScale(mMinScale, mMaxScale, mScale, detector.getScaleFactor());
            mMatrix.postScale(scale, scale, focusX, focusY);

            mLastFocusX = focusX;
            mLastFocusY = focusY;

            clearAnimation();
            ViewCompat.postInvalidateOnAnimation(TouchImageView.this);

            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            loadMatrixValues();

            final float currentDrawableWidth = mDrawableIntrinsicWidth * mScale;
            final float currentDrawableHeight = mDrawableIntrinsicHeight * mScale;

            final float dx = computeTranslation(getMeasuredWidth(), currentDrawableWidth, mTranslationX, 0);
            final float dy = computeTranslation(getMeasuredHeight(), currentDrawableHeight, mTranslationY, 0);

            if (Math.abs(dx) < 1 && Math.abs(dy) < 1) {
                return;
            }

            final float targetTranslationX = mTranslationX + dx;
            final float targetTranslationY = mTranslationY + dy;

            float targetScale = MathUtils.clamp(mScale, mMinScale, mMaxScale);

            clearAnimation();
            final Animation animation = new TouchAnimation(targetScale, targetTranslationX, targetTranslationY);
            animation.setDuration(ANIMATION_DURATION);
            startAnimation(animation);

            mIsAnimatingBack = true;
        }
    };

    mTouchGestureDetector = new TouchGestureDetector(context, listener);

    super.setScaleType(ScaleType.MATRIX);
}

From source file:org.mariotaku.twidere.view.TouchImageView.java

public TouchImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    final TouchGestureDetector.OnTouchGestureListener listener = new TouchGestureDetector.OnTouchGestureListener() {

        @Override/*from ww  w .ja va 2  s  . c  o m*/
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return performClick();
        }

        @Override
        public void onLongPress(MotionEvent e) {
            performLongClick();
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            loadMatrixValues();

            final float minScale = getMinScale();
            // If we have already zoomed in, we should return to our initial scale value (minScale). Otherwise, scale to full size
            final boolean shouldZoomOut = mScale > minScale;
            final float targetScale = shouldZoomOut ? minScale : mMaxScale;

            // First, we try to keep the focused point in the same position when the animation ends
            final float desiredTranslationX = e.getX() - (e.getX() - mTranslationX) * (targetScale / mScale);
            final float desiredTranslationY = e.getY() - (e.getY() - mTranslationY) * (targetScale / mScale);

            // Here, we apply a correction to avoid unwanted blank spaces
            final float targetTranslationX = desiredTranslationX + computeTranslation(getMeasuredWidth(),
                    mDrawableIntrinsicWidth * targetScale, desiredTranslationX, 0);
            final float targetTranslationY = desiredTranslationY + computeTranslation(getMeasuredHeight(),
                    mDrawableIntrinsicHeight * targetScale, desiredTranslationY, 0);

            clearAnimation();
            final Animation animation = new TouchAnimation(targetScale, targetTranslationX, targetTranslationY);
            animation.setDuration(DOUBLE_TAP_ANIMATION_DURATION);
            startAnimation(animation);

            if (mZoomListener != null) {
                if (shouldZoomOut) {
                    mZoomListener.onZoomOut();
                } else {
                    mZoomListener.onZoomIn();
                }
            }
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // Sometimes, this method is called just after an onScaleEnd event. In this case, we want to wait until we animate back our image
            if (mIsAnimatingBack) {
                return false;
            }

            loadMatrixValues();

            final float currentDrawableWidth = mDrawableIntrinsicWidth * mScale;
            final float currentDrawableHeight = mDrawableIntrinsicHeight * mScale;

            final float dx = computeTranslation(getMeasuredWidth(), currentDrawableWidth, mTranslationX,
                    -distanceX);
            final float dy = computeTranslation(getMeasuredHeight(), currentDrawableHeight, mTranslationY,
                    -distanceY);
            mMatrix.postTranslate(dx, dy);

            clearAnimation();
            ViewCompat.postInvalidateOnAnimation(TouchImageView.this);

            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            // Sometimes, this method is called just after an onScaleEnd event. In this case, we want to wait until we animate back our image
            if (mIsAnimatingBack) {
                return false;
            }

            loadMatrixValues();

            final float horizontalSideFreeSpace = (getMeasuredWidth() - mDrawableIntrinsicWidth * mScale) / 2F;
            final float minTranslationX = horizontalSideFreeSpace > 0 ? horizontalSideFreeSpace
                    : getMeasuredWidth() - mDrawableIntrinsicWidth * mScale;
            final float maxTranslationX = horizontalSideFreeSpace > 0 ? horizontalSideFreeSpace : 0;

            final float verticalSideFreeSpace = (getMeasuredHeight() - mDrawableIntrinsicHeight * mScale) / 2F;
            final float minTranslationY = verticalSideFreeSpace > 0 ? verticalSideFreeSpace
                    : getMeasuredHeight() - mDrawableIntrinsicHeight * mScale;
            final float maxTranslationY = verticalSideFreeSpace > 0 ? verticalSideFreeSpace : 0;

            // Using FlingScroller here. The results were better than the Scroller class
            // https://android.googlesource.com/platform/packages/apps/Gallery2/+/master/src/com/android/gallery3d/ui/FlingScroller.java
            mFlingScroller.fling(Math.round(mTranslationX), Math.round(mTranslationY), Math.round(velocityX),
                    Math.round(velocityY), Math.round(minTranslationX), Math.round(maxTranslationX),
                    Math.round(minTranslationY), Math.round(maxTranslationY));

            clearAnimation();
            final Animation animation = new FlingAnimation();
            animation.setDuration(mFlingScroller.getDuration());
            animation.setInterpolator(new LinearInterpolator());
            startAnimation(animation);

            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            mLastFocusX = null;
            mLastFocusY = null;

            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            loadMatrixValues();

            float currentDrawableWidth = mDrawableIntrinsicWidth * mScale;
            float currentDrawableHeight = mDrawableIntrinsicHeight * mScale;

            final float focusX = computeFocus(getMeasuredWidth(), currentDrawableWidth, mTranslationX,
                    detector.getFocusX());
            final float focusY = computeFocus(getMeasuredHeight(), currentDrawableHeight, mTranslationY,
                    detector.getFocusY());

            // Here, we provide the ability to scroll while scaling
            if (mLastFocusX != null && mLastFocusY != null) {
                final float dx = computeScaleTranslation(getMeasuredWidth(), currentDrawableWidth,
                        mTranslationX, focusX - mLastFocusX);
                final float dy = computeScaleTranslation(getMeasuredHeight(), currentDrawableHeight,
                        mTranslationY, focusY - mLastFocusY);

                if (dx != 0 || dy != 0) {
                    mMatrix.postTranslate(dx, dy);
                }
            }

            final float scale = computeScale(getMinScale(), mMaxScale, mScale, detector.getScaleFactor());
            mMatrix.postScale(scale, scale, focusX, focusY);

            mLastFocusX = focusX;
            mLastFocusY = focusY;

            clearAnimation();
            ViewCompat.postInvalidateOnAnimation(TouchImageView.this);

            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            loadMatrixValues();

            final float currentDrawableWidth = mDrawableIntrinsicWidth * mScale;
            final float currentDrawableHeight = mDrawableIntrinsicHeight * mScale;

            final float dx = computeTranslation(getMeasuredWidth(), currentDrawableWidth, mTranslationX, 0);
            final float dy = computeTranslation(getMeasuredHeight(), currentDrawableHeight, mTranslationY, 0);

            if (Math.abs(dx) < 1 && Math.abs(dy) < 1) {
                return;
            }

            final float targetTranslationX = mTranslationX + dx;
            final float targetTranslationY = mTranslationY + dy;

            clearAnimation();
            final Animation animation = new TouchAnimation(mScale, targetTranslationX, targetTranslationY);
            animation.setDuration(SCALE_END_ANIMATION_DURATION);
            startAnimation(animation);

            mIsAnimatingBack = true;
        }
    };

    mTouchGestureDetector = new TouchGestureDetector(context, listener);

    super.setScaleType(ScaleType.MATRIX);
}

From source file:org.appcelerator.titanium.view.TiUIView.java

protected void registerTouchEvents(final View touchable) {

    touchView = new WeakReference<View>(touchable);

    final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(touchable.getContext(),
            new SimpleOnScaleGestureListener() {
                // protect from divide by zero errors
                long minTimeDelta = 1;
                float minStartSpan = 1.0f;
                float startSpan;

                @Override//from w  w w.j  av  a2  s  .  c  o  m
                public boolean onScale(ScaleGestureDetector sgd) {
                    if (proxy.hierarchyHasListener(TiC.EVENT_PINCH)) {
                        float timeDelta = sgd.getTimeDelta() == 0 ? minTimeDelta : sgd.getTimeDelta();

                        // Suppress scale events (and allow for possible two-finger tap events)
                        // until we've moved at least a few pixels. Without this check, two-finger 
                        // taps are very hard to register on some older devices.
                        if (!didScale) {
                            if (Math.abs(sgd.getCurrentSpan() - startSpan) > SCALE_THRESHOLD) {
                                didScale = true;
                            }
                        }

                        if (didScale) {
                            KrollDict data = new KrollDict();
                            data.put(TiC.EVENT_PROPERTY_SCALE, sgd.getCurrentSpan() / startSpan);
                            data.put(TiC.EVENT_PROPERTY_VELOCITY,
                                    (sgd.getScaleFactor() - 1.0f) / timeDelta * 1000);
                            data.put(TiC.EVENT_PROPERTY_SOURCE, proxy);

                            return proxy.fireEvent(TiC.EVENT_PINCH, data);
                        }
                    }
                    return false;
                }

                @Override
                public boolean onScaleBegin(ScaleGestureDetector sgd) {
                    startSpan = sgd.getCurrentSpan() == 0 ? minStartSpan : sgd.getCurrentSpan();
                    return true;
                }
            });

    final GestureDetector detector = new GestureDetector(touchable.getContext(), new SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if (proxy.hierarchyHasListener(TiC.EVENT_DOUBLE_TAP)
                    || proxy.hierarchyHasListener(TiC.EVENT_DOUBLE_CLICK)) {
                boolean handledTap = proxy.fireEvent(TiC.EVENT_DOUBLE_TAP, dictFromEvent(e));
                boolean handledClick = proxy.fireEvent(TiC.EVENT_DOUBLE_CLICK, dictFromEvent(e));
                return handledTap || handledClick;
            }
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.d(TAG, "TAP, TAP, TAP on " + proxy, Log.DEBUG_MODE);
            if (proxy.hierarchyHasListener(TiC.EVENT_SINGLE_TAP)) {
                return proxy.fireEvent(TiC.EVENT_SINGLE_TAP, dictFromEvent(e));
                // Moved click handling to the onTouch listener, because a single tap is not the
                // same as a click. A single tap is a quick tap only, whereas clicks can be held
                // before lifting.
                // boolean handledClick = proxy.fireEvent(TiC.EVENT_CLICK, dictFromEvent(event));
                // Note: this return value is irrelevant in our case. We "want" to use it
                // in onTouch below, when we call detector.onTouchEvent(event); But, in fact,
                // onSingleTapConfirmed is *not* called in the course of onTouchEvent. It's
                // called via Handler in GestureDetector. <-- See its Java source.
                // return handledTap;// || handledClick;
            }
            return false;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.d(TAG, "SWIPE on " + proxy, Log.DEBUG_MODE);
            if (proxy.hierarchyHasListener(TiC.EVENT_SWIPE)) {
                KrollDict data = dictFromEvent(e2);
                if (Math.abs(velocityX) > Math.abs(velocityY)) {
                    data.put(TiC.EVENT_PROPERTY_DIRECTION, velocityX > 0 ? "right" : "left");
                } else {
                    data.put(TiC.EVENT_PROPERTY_DIRECTION, velocityY > 0 ? "down" : "up");
                }
                return proxy.fireEvent(TiC.EVENT_SWIPE, data);
            }
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            Log.d(TAG, "LONGPRESS on " + proxy, Log.DEBUG_MODE);

            if (proxy.hierarchyHasListener(TiC.EVENT_LONGPRESS)) {
                proxy.fireEvent(TiC.EVENT_LONGPRESS, dictFromEvent(e));
            }
        }
    });

    touchable.setOnTouchListener(new OnTouchListener() {
        int pointersDown = 0;

        public boolean onTouch(View view, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                lastUpEvent.put(TiC.EVENT_PROPERTY_X, (double) event.getX());
                lastUpEvent.put(TiC.EVENT_PROPERTY_Y, (double) event.getY());
            }

            scaleDetector.onTouchEvent(event);
            if (scaleDetector.isInProgress()) {
                pointersDown = 0;
                return true;
            }

            boolean handled = detector.onTouchEvent(event);
            if (handled) {
                pointersDown = 0;
                return true;
            }

            if (event.getActionMasked() == MotionEvent.ACTION_POINTER_UP) {
                if (didScale) {
                    didScale = false;
                    pointersDown = 0;
                } else {
                    pointersDown++;
                }
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                if (pointersDown == 1) {
                    proxy.fireEvent(TiC.EVENT_TWOFINGERTAP, dictFromEvent(event));
                    pointersDown = 0;
                    return true;
                }
                pointersDown = 0;
            }

            String motionEvent = motionEvents.get(event.getAction());
            if (motionEvent != null) {
                if (proxy.hierarchyHasListener(motionEvent)) {
                    proxy.fireEvent(motionEvent, dictFromEvent(event));
                }
            }

            // Inside View.java, dispatchTouchEvent() does not call onTouchEvent() if this listener returns true. As
            // a result, click and other motion events do not occur on the native Android side. To prevent this, we
            // always return false and let Android generate click and other motion events.
            return false;
        }
    });

}

From source file:pl.edu.agh.mindmapex.gui.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    progressDialog = ProgressDialog.show(this, "Drawing", "Please wait...", true, false);
    if (WelcomeScreen.workbook != null) {
        workbook = WelcomeScreen.workbook;
    }/*w w  w . j  a v  a2  s .  c  o  m*/
    res = getResources();
    if (handler == null) {
        handler = WorkbookHandler.createNewWorkbook();
    }

    if (workbook == null) {
        workbook = handler.getWorkbook();
        styleSheet = workbook.getStyleSheet();
        style1 = styleSheet.createStyle(IStyle.TOPIC);
        styleSheet.addStyle(style1, IStyleSheet.NORMAL_STYLES);
    }
    styleSheet = workbook.getStyleSheet();
    sheet1 = workbook.getPrimarySheet();
    res = getResources();
    if (style == null) {
        Intent intent = getIntent();
        style = intent.getStringExtra(WelcomeScreen.STYLE);
    }
    if (root == null) {

        //            Display display = getWindowManager().getDefaultDisplay();
        setContentView(R.layout.main_content_view);
        lay = (DrawView) findViewById(R.id.main_draw_view);
        lay.post(new Runnable() {
            @Override
            public void run() {
                rootTopic = sheet1.getRootTopic();
                root = new Box();

                //            Point size = new Point(lay.getWidth(), lay.getHeight());
                //            width = size.x / 2;
                //            height = size.y / 2;
                root.setPoint(new pl.edu.agh.mindmapex.common.Point(
                        lay.getWidth() / 2 - getResources().getDimensionPixelSize(R.dimen.init_box_size),
                        lay.getHeight() / 2
                                - getResources().getDimensionPixelSize(R.dimen.init_box_size_height)));
                lay.setZOrderOnTop(true);

                if (style.equals("ReadyMap")) {

                    if (sheet1.getTheme() == null) {
                        root.setPoint(new pl.edu.agh.mindmapex.common.Point(width, height));
                        final HashMap<String, Box> boxes = new HashMap<>();
                        root.topic = rootTopic;
                        if (root.topic.getStyleId() != null) {
                            checkStyle(root);
                        } else {
                            root.setDrawableShape(res.getDrawable(R.drawable.round_rect));
                        }

                        root.topic.setFolded(false);

                        boxes.put(root.topic.getId(), root);
                        for (ITopic t : root.topic.getAllChildren()) {
                            Box b = new Box();
                            b.topic = t;
                            boxes.put(root.topic.getId(), root);
                            b.point = new pl.edu.agh.mindmapex.common.Point();
                            if (b.topic.getStyleId() != null) {
                                checkStyle(b);
                            } else {
                                b.setDrawableShape(res.getDrawable(R.drawable.round_rect));
                            }
                            b.parent = root;
                            root.addChild(b);
                            rootTopic.add(b.topic, 0, ITopic.ATTACHED);
                            Utils.fireAddSubtopic(b, boxes);
                            boxes.put(t.getId(), b);
                        }
                        Utils.findRelationships(boxes);
                    } else {
                        if (sheet1.getTheme().getName().equals("%classic")
                                || sheet1.getTheme().getName().equals("%comic")) {
                            root.setPoint(new pl.edu.agh.mindmapex.common.Point(width, height));
                            root.topic = rootTopic;
                            final HashMap<String, Box> boxes = new HashMap<>();
                            if (root.topic.getStyleId() != null) {
                                checkStyle(root);
                            } else {
                                root.setDrawableShape(res.getDrawable(R.drawable.elipse));
                            }
                            root.topic.setFolded(false);
                            boxes.put(root.topic.getId(), root);

                            for (ITopic t : root.topic.getAllChildren()) {
                                Box b = new Box();
                                b.topic = t;
                                b.point = new pl.edu.agh.mindmapex.common.Point();
                                boxes.put(root.topic.getId(), root);
                                if (b.topic.getStyleId() != null) {
                                    checkStyle(b);
                                } else {
                                    b.setDrawableShape(res.getDrawable(R.drawable.round_rect));
                                }
                                b.parent = root;
                                root.addChild(b);
                                Utils.fireAddSubtopic(b, boxes);
                                rootTopic.add(b.topic, 0, ITopic.ATTACHED);
                                boxes.put(t.getId(), b);
                            }
                            Utils.findRelationships(boxes);
                        } else if (sheet1.getTheme().getName().equals("%simple")) {
                            root.setPoint(new pl.edu.agh.mindmapex.common.Point(width, height));
                            final HashMap<String, Box> boxes = new HashMap<>();
                            root.topic = rootTopic;
                            if (root.topic.getStyleId() != null) {
                                checkStyle(root);
                            } else {
                                root.setDrawableShape(res.getDrawable(R.drawable.elipse));
                            }
                            root.topic.setFolded(false);
                            boxes.put(root.topic.getId(), root);

                            for (ITopic t : root.topic.getAllChildren()) {
                                Box b = new Box();
                                b.topic = t;
                                b.point = new pl.edu.agh.mindmapex.common.Point();
                                boxes.put(root.topic.getId(), root);

                                if (b.topic.getStyleId() != null) {
                                    checkStyle(b);
                                } else {
                                    b.setDrawableShape(res.getDrawable(R.drawable.no_border));
                                }
                                b.parent = root;
                                root.addChild(b);
                                rootTopic.add(b.topic, 0, ITopic.ATTACHED);
                                Utils.fireAddSubtopic(b, boxes);
                                boxes.put(t.getId(), b);
                            }
                            Utils.findRelationships(boxes);
                        } else if (sheet1.getTheme().getName().equals("%bussiness")) {
                            root.setPoint(new pl.edu.agh.mindmapex.common.Point(width, height));
                            final HashMap<String, Box> boxes = new HashMap<>();
                            root.topic = rootTopic;
                            if (root.topic.getStyleId() != null) {
                                checkStyle(root);
                            } else {
                                root.setDrawableShape(res.getDrawable(R.drawable.round_rect));
                            }
                            root.topic.setFolded(false);

                            boxes.put(root.topic.getId(), root);

                            for (ITopic t : root.topic.getAllChildren()) {
                                Box b = new Box();
                                b.topic = t;
                                b.point = new pl.edu.agh.mindmapex.common.Point();
                                boxes.put(root.topic.getId(), root);
                                if (b.topic.getStyleId() != null) {
                                    checkStyle(b);
                                } else {
                                    b.setDrawableShape(res.getDrawable(R.drawable.rect));
                                }

                                b.parent = root;
                                root.addChild(b);
                                rootTopic.add(b.topic, 0, ITopic.ATTACHED);
                                Utils.fireAddSubtopic(b, boxes);
                                boxes.put(t.getId(), b);
                            }
                            Utils.findRelationships(boxes);
                        } else if (sheet1.getTheme().getName().equals("%academese")) {
                            root.setPoint(new pl.edu.agh.mindmapex.common.Point(width, height));
                            final HashMap<String, Box> boxes = new HashMap<>();
                            root.topic = rootTopic;
                            if (root.topic.getStyleId() != null) {
                                checkStyle(root);
                            } else {
                                root.setDrawableShape(res.getDrawable(R.drawable.rect));
                            }
                            root.topic.setFolded(false);
                            Style s = (Style) workbook.getStyleSheet().createStyle(IStyle.MAP);
                            s.setProperty(Styles.FillColor,
                                    Integer.toString(res.getColor(R.color.dark_gray), 16));
                            styleSheet.addStyle(s, IStyleSheet.NORMAL_STYLES);
                            sheet1.setStyleId(s.getId());
                            lay.setBackgroundColor(res.getColor(R.color.dark_gray));

                            boxes.put(root.topic.getId(), root);

                            for (ITopic t : root.topic.getAllChildren()) {
                                Box b = new Box();
                                b.topic = t;
                                b.point = new pl.edu.agh.mindmapex.common.Point();
                                boxes.put(root.topic.getId(), root);

                                if (b.topic.getStyleId() != null) {
                                    checkStyle(b);
                                } else {
                                    b.setDrawableShape(res.getDrawable(R.drawable.elipse));
                                }
                                b.parent = root;
                                root.addChild(b);
                                Utils.fireAddSubtopic(b, boxes);
                                rootTopic.add(b.topic, 0, ITopic.ATTACHED);
                                boxes.put(t.getId(), b);
                            }
                            Utils.findRelationships(boxes);
                        }
                    }
                } else if (style.equals("Default")) {

                    rootTopic.setTitleText("Central Topic");
                    root.topic = rootTopic;
                    root.topic.setFolded(false);
                    root.setDrawableShape(res.getDrawable(R.drawable.round_rect));
                    IStyle style3 = styleSheet.createStyle(IStyle.TOPIC);
                    style3.setProperty(Styles.FillColor, "#CCE5FF");
                    style3.setProperty(Styles.ShapeClass, Styles.TOPIC_SHAPE_ROUNDEDRECT);
                    style3.setProperty(Styles.LineClass, Styles.BRANCH_CONN_STRAIGHT);
                    styleSheet.addStyle(style3, IStyleSheet.NORMAL_STYLES);
                    rootTopic.setStyleId(style3.getId());
                } else if (style.equals("Classic")) {
                    rootTopic.setTitleText("Central Topic");
                    root.topic = rootTopic;
                    root.topic.setFolded(false);
                    root.setDrawableShape(res.getDrawable(R.drawable.elipse));
                    IStyle style2 = styleSheet.createStyle(IStyle.THEME);
                    style2.setName("%classic");
                    style2.setProperty(Styles.FillColor, String.valueOf(res.getColor(R.color.light_yellow)));
                    styleSheet.addStyle(style2, IStyleSheet.NORMAL_STYLES);
                    sheet1.setThemeId(style2.getId());
                    IStyle style3 = styleSheet.createStyle(IStyle.TOPIC);
                    style3.setProperty(Styles.FillColor, "#9ACD32");
                    style3.setProperty(Styles.ShapeClass, Styles.TOPIC_SHAPE_ELLIPSE);
                    style3.setProperty(Styles.LineClass, Styles.BRANCH_CONN_CURVE);
                    styleSheet.addStyle(style3, IStyleSheet.NORMAL_STYLES);
                    style2.setProperty(Style.TOPIC, style3.getId());
                    rootTopic.setStyleId(style3.getId());
                } else if (style.equals("Simple")) {
                    rootTopic.setTitleText("Central Topic");
                    root.topic = rootTopic;
                    root.topic.setFolded(false);
                    root.setDrawableShape(res.getDrawable(R.drawable.elipse));
                    IStyle style2 = styleSheet.createStyle(IStyle.THEME);
                    style2.setName("%simple");
                    style2.setProperty(Styles.FillColor, String.valueOf(res.getColor(R.color.white)));
                    styleSheet.addStyle(style2, IStyleSheet.NORMAL_STYLES);
                    sheet1.setThemeId(style2.getId());
                    IStyle style3 = styleSheet.createStyle(IStyle.TOPIC);
                    style3.setProperty(Styles.FillColor, "#FFFFFF");
                    style3.setProperty(Styles.ShapeClass, Styles.TOPIC_SHAPE_ELLIPSE);
                    style3.setProperty(Styles.LineClass, Styles.BRANCH_CONN_CURVE);
                    styleSheet.addStyle(style3, IStyleSheet.NORMAL_STYLES);
                    style2.setProperty(Style.TOPIC, style3.getId());
                    rootTopic.setStyleId(style3.getId());
                } else if (style.equals("Business")) {
                    rootTopic.setTitleText("Central Topic");
                    root.topic = rootTopic;
                    root.topic.setFolded(false);
                    root.setDrawableShape(res.getDrawable(R.drawable.round_rect));
                    IStyle style2 = styleSheet.createStyle(IStyle.THEME);
                    style2.setName("%business");
                    style2.setProperty(Styles.FillColor, String.valueOf(res.getColor(R.color.white)));
                    styleSheet.addStyle(style2, IStyleSheet.NORMAL_STYLES);
                    sheet1.setThemeId(style2.getId());
                    IStyle style3 = styleSheet.createStyle(IStyle.TOPIC);
                    style3.setProperty(Styles.FillColor, "#B87333");
                    style3.setProperty(Styles.ShapeClass, Styles.TOPIC_SHAPE_ROUNDEDRECT);
                    style3.setProperty(Styles.LineClass, Styles.BRANCH_CONN_CURVE);
                    styleSheet.addStyle(style3, IStyleSheet.NORMAL_STYLES);
                    style2.setProperty(Style.TOPIC, style3.getId());
                    rootTopic.setStyleId(style3.getId());
                } else if (style.equals("Academese")) {
                    rootTopic.setTitleText("Central Topic");
                    root.topic = rootTopic;
                    root.topic.setFolded(false);
                    root.setDrawableShape(res.getDrawable(R.drawable.rect));
                    IStyle style2 = styleSheet.createStyle(IStyle.THEME);
                    style2.setProperty(Styles.FillColor, "#404040");
                    styleSheet.addStyle(style2, IStyleSheet.NORMAL_STYLES);
                    sheet1.setStyleId(style2.getId());
                    lay.setBackgroundColor(res.getColor(R.color.dark_gray));
                    IStyle style3 = styleSheet.createStyle(IStyle.TOPIC);
                    style3.setProperty(Styles.FillColor, "#404040");
                    style3.setProperty(Styles.ShapeClass, Styles.TOPIC_SHAPE_RECT);
                    style3.setProperty(Styles.LineClass, Styles.BRANCH_CONN_STRAIGHT);
                    style3.setProperty(Styles.LineColor, "#FFFFFF");
                    styleSheet.addStyle(style3, IStyleSheet.NORMAL_STYLES);
                    style2.setProperty(Style.TOPIC, style3.getId());
                    rootTopic.setStyleId(style3.getId());
                }

            }
        });

    } else {
        setContentView(R.layout.main_content_view);
        lay = (DrawView) findViewById(R.id.main_draw_view);
        lay.setZOrderOnTop(true);
    }
    gestureDetector = new GestureDetector(this, gestList);
    Utils.lay = lay;
    if (lay != null) {
        lay.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                detector.onTouchEvent(event);

                switch (event.getActionMasked()) {
                case (MotionEvent.ACTION_OUTSIDE):
                    return true;
                case (MotionEvent.ACTION_UP):
                    break;
                case MotionEvent.ACTION_POINTER_UP:

                    if (!detector.isInProgress()) {
                        int count = event.getPointerCount(); // Number of 'fingers' in this time

                        Utils.getCoordsInView(lay, event, 1);
                        boxEdited = Utils.whichBox(lay, event);
                        float[] tab1 = Utils.getCoordsInView(lay, event, 0);
                        float[] tab = Utils.getCoordsInView(lay, event, 1);
                        if (count == 2 && boxEdited != null) {

                            if (tab.length == 2) {
                                if (mTourGuide != null)
                                    mTourGuide.cleanUp();
                                Box box1 = new Box();
                                box1.setPoint(new pl.edu.agh.mindmapex.common.Point(
                                        (int) tab[0] - (box1.getWidth() / 2),
                                        (int) tab[1] - (box1.getHeight() / 2)));
                                AddBox addBox = new AddBox();
                                Properties properties = new Properties();
                                properties.put("box", MainActivity.boxEdited);
                                properties.put("new_box", box1);
                                properties.put("root", root);
                                properties.put("res", res);
                                properties.put("style", style);
                                addBox.execute(properties);
                                MainActivity.addCommendUndo(addBox);
                                editContent(box1, addBox);
                                lay.updateBoxWithText(box1);

                            }
                        }

                        break;
                    }
                default:
                    break;
                }

                boolean response = gestureDetector.onTouchEvent(event);
                lay.requestFocus();
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(lay.getApplicationWindowToken(), 0);
                return response;
            }
        });
        lay.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    Utils.context = this;
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayOptions(
            ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.show();

    detector = new ScaleGestureDetector(this, new SimpleOnScaleGestureListener() {
        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            mScaling = true;
            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            //                float focusX = detector.getFocusX();
            //                float focusY = detector.getFocusY();
            ////                lay.setPivotX(mid.x);
            ////                lay.setPivotY(mid.y);
            ////                               lay.pivotx = (int) (lastFocusX  + detector.getFocusX())/2;
            ////               lay.pivoty = (int) (lastFocusY+ detector.getFocusY())/2;
            //          //     lay.pivotx = (int) mid.x;
            //          //     lay.pivoty = (int) mid.y;
            //            //    lay.canvas.translate(-focusX,-focusY);
            lay.pivotx = detector.getFocusX();
            lay.pivoty = detector.getFocusY();
            //                lay.transx = (lay.pivotx);
            //                lay.transy = (lay.pivoty);
            //                lay.pivotx = (int) mid.x;
            //                lay.pivoty = (int) mid.y;
            //                lay.setPivotX(lastFocusX);
            //                lay.setPivotY(lastFocusY);
            float SF = detector.getScaleFactor();
            lay.zoomx *= SF;
            lay.zoomy *= SF;
            //    lay.canvas.scale(SF, SF, mid.x, mid.y);
            //                float focusShiftX = focusX - lastFocusX;
            //                float focusShiftY = focusY - lastFocusY;
            //lay.canvas.translate(focusX + focusShiftX, focusY + focusShiftY);
            //  lastFocusX = focusX;
            // lastFocusY = focusY;
            //  lay.transy = detector.getFocusY();
            // lay.zoomx = Math.max(0.1f, Math.min(lay.zoomx, 5.0f));
            // lay.zoomy = Math.max(0.1f, Math.min(lay.zoomy, 5.0f));
            return true;

        }
    });
    progressDialog.dismiss();
    lay.setId(View.generateViewId());
    lay.setSaveEnabled(true);

    if (savedInstanceState != null) {
        lay.transx = savedInstanceState.getFloat(TRANSX_KEY);
        lay.transy = savedInstanceState.getFloat(TRANSY_KEY);
        lay.zoomx = savedInstanceState.getFloat(ZOOMX_KEY);
        lay.zoomy = savedInstanceState.getFloat(ZOOMY_KEY);
        lay.pivotx = savedInstanceState.getFloat(PIVOTX_KEY);
        lay.pivoty = savedInstanceState.getFloat(PIVOTY_KEY);

    }

}