Search code examples
androidcanvascoordinatesdrawandroid-custom-view

Draw Bitmap in Custom ImageView and get the Co-ordinates of the ImageView irrespective of the device


I want to get the co-ordinates of the ImageView, Irrespective of device size.

Is there any possible way !!

I have tried to create specific size for the ImageView,Even specific size for the Parent View also ,but its not working.

I have tried the following possible ways.

int[] posXY = new int[2];
imageview.getLocationOnScreen(posXY);
int MoveX = posXY[0];
int MoveY = posXY[1];

I have tried with Matrix too ,But not working.

Matrix m = imageview.getImageMatrix();

Tried the below code, but it is also not working.!!

I need to get the same {x,y} Co-ordinates for all devices for the same point (Location).

final float[] getPointerCoords(ImageView view, MotionEvent e)
    {
        final int index = e.getActionIndex();
        final float[] coords = new float[] { 
                e.getX(index), e.getY(index) 
                };
        Matrix matrix = new Matrix();
        view.getImageMatrix().invert(matrix);

        matrix.mapPoints(coords);

        return coords;
    }

Here is draw Method :

if i set bitmap image in draw, it does not fit the screen for every devices. If i set image with Display width and height, i am getting different co-ordinates.

 @Override
    public void draw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.draw(canvas);

        Resources res = getResources();
        Drawable drawable = res.getDrawable(R.drawable.subimage);
        mBitmap = ((BitmapDrawable) drawable).getBitmap();     
        canvas.drawBitmap(mBitmap, 0, 0, null);

    }

Any Idea or help would be really helpful.


Solution

  • Finally found some relative solution,Same Co-ordinates for all devices.Using Activity,no Custom ImageView.

    On ImageView OnTouch ..

     public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
          final int index = e.getActionIndex();
          final float[] coords = new float[] {
                  e.getX(index), e.getY(index)
          };
          Matrix matrix = new Matrix();
          choosenImageView.getImageMatrix().invert(matrix);
          matrix.mapPoints(coords);
        return true;
      }
    

    Draw Using Canvas

            Resources res = getResources();
            Drawable drawable = res.getDrawable(R.drawable.image);
    
            Bitmap bmp= ((BitmapDrawable) drawable).getBitmap();
    
            alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
            canvas = new Canvas(alteredBitmap);
            paint = new Paint();
            paint.setColor(Color.GREEN);
            paint.setStrokeWidth(5);
            matrix = new Matrix();
            canvas.drawBitmap(bmp, matrix, paint);