Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent scrolling while drawing inside Scrollview

Tags:

android

I have got a drawing View and a ScrollView with Elements in it. The problem is, that I only could draw lines horizontally, because when I try to draw lines vertically the view is scrolling down because of the ScrollView.

When I delete the ScrollView, also the vertically lines could draw, but I need the ScrollView in order to display all the Elements.

I tried to change the overScrollMode in the ScrollView, but this didn't help.

How to prevent scrolling down the view, while I draw in drawing View?

Here's the corresponding part of xml file:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/openproctScrollview"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#FFFFFF"
    android:fillViewport="true"
    android:overScrollMode="never">

    <FrameLayout

        android:id="@+id/drawingLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.unitnode.Drawing
            android:id="@+id/drawingView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="ifContentScrolls" />
    </FrameLayout>
</ScrollView>

Here is the drawing class:

public class Drawing extends View {
    private static final float TOUCH_TOLERANCE = 4;
    public static String strichFarbeHex;
    private Path mPath;
    private Canvas canvas;
    private Paint mPaint;
    private Paint mBitmapPaint;
    private Bitmap mBitmap;
    private BlurMaskFilter mBlur;
    private MaskFilter mEmboss;
    private float mX, mY;

    public Drawing(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStrokeWidth(12);
        mPaint.setStyle(Paint.Style.STROKE);
        mPath = new Path();

        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        this.setDrawingCacheEnabled(true);

        mEmboss = new EmbossMaskFilter(new float[]{1, 1, 1}, 0.4f, 6, 3.5f);
        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

        this.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                float x = event.getX();
                float y = event.getY();
                Log.d("onTouch", "onTouch");
                Log.d("x", String.valueOf(x));
                Log.d("y", String.valueOf(y));

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.d("a", "down");
                        Log.d("hier", "hier1");
                        touch_start(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.d("onTouch", "onTouch2");
                        touch_move(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.d("onTouch", "onTouch3");
                        touch_up();
                        invalidate();
                        break;
                }
                return true;
            }
        });
    }

    // mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

    public static String setStrichFarbe(String strichFarbe) {

        strichFarbeHex = strichFarbe;
        Log.d("strichFarbeHex", "strichFarbeHex " + strichFarbeHex);
        return strichFarbeHex;
    }

    private void touch_start(float x, float y) {
        // mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = 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;
        }
    }

    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        canvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        // mPaint.setXfermode(new PorterDuffXfermode(Mode.SCREEN));
        // mPaint.setMaskFilter(null);
        // mPath.reset();
    }

    @Override
    public void onDraw(Canvas c) {

        Log.d("onDraw", "onDraw " + mPath + "|" + mPaint);
        long startTime = this.getDrawingTime();
        Log.d("startTime", "startTime " + startTime);
        mPaint.setColor(Color.WHITE);
        c.drawPaint(mPaint);

        // TODO: Ändern der Strichfarbe soll nicht komplett alles ändern, sondern nur den letzten Strich
        if (strichFarbeHex != null) {
            String strichFarbHexCode = getStrichFarbe();
            Log.d("strichFarbHexCode", "strichFarbHexCode " + strichFarbHexCode);
            mPaint.setColor(Color.parseColor(strichFarbHexCode));
        } else {
            mPaint.setColor(Color.parseColor("#CD5C5C"));
        }

        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        c.drawPath(mPath, mPaint);
    }

    @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);
        canvas = new Canvas(mBitmap);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // wenn die Zurücktaste das erste Mal gedrückt wurde
        if (keyCode == KeyEvent.KEYCODE_BACK) {

        }
        return true;
    }

    public String getStrichFarbe() {
        return strichFarbeHex;
    }
}

and this picture shows how it looks like:

enter image description here Thanks for helping

like image 411
Vik0809 Avatar asked Dec 04 '22 05:12

Vik0809


1 Answers

I had the same issue.

Try giving

getParent().requestDisallowInterceptTouchEvent(true);

inside the drawing function.- onDraw(Canvas c)

like image 116
Rohith K N Avatar answered Dec 10 '22 12:12

Rohith K N