使用ScrollView绘制区域

时间:2015-07-30 07:37:59

标签: java android android-activity draw

我在我的Activity中创建了一个绘画和绘图区域,我想用scrollView移动它。

如果我有这个代码,我没有任何问题:

<RelativeLayout
    android:id="@+id/frameVer"
    android:layout_width="match_parent"
    android:layout_height="2000dip"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1"
    android:background="@null">

    <package.DrawingView
        android:id="@+id/drawing"
        android:layout_width="fill_parent"
        android:layout_height="2000dip"
        android:background="@null" />

</RelativeLayout>

但是,如果我添加ScrollView,它会分成几部分:

<package.LinkableScrollView
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="2000dip"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1"
    android:background="@null">

        <RelativeLayout
            android:id="@+id/frameVer"
            android:layout_width="match_parent"
            android:layout_height="2000dip"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="3dp"
            android:layout_weight="1"
            android:background="@null">

            <package.DrawingView
                android:id="@+id/drawing"
                android:layout_width="fill_parent"
                android:layout_height="2000dip"
                android:background="@null" />

        </RelativeLayout>
</package.LinkableScrollView>

enter image description here

如果我在纵向模式下绘画,它可以让我画画,但是在片段中,如果我在风景中画画,我可以画得很完美。

我使用启用或禁用属性创建了自己的ScrollView,但它以相同的方式绘制。

public class LinkableScrollView extends ScrollView {

    private static final int NUMBER_OF_FINGERS_TO_ACTIVATE_SCROLL = 2;
    private boolean doubleDrag = false;
    private boolean enableScrolling = true;

    public LinkableScrollView(Context context, AttributeSet attrs, int defStyle)       {
        super(context, attrs, defStyle);
    }

    public LinkableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LinkableScrollView(Context context) {
       super(context);
    }

    private int getScrollHeight() {
        return super.getChildAt(0).getHeight() - super.getHeight();
    }

    public boolean isEnableScrolling() {
       return enableScrolling;
    }

    public void setEnableScrolling(boolean enableScrolling) {
       this.enableScrolling = enableScrolling;
    }



     @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {

        if (isEnableScrolling()) {
            int action = event.getAction();

            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    super.onTouchEvent(event);
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_UP:
                    super.onTouchEvent(event);
                   doubleDrag = false;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (event.getPointerCount() == NUMBER_OF_FINGERS_TO_ACTIVATE_SCROLL) {
                        doubleDrag = true;
                    }
                    if (doubleDrag) {
                       super.onTouchEvent(event);
                    }
            }
            return true;
        }
        else {
            return false;
        }


    }
}

绘制触摸事件:

@Override
public boolean onTouchEvent(MotionEvent event) {

    Point p = new Point();
    float touchX = event.getX();
    float touchY = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            drawPath.moveTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_MOVE:
            drawPath.lineTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_UP:
            drawCanvas.drawPath(drawPath, drawPaint);
            drawPath.reset();
            break;
        default:
            return false;
    }

    invalidate();
    return true;
}

拜托,有人可以帮帮我吗?谢谢!

0 个答案:

没有答案