-1

Recently I fixed an error, but now I'm stuck at clearing the canvas for my draw project. I already took a look at some other topics with the same question, but it didn't fix my problem.

package org.arkmap.klaas.testdrawing;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class WriteOnScreenActivity extends AppCompatActivity {
    Button clear;
    TouchEventView myView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_on_screen);
        clear=(Button)findViewById(R.id.clearbutton);
        myView = new TouchEventView(this, null);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View vw) {
                myView.clearCanvas();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_write_on_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

WriteOnScreenActivity.java

package org.arkmap.klaas.testdrawing;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.Random;
/**
 * Created by Startklaar on 19-1-2016.
 */
public class TouchEventView extends View {
    public Paint paint = new Paint();
    private Path path = new Path();
    public boolean cc = false;

    Random rnd = new Random();

    public TouchEventView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        paint.setAntiAlias(true);
        paint.setARGB(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        //paint.setColor(Color.WHITE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (cc) {
            path = new Path();
            Paint clearPaint = new Paint();
            clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            canvas.drawRect(0, 0, 0, 0, clearPaint);
            cc = false;
        }
        canvas.drawPath(path, paint);
    }

    public void clearCanvas()
    {
        cc = true;
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float xPos = event.getX();
        float yPos = event.getY();

        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(xPos, yPos);
                return true;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(xPos,yPos);
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                return false;
        }

        invalidate();
        return true;
    }
}

TouchEventView.java

Hope to get some help here!

EDIT: The error has been fixed, the only problem I have is that the button is not functioning.

Laurel
  • 5,965
  • 14
  • 31
  • 57
klaasa
  • 17
  • 7

1 Answers1

1

myView is not being initialized. You should set it:

 myView = (TouchEventView) findViewById(R.id.XXXX); 
JpCrow
  • 4,881
  • 4
  • 32
  • 46
  • I fixed the error with myView = new TouchEventView(this, null);. Now when I press the button nothing happens. Am I missing something? – klaasa Jan 22 '16 at 15:35
  • Yes , put the view in your XML layout and give it an id. – Luca Ziegler Jan 22 '16 at 15:37
  • I am very new to android and don't know how to add it to the XML and give it an id. Could you elaborate this? – klaasa Jan 22 '16 at 15:43
  • You should read more about android, maybe this can help u http://developer.android.com/intl/es/training/index.html – JpCrow Jan 22 '16 at 15:46
  • Fixed it, already added it to the XML but tought you meant adding myView to the XML :) – klaasa Jan 22 '16 at 16:26