当前位置: 动力学知识库 > 问答 > 编程问答 >

imageview - 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" />

网友答案:
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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: