Janmejoy
Janmejoy

Reputation: 2731

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.

Upvotes: 6

Views: 2110

Answers (3)

Janmejoy
Janmejoy

Reputation: 2731

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);

Upvotes: 1

Ercan
Ercan

Reputation: 3705

You have to calculate your device screen ratio first,

deviceWidth / screenHeight = ratio

then you have to multiply it with your desired coordinate :

ratio * xCoord = newXcoord; // and same for Y coord

By doing this you will keep the equivalent coordinates for all device sizes.

Also you may declare sizes and coordinates with respect to % of screen dimensions like :

x = 0.2 * deviceWidth  // %20 of device width from left

Upvotes: 1

rKrishna
rKrishna

Reputation: 1399

You can try this

imageview.post(new Runnable() {
            @Override
            public void run() {
                int[] posXY = new int[2];
                imageview.getLocationOnScreen(posXY);
                int MoveX = posXY[0];
                int MoveY = posXY[1];
Log.i("xy co-ordinate", "x "+MoveX, "y"+MoveY);

            }
        });

Upvotes: 0

Related Questions