ScrollView事件覆盖DrawView事件

时间:2014-07-11 14:01:03

标签: android events scroll ontouchevent overwrite

我在scrollView上有一个drawView。现在,当你不滚动,写,没有问题。一切都很好看。 但是,滚动后你可以垂直绘制,但是一旦你画完,它就会将你的Y坐标设置为ScrollView的坐标(我认为)

我怀疑scrolllevent正在覆盖onTouch事件。但我不知道如何修复它。

例如

07-11 13:17:46.903  25508-25508/ V/﹕ Y - 8859.633
07-11 13:17:46.913  25508-25508/ V/﹕ Y - 8856.579
07-11 13:17:46.913  25508-25508/ V/﹕ Y - 8856.579
07-11 13:17:46.933  25508-25508/ V/﹕ Y - 8851.542
07-11 13:17:46.933  25508-25508/ V/﹕ Y - 8851.542
07-11 13:17:46.953  25508-25508/ V/﹕ Y - 8847.878
07-11 13:17:46.973  25508-25508/ V/﹕ Y - 544.6034 (as soon as I paint vertically up or down)

下面的图片说明了会发生什么。黄色荧光笔将是我的笔/手指,蓝色是实际绘图。

不向下滚动时

enter image description here

当我向上或向下滚动时向下滚动

enter image description here

代码

public class MainActivity extends Activity {
    DrawingView dv ;
    private Paint mPaint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       dv = new DrawingView(this);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);

        ScrollView mScrollView = new ScrollView(this);
        mScrollView.addView(dv);
        setContentView(mScrollView);

        mScrollView.setOnTouchListener( new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent m)
            {
                if(m.getTouchMajor() == 0.0f) {
                    Log.v("xxxx","Scroll Touch Pen");
                    dv.onTouchEvent(m);  <-- I know I am not passing the correct MotionEvent, but I have NO idea how to get the right one. At least this way I'm a "bit" closer to solving it as I can write horizontally as well without scrolling.
                    return true;
                }
                else
                {
                    Log.v("xxxx","Scroll Touch Fingure");

                    return false;
                }
            }


        });
    }

和我的DrawinngView

 public class DrawingView extends View {

        public int width;
        public  int height;
        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;
        Context context;
        private Paint circlePaint;
        private Path circlePath;
        Rect clipBounds_canvas;

        public DrawingView(Context c) {
            super(c);
            context=c;
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
            circlePaint = new Paint();
            circlePath = new Path();
            circlePaint.setAntiAlias(true);
            circlePaint.setColor(Color.BLUE);
            circlePaint.setStyle(Paint.Style.STROKE);
            circlePaint.setStrokeJoin(Paint.Join.MITER);
            circlePaint.setStrokeWidth(4f);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);

            mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);

        }
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            clipBounds_canvas = canvas.getClipBounds();
            canvas.drawBitmap( mBitmap, 0, 0, mBitmapPaint);

            canvas.drawPath( mPath,  mPaint);

            canvas.drawPath( circlePath,  circlePaint);
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // Compute the height required to render the view
            // Assume Width will always be MATCH_PARENT.
            int width = MeasureSpec.getSize(widthMeasureSpec);

            int height = 10000; 
            setMeasuredDimension(width, height);
        }
        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;


        private void touch_start(float x, float y) {
        //    mPath.reset();

            mPath.moveTo(x, y);
            mX = x;
            mY = y;
            Log.v("xxxx","Y - Start - " + y);

        }
        private void touch_move(float x, float y) {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                mX = x;
                mY = y;

                Log.v("xxxx","Y - Move - " + y);

                circlePath.reset();
                circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
            }
        }
        private void touch_up() {
            mPath.lineTo(mX, mY);
            circlePath.reset();
            // commit the path to our offscreen
            mCanvas.drawPath(mPath,  mPaint);
        }

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

            Log.v("xxxx","Y - " + y);

            if(event.getTouchMajor() == 0.0f) {
                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;

//                handleTouch(event);
//                return true;
            }
            else
            {
                return false;
            }
        }
    }

你是否能够看到我如何防止滚动视图覆盖我的动作事件(或者如果可能的话,至少有两个独立的动作事件。

1 个答案:

答案 0 :(得分:2)

找到了一个很棒的解决方案here, that worked perfectly

只需使用此方法

public static void disableTouchTheft(View view) {
    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            view.getParent().requestDisallowInterceptTouchEvent(true);
            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                view.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
            return false;
        }
    });
}