2

I want to have a UI which draws some graphics items and users can scroll and scale the graphics scene with gestures(like Google maps).I have used graphics framework in Qt but I can't find anything like it in Android.

I fond Dragging and Scaling and sample InteractiveChart in Developer, but it seems complicated.I tried to realize by mysely.Here is some snippet.I use ScaleGestureDetector and GestureDetector to change the parameter of mContent,the mContent is a FrameLayout.It's functional but have some bugs.I want some advice, thanks!!

private final ScaleGestureDetector.OnScaleGestureListener mScaleGestureListener
        = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        float scaleFactor = detector.getScaleFactor();//This value is defined as (getCurrentSpan() / getPreviousSpan())
        float factor = scaleFactor / lastScaleFactor;
        Log.d(TAG, "factor+" + factor);
        lastScaleFactor=scaleFactor;
        scale*= factor;
        mContent.setScaleX(scale);
        mContent.setScaleY(scale);

        return super.onScale(detector);
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        mContent.setPivotX(detector.getFocusX());
        mContent.setPivotY(detector.getFocusY());
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {
        lastScaleFactor=1;
    }
};

private final GestureDetector.SimpleOnGestureListener mGestureListener
        = new GestureDetector.SimpleOnGestureListener() {
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        mContent.scrollBy((int)distanceX,(int)distanceY);
        return true;
    }
};
StephenLau
  • 46
  • 4

0 Answers0