Get the displayed size of an image inside an ImageView
the following will work:
ih=imageView.getMeasuredHeight();//height of imageView
iw=imageView.getMeasuredWidth();//width of imageView
iH=imageView.getDrawable().getIntrinsicHeight();//original height of underlying image
iW=imageView.getDrawable().getIntrinsicWidth();//original width of underlying image
if (ih/iH<=iw/iW) iw=iW*ih/iH;//rescaled width of image within ImageView
else ih= iH*iw/iW;//rescaled height of image within ImageView
(iw x ih) now represents the actual rescaled (width x height) for the image within the view (in other words the displayed size of the image)
EDIT: I think a nicer way to write the above answer (and one that works with ints) :
final int actualHeight, actualWidth;
final int imageViewHeight = imageView.getHeight(), imageViewWidth = imageView.getWidth();
final int bitmapHeight = ..., bitmapWidth = ...;
if (imageViewHeight * bitmapWidth <= imageViewWidth * bitmapHeight) {
actualWidth = bitmapWidth * imageViewHeight / bitmapHeight;
actualHeight = imageViewHeight;
} else {
actualHeight = bitmapHeight * imageViewWidth / bitmapWidth;
actualWidth = imageViewWidth;
}
return new Point(actualWidth,actualHeight);
Get image dimensions after it draws in screen
You can do a lot with the matrix the view uses to display the image.
Here I calculate the scale the image is drawn at:
private float scaleOfImageView(ImageView image) {
float[] coords = new float[]{0, 0, 1, 1};
Matrix matrix = image.getImageMatrix();
matrix.mapPoints(coords);
return coords[2] - coords[0]; //xscale, method assumes maintaining aspect ratio
}
Applying the scale to the image dimensions gives the displayed image size:
private void logImageDisplaySize(ImageView image) {
Drawable drawable = image.getDrawable();
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
float scale = scaleOfImageView(image);
float displayedWidth = scale * width;
float displayedHeight = scale * height;
Log.d(TAG, String.format("Image drawn at scale: %.2f => %.2f x %.2f",
scale, displayedWidth, displayedHeight));
}
I suspect you don't really care about the image size, I suspect you want to map touch points back to a coordinate on the image, this answer shows how to do this (also using the image matrix): https://stackoverflow.com/a/9945896/360211
How to change imageview according to screen size
First, remove the try and catch. Its not good to hide this error, simply check for possible url issues and set your image by hand, like in your cache block. Check the userimage url for its empty or null, if you use a String or Uri.
The point is you are using a LinearLayout
, which takes care that the child views not overlap. If you change the ImageView
height it could be possible, that your TextViews
get cut on bottom, because there is not enough space defined by your android:layout_height="@dimen/nav_header_height"
value.
Set your wished hight and width in XML like:
<ImageView
android:id="@+id/header_image"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@android:drawable/sym_def_app_icon" />
Simply find your correct size / ask your designer (if you work with one) whats the suitable size for this image. This will change the View bounds of the ImageView
, the shown image will, of course, depend on this size.
You can play a little bit with the following attributes for imageView:
android:scaleType
android:adjustViewBounds
This changes the behavior how the image will be shown inside the bounds of the ImageView
.
Use dp instead of pixel, this takes also care, that your image view gets good scaled on different device sizes.
And you can play with Picasso functions like:
Picasso.with(this)
.load(user.getUserImage())
.error(R.drawable.ic_menu_camera)
.placeholder(...)
.resize(imageView.getMeasuredWidth(), imageView.getMeasuredHeight()) // <-- is needed for some functions
.fit()// <--
.centerCrop()// <--
.centerInside()// <--
.into(profileImage);
Simply find your function which gives you best result.
Android ImageView Fixing Image Size
Fix ImageView's size with dp
or fill_parent
and set android:scaleType
to fitXY
.
increase the size of the image inside the imageview
Try this:
for image use: android:scaleType="fitXY"
and for 1/3 of the screen use android weightsum
and layout_weight
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3">
<ImageButton
android:id="@+id/image_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/transparent"
android:scaleType="fitXY"
android:src="@android:drawable/ic_menu_add"
android:visibility="visible" />
</LinearLayout>
output:
Trying to get an ImageView to scale as the screen gets bigger
you should change the scaleType of imageview.
if you want to scale image with maintain aspect ratio you should use
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitcenter"/>
u can try another option like centerCrop and fitxy and see the result
Related Topics
Returning from an Activity Using Navigateupfromsametask()
Edittext Maxlines Not Working - User Can Still Input More Lines Than Set
How to Set Android_Sdk_Home Environment Variable
Where to Find Info on Android's "Service Call" Shell Command
Android: How to Check If the Server Is Available
Actionbaractivity Cannot Resolve a Symbol
Volley Android Networking Library
Slide a Layout Up from Bottom of Screen
How to Get Response as String Using Retrofit Without Using Gson or Any Other Library in Android
How to Perform an SQLite Query Within an Android Application
Keep a Service Running Even When Phone Is Asleep
Eclipse/Android:"Errors Running Builder 'Android Pre Compiler' on Project..."
Is There a Simple Example of the Popupwindow Class Using Android V2.0
Flutter Projects & Android X Migration Issues
How to Make Android Apps Which Support Both 32-Bit and 64-Bit Architecture
How to Perform Collection Group Query Using Document Id in Cloud Firestore