How to update Android ShapeDrawable bounds changes after implementing Scroller with GestureDetector?
Asked Answered
G

1

0

What I have Tried:

I'm drawing shapeDrawables in onDraw(Canvas canvas) with specified the bounds.

Added Handle Input Gestures which allow the view to scroll in any direction like 2d scroll view like mentioned in this SO post.

Problem:

when I scroll the view by onScroll() GestureDetector shapeDrawable's bound changes ! is there any method to change/update the already displayed shapeDrawable bounds when scrolled ??

I have implemented onTouchEvent(MotionEvent event)such that if i click on Shape drawables displayed on the canvas/view if the clicked x,y contains the bound of shapedrawable then delete the drawable. this thing works fine when view is not scrolled, if scroll the view bound fails to detect shapedrawable becouse of the change in bound and x, y parameters.

Code:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.drawable.shapes.Shape;
import android.os.Build;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;

import java.util.*;

public class ShapeDrawableView extends View {
    private List<ShapeDrawable> shapes = new ArrayList<ShapeDrawable>();
    private Integer[] mColors = { Color.BLACK, Color.BLUE, Color.GREEN,
            Color.RED };

    // If made programmatically and added with setContentView

    public ShapeDrawableView(Context context) {
        super(context);
    }

    private Scroller mScroller;
    private GestureDetector mDetector;

    public ShapeDrawableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mDetector = new GestureDetector(ShapeDrawableView.this.getContext(),
                new GestureListener());
        if (Build.VERSION.SDK_INT < 11) {
            mScroller = new Scroller(getContext());
        } else {
            mScroller = new Scroller(getContext(), null, true);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (ShapeDrawable shape : shapes) {
            shape.draw(canvas);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        mDetector.onTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX(); // Use getX(int) for multi-finger
                                        // gestures
            int y = (int) event.getY();
            if (!isDeletingExistingShape(x, y)) {
                shapes.add(makeShapeDrawable(x, y));
            }
            invalidate();
            return (true); // Handled touch event
        } else {
            return (false); // Did not handle touch event
        }
    }

    private boolean isDeletingExistingShape(int x, int y) {
        for (ShapeDrawable shape : shapes) {
            Rect bounds = shape.getBounds();
            if (bounds.contains(x, y)) {
                shapes.remove(shape);
                return (true);
            }
        }
        return (false);
    }

    private ShapeDrawable makeShapeDrawable(int x, int y) {
        int maxWidth = getWidth() / 10;
        int maxHeight = getHeight() / 10;
        Shape shape;
        if (Math.random() < 0.5) {
            shape = new OvalShape();
        } else {
            shape = new RectShape();
        }
        ShapeDrawable shapeD = new ShapeDrawable(shape);
        int width = RandomUtils.randomInt(maxWidth) + 5;
        int height = RandomUtils.randomInt(maxHeight) + 5;
        shapeD.setBounds(x - width / 2, y - height / 2, x + width / 2, y
                + height / 2);
        shapeD.getPaint().setColor(RandomUtils.randomElement(mColors));

        return (shapeD);
    }

    private class GestureListener extends
            GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            // Set the pie rotation directly.
            // float scrollTheta = vectorToScalarScroll(
            // distanceX,
            // distanceY,
            // e2.getX() - mPieBounds.centerX(),
            // e2.getY() - mPieBounds.centerY());
            // setPieRotation(getPieRotation() - (int) scrollTheta /
            // FLING_VELOCITY_DOWNSCALE);

            scrollBy((int) distanceX, (int) distanceY);

            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            // Set up the Scroller for a fling
            /*
             * float scrollTheta = vectorToScalarScroll( velocityX, velocityY,
             * e2.getX() - mPieBounds.centerX(), e2.getY() -
             * mPieBounds.centerY()); mScroller.fling( 0, (int)
             * getPieRotation(), 0, (int) scrollTheta /
             * FLING_VELOCITY_DOWNSCALE, 0, 0, Integer.MIN_VALUE,
             * Integer.MAX_VALUE);
             * 
             * // Start the animator and tell it to animate for the expected
             * duration of the fling. if (Build.VERSION.SDK_INT >= 11) {
             * mScrollAnimator.setDuration(mScroller.getDuration());
             * mScrollAnimator.start(); }
             */

            mScroller.fling(getScrollX(), getScrollY(), -(int) velocityX,
                    -(int) velocityY, 0, (int) 100, 0, (int) 100);
            invalidate(); // don't remember if it's needed
            return true;

            // return true;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            // The user is interacting with the pie, so we want to turn on
            // acceleration
            // so that the interaction is smooth.
            /*
             * mPieView.accelerate(); if (isAnimationRunning()) {
             * stopScrolling(); }
             */

            if (!mScroller.isFinished()) { // is flinging
                mScroller.forceFinished(true); // to stop flinging on touch
            }
            return true; // else won't work
            // return true;
        }
    }

}

Gist

Geomancy answered 15/10, 2014 at 7:15 Comment(3)
just update x and y in onTouchEvent: x += getScrollX(); y += getScrollY();Circumjacent
@Circumjacent please paste that answer i will accept your answer :) your single line comment is the right answer(life saver) for me!Geomancy
@Circumjacent can you help me on this #26543474 failed to take the bitmap of view which is out of screen width and height becz of scroller implementationGeomancy
C
1

just update x and y in onTouchEvent:

x += getScrollX();
y += getScrollY();
Circumjacent answered 15/10, 2014 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.