一个horizo​​ntalscrollview内fingerpaint [英] fingerpaint within a horizontalscrollview

查看:166
本文介绍了一个horizo​​ntalscrollview内fingerpaint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我结合几张code,我已经发现了计算器和Android的开发工具包。我想把fingerpaint画布内可锁定horizo​​ntalscrollview。然而,每当我atempt画在水平方向上的滚动视图滚动,而不是画在画布上。它没有这个问题,当我在从fingerpaint自定义视图中的位置的ImageView的。我认为的onTouchEvent也许在这两个自定义lockableHorizo​​ntalScrollView和自定义drawingView压倒一切可能发生故障。如果需要,我可以提供进一步的细节和code。

从提取物:

drawingView.java

  @覆盖
公共布尔的onTouchEvent(MotionEvent事件){
    浮X = event.getX();
    浮动Y = event.getY();

    开关(event.getAction()){
        案例MotionEvent.ACTION_DOWN:
            touch_start(X,Y​​);
            无效();
            打破;
        案例MotionEvent.ACTION_MOVE:
            TOUCH_MOVE(X,Y);
            无效();
            打破;
        案例MotionEvent.ACTION_UP:
            润色();
            无效();
            打破;
    }
    返回true;
}
 

LockableHorizo​​ntalScrollView.java

  @覆盖
公共布尔的onTouchEvent(MotionEvent EV){
    开关(ev.getAction()){
        案例MotionEvent.ACTION_DOWN:
            //如果我们可以滚动的事件传递给超
            如果(mScrollable)返回super.onTouchEvent(EV);
            //只有不断地处理触摸事件,如果滚动启用
            返回mScrollable; // mScrollable永远是假的,在这一点上
        默认:
            返回super.onTouchEvent(EV);
    }
}
 

解决方案

这是一个结构问题,XML文件以及覆盖错误的方法。所需要的按钮置于horizo​​ntalScrollView之外。相反,lockableHorizo​​ntalScrollView覆盖的onTouchEvent方法,它应该是覆盖onInterceptTouchEvent时,code到如下;

 公共类LockableHorizo​​ntalScrollView扩展Horizo​​ntalScrollView {

公共LockableHorizo​​ntalScrollView(上下文的背景下,AttributeSet中的attrSet)根据{
    超(背景下,attrSet)根据;
}

//如此,如果我们可以滚动(未锁定)
//错误的,如果我们不能滚动(锁定)
私人布尔mScrollable = TRUE;

公共无效setIsScrollable(布尔滚动){
    mScrollable =滚动;
}

公共布尔getIsScrollable(){
    返回mScrollable;
}

@覆盖
公共布尔onInterceptTouchEvent(MotionEvent EV){
    如果(mScrollable)返回super.onTouchEvent(EV);
    否则返回false;
}
 

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

解决方案

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

这篇关于一个horizo​​ntalscrollview内fingerpaint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆