在水平滚动视图中进行手指绘画

发布于 2024-11-09 00:38:52 字数 1367 浏览 0 评论 0原文

我正在组合我在 stackoverflow 和 android 开发工具包中找到的一些代码。我想将手指画画布放在可锁定的水平滚动视图中。然而,每当我尝试在水平方向上绘制时,滚动视图就会滚动而不是在画布上绘制。当我用 imageview 代替 Fingerpaint 的自定义视图时,它没有这个问题。我认为自定义 lockableHorizo​​ntalScrollView 和自定义 DrawingView 中的 onTouchEvent 的重写可能有问题。如果需要,我可以提供更多详细信息和代码。

摘自:

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;
}

LockableHorizo​​ntalScrollView.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);
    }
}

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒儿 2024-11-16 00:38:52

这是 xml 文件的排列问题以及覆盖错误的方法。该按钮需要放置在horizo​​ntalScrollView 之外。 lockableHorizo​​ntalScrollView 不应该重写 onTouchEvent 方法,而应该重写 onInterceptTouchEvent,代码如下;

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;
}

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;
}
小瓶盖 2024-11-16 00:38:52

我不明白您希望滚动行为如何工作:具体来说,系统如何区分滚动手势和绘画手势之间的区别。

但是,您可以通过阅读上一个问题来了解如何以编程方式启用和禁用滚动:

禁用 ScrollView 操作

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文