0

Desired Outcome

I want to draw a red circle at the position the screen is touched then remove it when the touch is released.

Current Outcome

Whenever I touch the screen the red circle appears in the touched location, however when I release the touch the circle stays on the screen.

Extra Help

I am pretty new to coding for android and if a quick gloss over my code shows that I'm using anything unnecessary I would love to be told so I can improve.

My Code

package com.mr.mwood.thumbinput;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity extends Activity {

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

    class SampleView extends SurfaceView {

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

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

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            Canvas canvas = surfaceHolder.lockCanvas();
            switch (event.getActionMasked()) {
            case (MotionEvent.ACTION_DOWN):
                canvas.drawColor(Color.BLACK);
                canvas.drawCircle(event.getX(), event.getY(), 50, paint);  
                break;
            case (MotionEvent.ACTION_UP):
                canvas.drawColor(Color.BLACK);
                break;
            }
            surfaceHolder.unlockCanvasAndPost(canvas);
            return false;
        }
    }
}
user1041528
  • 25
  • 2
  • 6
  • you need to `surfaceHolder.unlockCanvasAndPost(canvas);` after you drawColor(BLACK); (just move it outside the switch – njzk2 Aug 29 '13 at 09:47
  • I modified the code and update the code in the original post but this has not helped @njzk2 – user1041528 Aug 29 '13 at 09:58

0 Answers0