Search code examples
androidcanvaspaint

Draw a circle(dot) on an imageview


I'm trying to write a simple script that, when someone touches a map of the United States, puts a dot on the screen at the (x,y) coordinate.

Code is below.

public class map extends AppCompatActivity {

    ImageView ivMap_ActivityMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        ivMap_ActivityMap = (ImageView)findViewById(R.id.ivMap_ActivityMap);
        ivMap_ActivityMap.setImageResource(R.drawable.usa);
        ivMap_ActivityMap.setOnTouchListener(handleTouch);

    }

    private final View.OnTouchListener handleTouch = new View.OnTouchListener() {

        @SuppressLint("ResourceAsColor")

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

            int x = (int) event.getX();
            int y = (int) event.getY();
            Toast.makeText(map.this, "x = " + x + ", y = " + y, Toast.LENGTH_LONG).show();

            Paint paint = new Paint();
            int radius;
            radius = 100;
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(R.color.colorPrimaryDark);
            Canvas canvas = new Canvas();
            canvas.drawCircle(x, y, radius, paint);

            return true;
            }
        };
    }

Solution

  • Your code didn't work because you were drawing in an orphan Canvas.

    You can achieve your goal by making a canvas with an empty bitmap of the ImageView size and after that draw circle and finally set the drawable to the ImageView in the following way,

    public class map extends AppCompatActivity {
    
        ImageView ivMap_ActivityMap;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_map);
    
            ivMap_ActivityMap = (ImageView)findViewById(R.id.ivMap_ActivityMap);
            // Set the map as background  
            ivMap_ActivityMap.setBackgroundResource(R.drawable.usa);
            ivMap_ActivityMap.setOnTouchListener(handleTouch);
    }
    
    private final View.OnTouchListener handleTouch = new View.OnTouchListener() {
        // Stored to draw multiple circle. 
        // If you want to draw only one circle then you can make it a local variable
        private Bitmap bmp;
    
        @SuppressLint("ResourceAsColor")
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
    
            int x = (int) event.getX();
            int y = (int) event.getY();
            Toast.makeText(map.this, "x = " + x + ", y = " + y, Toast.LENGTH_LONG).show();
    
            Paint paint = new Paint();
            int radius;
            radius = 100;
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(R.color.colorPrimaryDark);
            // Create a bitmap object of your view size
            if(bmp==null) bmp = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
                   // Create the canvas with the bitmap
            Canvas canvas = new Canvas(bmp);
            canvas.drawCircle(x, y, radius, paint);
            // Set the bitmap to the imageView
            ImageView iv = (ImageView) v;
                   iv.setImageBitmap(bmp);
            return true;
            }
        };
    }