Search code examples
javaandroidbitmapdrawgesture

Android: Drawing a circle on bitmap


I'm trying to make an app that draws a circle on a bitmap. Right now, I have a button that produces the circle. Instead, I would like to draw the circle where the user double-taps (instead of pressing a button). How can I do this programmatically? Here is the content of the activity so far:

public static final String KEY_PATH = "img.jpg";
private ZoomInZoomOut touch;
private Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_display);
    Intent intent = getIntent();
    String path = getIntent().getStringExtra(ImageDisplayActivity.KEY_PATH);
    try {
        java.io.FileInputStream in = this.openFileInput(path);
         bitmap = BitmapFactory.decodeStream(in);

        bitmap = bitmap.copy(bitmap.getConfig(), true);
        touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
        touch = arrangeImageView(touch);
        touch.setImageBitmap(bitmap);
        in.close();
        Button draw = (Button) findViewById(R.id.draw);
        draw.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(),
                        bitmap.getHeight(),
                        bitmap.getConfig());
                Canvas canvas = new Canvas(bmOverlay);
                Paint p = new Paint();
                p.setAntiAlias(true);
                p.setColor(Color.BLUE);
                p.setStrokeWidth(2);
                p.setStyle(Paint.Style.STROKE);
                canvas.drawBitmap(bitmap,new Matrix(),null);
                canvas.drawCircle(1000, 1000, 20, p);
               touch.setImageBitmap(bmOverlay);
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Solution

  • You have to implement GestureDetector and put your code in single/double click. Here you can replace button with bitmap.

    TestActivity.java

    iv.setOnClickListener(new OnClickListener() {           
                    @Override
                    public void onClick(View v) {
                        //putyour first activity call.
                    }
        }
    
    iv.setOnTouchListener(new OnTouchListener() {
                 GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector(context));
             @Override
             public boolean onTouch(View v, MotionEvent event) {
                    return gestureDetector.onTouchEvent(event);
             }
    });
    

    Now you have to create GestureDetector.java class.

    public class MyGestureDetector extends SimpleOnGestureListener {
    public Context context;
    public String phno;
    
    public MyGestureDetector(Context con)
    {
        this.context=con;       
    }
    @Override
    public boolean onDown(MotionEvent e) {
        return super.onDown(e);
    }
    public MyGestureDetector(Context con) {
        this.context=con;
    }
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        System.out.println("in Double tap");
    
        return true;
    }
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        System.out.println("in single tap up");
                //put your second activity.
        return super.onSingleTapUp(e);
    }   
    }