2

我尝试为 Android 创建“绘画”。我找到了这篇文章:http: //javastart.pl/przykladowe-programy/piszemy-painta-na-androida/ (不是英语)。

我的代码:Paint.xml:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
    <com.myapp.PaintView
         android:id="@+id/paintView"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="#ffffff" />
    </FrameLayout>

主要.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#ff00ff" >
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:scrollbars="vertical"
            android:id="@+id/scrollview1" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000"
            android:gravity="center|top"
            android:orientation="vertical" >
            <include
                android:layout_width="176dp"
                android:layout_height="250dp"
                android:layout_marginTop="26dp"
                layout="@layout/paint"
                android:background="#ffffff"" />

        </LinearLayout>
 </ScrollView>
</FrameLayout>

PaintView.java:

//copied from article
public class PaintView extends SurfaceView implements SurfaceHolder.Callback {
    ArrayList<RectF> punkty;
    Paint paint = new Paint();

    public PaintView(Context context, AttributeSet attrs) {
        super(context, attrs);
        punkty = new ArrayList<RectF>();
        paint = new Paint();
    }

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
    }

    public void surfaceCreated(SurfaceHolder arg0) {

    }

    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
    }

    public boolean onTouchEvent(MotionEvent event) {

        RectF oval = new RectF(event.getX()-50, event.getY()-50, event.getX() + 50, event.getY() + 50);
        punkty.add(oval);
        invalidate();
        return true;
    }

    protected void onDraw(Canvas canvas) {

        paint.setColor(Color.RED);

        for (RectF punkt : punkty) {
            canvas.drawOval(punkt, paint);
        }

    }
}

问题是当我画一些东西时。当我移动手指来绘制屏幕时,屏幕正在滚动。绘图时如何停止滚动?

4

0 回答 0