Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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" />
like image 655
Fedor Tsyganov Avatar asked Dec 11 '14 01:12

Fedor Tsyganov


People also ask

How can get device width and height in android programmatically?

Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size. x; int height = size.


1 Answers

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.

like image 144
Dauezevy Avatar answered Oct 12 '22 20:10

Dauezevy