Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 975
Janmejoy Avatar asked Sep 24 '15 09:09

Janmejoy


People also ask

What is ImageView in Android Studio?

ImageView class is used to display any kind of image resource in the android application either it can be android. graphics. Bitmap or android. graphics. drawable.

How do you show the full size image in pop up when clicking the picture on android?

//On clicking image,display the image in full screen imageView. setOnClickListener(new View. OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(context, FullImageActivity. class); intent.

What is a bitmap Android?

A bitmap is simply a rectangle of pixels. Each pixel can be set to a given color but exactly what color depends on the type of the pixel. The first two parameters give the width and the height in pixels. The third parameter specifies the type of pixel you want to use.


2 Answers

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
like image 100
Ercan Avatar answered Oct 05 '22 06:10

Ercan


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);
like image 22
Janmejoy Avatar answered Oct 05 '22 07:10

Janmejoy