ch mohansai
ch mohansai

Reputation: 19

How to create a point on touch of a particular position of a image in android

I can able to create a point in image but when I give the coordinates as touchx,touchy in canvas.drawCircle(touchX,touchY, 2, paint) I am unable to create circle dot on the place where I've touched and also this functionality should work on click of image only but its creating point when i touch outside of image also and when I touch the screen the image is zooming...I wnat Image which should not be zoomed and should create point on touch of image only..Kindly Help...

imageView.setOnTouchListener(new View.OnTouchListener() {                    
 @Override
public boolean onTouch(View v, MotionEvent event) {               
                touchX = (int) (event.getX());
                touchY = (int) (event.getY());                
                ImageView imageView = (ImageView) findViewById(R.id.imageView2);
                Bitmap bitMap = Bitmap.createBitmap(100,100, Bitmap.Config.ARGB_8888);
                bitMap = bitMap.copy(bitMap.getConfig(), true);     
                Canvas canvas = new Canvas(bitMap);                
                Paint paint = new Paint();                       
                paint.setColor(Color.BLUE);
                paint.setStyle(Paint.Style.FILL);
                paint.setAntiAlias(true);
                imageView.setImageBitmap(bitMap);      
                imageView.setBackgroundResource(R.drawable.image_map);
                canvas.drawCircle(50, 60, 2, paint);
                imageView.invalidate();
                return true;
            }
        });

Upvotes: 1

Views: 2083

Answers (1)

Piyush Malaviya
Piyush Malaviya

Reputation: 1091

        ivImage = (ImageView) findViewById(R.id.ivImage);
        final Bitmap bitmap = ((BitmapDrawable)ivImage.getDrawable()).getBitmap().copy(Config.ARGB_8888, true);
        ivImage.setOnTouchListener(new OnTouchListener() {

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

                int touchX = (int)(event.getX());
                int touchY = (int)(event.getY());
                Canvas canvas = new Canvas(bitmap);
                Paint paint = new Paint();
                paint.setColor(Color.GREEN);
                canvas.drawCircle(touchX, touchY, 2, paint);    // for circle dot
                //canvas.drawPoint(touchX, touchY, paint);  // for single point
                ivImage.setImageBitmap(bitmap);
                ivImage.invalidate();
                return true;
            }
        });

Upvotes: 2

Related Questions