简体   繁体   English

如何在android中获取显示图像的宽度和高度?

[英]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. 我有一个ImageViewGLSurfaceView是在顶部绘制ImageView当我按下一个按钮。

For my ImageView I pass a Bitmap and scale it down in order to avoid an OutOfMemoryError exception. 对于我的ImageView我传递一个Bitmap并缩小它以避免OutOfMemoryError异常。 My ImageView is aligned to other elements in my UI, so it stretches the image to fill width/height (this is what I want). 我的ImageView与我的UI中的其他元素对齐,因此它会拉伸图像以填充宽度/高度(这就是我想要的)。

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. 当我从ImageView更改为GLSurfaceView ,我的图像周围的黑色区域在我的ImageView变得透明,因为GLSurfaceView在UI中创建了一个洞并且工作方式与通常的元素不同,它将我的图像背景绘制为黑色,我不需要它,因为我有自定义背景。

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. 我找到了一个解决方案,以编程方式为GLSurfaceView设置LayoutParams ,我使用Bitmap宽度和高度,但最终得到一个较小的图像。 I need the exact width and height of the image displayed - not the ImageView because it has transparent regions. 我需要显示图像的确切宽度和高度 - 而不是ImageView因为它有透明区域。

PS: I'm not an OpenGL expert and if you have a solution that works for OpenGL please share it. PS:我不是OpenGL专家,如果你有一个适用于OpenGL的解决方案,请分享。

a) 一个)

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

b) b)

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

My ImageView and GLSurfaceView : 我的ImageViewGLSurfaceView

<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 像素到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. 使GLSurfaceView的宽度和高度:wrap_content。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM