10

I am trying to draw a circle where user touches screen .

public class MainActivity extends Activity implements OnTouchListener {

LinearLayout layout;
float x=0;
float y=0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);
    layout=(LinearLayout)findViewById(R.id.layout);
    layout.addView(new CustomView(MainActivity.this));
}

public class CustomView extends View {

    Bitmap mBitmap;
    Paint paint;
    public CustomView(Context context) {
        super(context);
        mBitmap = Bitmap.createBitmap(400, 800, Bitmap.Config.ARGB_8888);
        paint=new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.setBitmap(mBitmap);
        canvas.drawCircle(x, y, 50, paint);
        Toast.makeText(MainActivity.this, "DDD", 1).show();
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
    layout.invalidate();

    }
    return false;
}}
Code_Life
  • 5,742
  • 4
  • 29
  • 49

2 Answers2

15

I would do it this way:

public class TestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new DrawingView(this));
    }

    class DrawingView extends SurfaceView {

        private final SurfaceHolder surfaceHolder;
        private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        public DrawingView(Context context) {
            super(context);
            surfaceHolder = getHolder();
            paint.setColor(Color.RED);
            paint.setStyle(Style.FILL);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                if (surfaceHolder.getSurface().isValid()) {
                    Canvas canvas = surfaceHolder.lockCanvas();
                    canvas.drawColor(Color.BLACK);
                    canvas.drawCircle(event.getX(), event.getY(), 50, paint);
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
            return false;
        }
    }
}
sdabet
  • 18,360
  • 11
  • 89
  • 158
  • 2) When doing active drawing like this, it's better to use a SurfaceView – sdabet Aug 03 '12 at 13:34
  • 3) You don't assign the OnTouchListener to anything...so the `onTouch()`method is not called – sdabet Aug 03 '12 at 13:38
  • @chx101 I don't understand your point, sorry, but feel free to provide a better answer if you have one. – sdabet Oct 16 '15 at 13:14
  • @fiddler First I would extend `View` and override `onDraw` and `onTouch` then return `true` to signal the touch as been handled. I'd then call `invalidate()` from the `onTouch` method. That will allow you to draw other items in the `onDraw` method since it will be called by other events as well. – TheRealChx101 Oct 16 '15 at 16:05
  • @chx101 That sounds like a nice improvement, you should post it. Not sure my answer deserved a downvote though (as a reminder, a downvote means "This answer is not useful"). – sdabet Oct 16 '15 at 16:19
  • lock canvas returns null – Cyph3rCod3r Nov 02 '18 at 06:12
  • 1
    On every touch is it cleans the screen and add a new circle. I thought it'll keep adding circles. – Ashutosh Sagar May 13 '20 at 14:59
  • What if I want to draw on the original activity? – Beyondo Oct 18 '21 at 06:16
7

The is your version with fixes to make it work:

public class MainActivity extends Activity {

LinearLayout layout;
float x = 0;
float y = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    layout=(LinearLayout)findViewById(R.id.layout);
    layout.addView(new CustomView(MainActivity.this));
}

public class CustomView extends View {

    Bitmap mBitmap;
    Paint paint;

    public CustomView(Context context) {
        super(context);
        mBitmap = Bitmap.createBitmap(400, 800, Bitmap.Config.ARGB_8888);
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(x, y, 50, paint);
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            x = event.getX();
            y = event.getY();
            invalidate();
        }
        return false;
    }
}

}
sdabet
  • 18,360
  • 11
  • 89
  • 158