如何在滚动视图内绘制时防止滚动

我有一个绘图视图和一个带有元素的ScrollView 。 问题是,我只能水平绘制线条,因为当我尝试垂直绘制线条时,由于ScrollView. ,视图会向下ScrollView.

当我删除ScrollView,垂直线也可以绘制,但我需要ScrollView才能显示所有元素。

我试图改变ScrollView,overScrollMode ScrollView,但这没有帮助。

如何防止向下滚动视图,而我在绘制视图中绘制?

这里是xml文件的相应部分:

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

这是绘图类:

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

这张图片显示了它的样子:

在这里输入图像描述 感谢您的帮助


我遇到过同样的问题。

尝试给予

getParent().requestDisallowInterceptTouchEvent(true);

在绘图函数内.- onDraw(Canvas c)

链接地址: http://www.djcxy.com/p/63697.html

上一篇: How to prevent scrolling while drawing inside Scrollview

下一篇: ListView inside ScrollView is not scrolling on Android