0

I am combining some pieces of code which I have found on stackoverflow and in the android development kit. I want to put the fingerpaint canvas within a lockable horizontalscrollview. However whenever I atempt to draw in a horizontal direction the scrollview scrolls rather than painting on the canvas. It did not have this problem when I had an imageview in the place of the custom view from fingerpaint. I think that perhaps the overriding of the onTouchEvent in both the custom lockableHorizontalScrollView and the custom drawingView may be at fault. I can provide further details and code if required.

Extracts from:

drawingView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
    }
    return true;
}

LockableHorizontalScrollView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // if we can scroll pass the event to the superclass
            if (mScrollable) return super.onTouchEvent(ev);
            // only continue to handle the touch event if scrolling enabled
            return mScrollable; // mScrollable is always false at this point
        default:
            return super.onTouchEvent(ev);
    }
}

2 Answers2

1

This was a arrangement problem with the xml file as well as overriding the wrong method. The button needed to be placed outside of the horizontalScrollView. Instead of the lockableHorizontalScrollView overriding the onTouchEvent method it should have been overriding the onInterceptTouchEvent, the code to which follows;

public class LockableHorizontalScrollView extends HorizontalScrollView{

public LockableHorizontalScrollView(Context context, AttributeSet attrset) {
    super(context, attrset);
}

// true if we can scroll (not locked)
// false if we cannot scroll (locked)
private boolean mScrollable = true;

public void setIsScrollable(boolean scrollable) {
    mScrollable = scrollable;
}

public boolean getIsScrollable() {
    return mScrollable;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (mScrollable) return super.onTouchEvent(ev);
    else return false;
}
0

I don't understand how you want the scroll behaviour to work: specifically, how the system is meant to tell the difference between a scroll gesture and a paint gesture.

However, you can find out how to programatically enable and disable scrolling by reading this previous question:

Disable ScrollView action

Community
  • 1
  • 1
Jodes
  • 14,118
  • 26
  • 97
  • 156
  • The source which you have included does partially seem to help, but now the paint component will only paint small sections in a horizontal direction before it gives up. I guess there are other gestures to be considered. The scroll should be locked whilst I am painting, then when I click a button, the scrolling behaviour should be reinstated. – Steven Cooke May 22 '11 at 20:09
  • Looks like I might have to override an onInterceptTouchEvent as well. I will try to get my head around it. Here is a link I found; http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling – Steven Cooke May 23 '11 at 22:36