3
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_root" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/myImage" android:layout_width="match_parent" android:layout_height="match_parent" android:adjustViewBounds="true" android:background="@android:color/black" android:src="@drawable/image2" /> <View android:id="@+id/view_clickable_first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#aa000000" /> </RelativeLayout> 

I want to make a small part of image in Imageview(myImage) clickable after getting coordinates. Its working fine for me. Now I want to get the functionality of pinch zoom on this image. As I zoom in, clickable area along with the image will also zoom and accordingly.

My problem is that, I want to get height of image in ImageView (excluding black spaces in top and bottom. As ImageView is set to match parent, Image is showing in center of the screen in landscape mode). EveryTime I am getting height of imageview.

My code for getting height of image and imageview:

 int imagWidth = imageView.getWidth(); int h = imageView.getHeight(); int w = imageView.getDrawable().getIntrinsicWidth(); int imagHeight = imageView.getDrawable().getIntrinsicHeight(); Log.e("height", "Image height" + imagHeight + "\n" + "Imageview height" + h); 

So as per calculation: Image height= 882 Imageview height = 672

11
  • 1
    see ImageView#getDrawable() method Commented Mar 13, 2018 at 10:13
  • @pskink... yes I have tried int w = imageView.getDrawable().getIntrinsicWidth(); int imagHeight = imageView.getDrawable().getIntrinsicHeight(); Its not working Commented Mar 13, 2018 at 10:16
  • @NileshRathod, the post u have suggested isn't genuine . . . just check the first comment of Jemshit Iskenderov on the answer Commented Mar 13, 2018 at 10:17
  • 1
    yes, it is working - it returns the size of the Drawable that is shown Commented Mar 13, 2018 at 10:18
  • @pskink : plz check my updated post I ahve updated values which I am getting Commented Mar 13, 2018 at 10:27

3 Answers 3

3

Work for me

 imgView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // Ensure you call it only once : imgView.getViewTreeObserver().removeGlobalOnLayoutListener(this); // Here you can get the size :) width = imgView.getWidth(); height = imgView.getHeight(); } }); 
Sign up to request clarification or add additional context in comments.

Comments

1

To get the height of an image in the image view you can simply do:

int width = imgView.getDrawable().getIntrinsicWidth(); int height = imgView.getDrawable().getIntrinsicHeight(); 

This will return the 'intrinsic' values of height and width. (these values are the values of the image you are loading into the image view as is without any scaling. you can adjust these values to match any scaling that you may have applied) To adjust values for scaling (given you know its in landscape mode) the following should work :

int width = imgView.getDrawable().getIntrinsicWidth(); int height = imgView.getDrawable().getIntrinsicHeight(); double scale = ((double)imgeView.getWidth())/width int final_height = Math.toIntExact(Math.round(scale * height)); 

now final_height is the height of the displayed image.

3 Comments

I have tried this...not working...getting height and width of imageview..
@PragyaMendiratta ... can we see your code please ? as i have used the same functions before to get the height and width of images that i use in apps.
@PragyaMendiratta you do seem to be getting the height of the image as is. since the image height is different from the image view height. are you asking for the height of the displayed image ? just scale your image height by the current image scaling. ill edit my answer to match this
0

Use these below code with ViewTreeObserver

int yourImageViewWidth , yourImageViewHeight; ImageView y = (ImageView)findViewById(R.id.scaled_image); ViewTreeObserver vto = yourImageView.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { yourImageView.getViewTreeObserver().removeOnPreDrawListener(this); yourImageViewHeight = yourImageView.getMeasuredHeight(); yourImageViewWidth = yourImageView.getMeasuredWidth(); Log.d("Height: " + yourImageViewHeight + " Width: " + yourImageViewWidth); return true; } }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.