How to get width and height of displayed image in android?
Asked Answered
M

1

12

This question may seem trivial and been asked many times, but I couldn't find an answer that will work for me.

I have an ImageView and GLSurfaceView that is drawn on top of ImageView when I push a button.

For my ImageView I pass a Bitmap and scale it down in order to avoid an OutOfMemoryError exception. My ImageView is aligned to other elements in my UI, so it stretches the image to fill width/height (this is what I want).

When I change from ImageView to GLSurfaceView I get black regions around my image that used to be transparent in my ImageView, because GLSurfaceView makes a hole in UI and works differently than usual elements it draws background of my image to black and I don't need it, because I have custom background.

I came down with a solution to set LayoutParams for GLSurfaceView programmatically and I use Bitmap width and height, but I end up with a smaller image. I need the exact width and height of the image displayed - not the ImageView because it has transparent regions.

PS: I'm not an OpenGL expert and if you have a solution that works for OpenGL please share it.

a)

 _______________
|               |
|               |
|---------------|          <-
|   transparent |     
|---------------| <-            Entire
|               |               ImageView
|     Image     |   need
|               | only this
|---------------| <-
|   transparent |
|---------------|          <-
|               |
 ---------------

b)

 _______________
|               |
|               |
|---------------|       <-
|   black       |
|---------------|
|               |              
|    Image      |          GLSurfaceView
|               |
|---------------|
|   black       |
|---------------|       <-
|               |
 ---------------

My ImageView and GLSurfaceView:

<android.opengl.GLSurfaceView
    android:id="@+id/glsvImageHolder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/horScrollImage"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="50dp"
    android:visibility="invisible" />
<ImageView
    android:id="@+id/ivImageHolder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/horScrollImage"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="50dp"
    android:visibility="visible" />
Monoculture answered 11/12, 2014 at 1:0 Comment(2)
Does bitmap itself contains top and bottom transparent regions?Shererd
No, bitmap doesn't contain transparent regions.Monoculture
P
4
float widthPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 
                                pxToMm(ivImageHolder.getWidth(), context), 
                                getResources().getDisplayMetrics());

float heightPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 
                            pxToMm(ivImageHolder.getHeight(), context), 
                            getResources().getDisplayMetrics());

ViewGroup.LayoutParams layoutParams= glsvImageHolder.getLayoutParams();
layoutParams.width= (int) widthPx;
layoutParams.height= (int) heightPx;

glsvImageHolder.setLayoutParams(layoutParams);

Pixel to MM

public static float pxToMm(final float px, final Context context)
{
    final DisplayMetrics dm = context.getResources().getDisplayMetrics();
    return px / TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 1, dm);
}

Make GLSurfaceView' s width and height : wrap_content.

Paintbrush answered 20/8, 2015 at 10:14 Comment(1)
this is just converting width and height of something from an arbitrary unit(maybe px) to mm...Nganngc

© 2022 - 2024 — McMap. All rights reserved.