0

i'm pretty much a new in android development. I try to draw line based on touch event. It work well with out scrollview. But, when I use scrollView, it just doesn't work. I read some question and answer in other thread, but nothing can get me understand how should the code.

here is my code:

public class CustomDrawingView extends View 
{
    private final int paintColor = Color.BLACK;
    private Paint drawPaint;
    private Path path = new Path();
    private boolean clearCanvas = false;

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

private void setupPaint() {
    drawPaint = new Paint();
    drawPaint.setColor(paintColor);
    drawPaint.setAntiAlias(true);
    drawPaint.setStrokeWidth(5);
    drawPaint.setStyle(Paint.Style.STROKE);
    drawPaint.setStrokeJoin(Paint.Join.ROUND);
    drawPaint.setStrokeCap(Paint.Cap.ROUND);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    float touchX = event.getX();
    float touchY = event.getY();
    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(touchX, touchY);
            break;
    }
    postInvalidate();
    return true;
}
@Override
protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, drawPaint);
}
public void clearScreen()
{
    path.reset();
    postInvalidate();
}

}

MainActifity:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mirana.touchinteraction.MainActivity">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <com.example.mirana.touchinteraction.CustomDrawingView
            android:id="@+id/simpleDrawingView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
    </ScrollView>

</RelativeLayout>

how to able to draw line in the screen based on the touch Interaction? Please help me.

on the image below, the screen contain "work draw" is what I want my code run (remove scrollView from xml). The blank screen is the result when I add scrollview to the xml just like in the code above. I can draw anything by touch and drag my pointer to the screen.

"working" screen and "not working" screen captures

0 Answers0