簡體   English   中英

如何在android中獲取顯示圖像的寬度和高度?

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

這個問題可能看似微不足道,並且多次被問過,但我找不到對我有用的答案。

我有一個ImageViewGLSurfaceView是在頂部繪制ImageView當我按下一個按鈕。

對於我的ImageView我傳遞一個Bitmap並縮小它以避免OutOfMemoryError異常。 我的ImageView與我的UI中的其他元素對齊,因此它會拉伸圖像以填充寬度/高度(這就是我想要的)。

當我從ImageView更改為GLSurfaceView ,我的圖像周圍的黑色區域在我的ImageView變得透明,因為GLSurfaceView在UI中創建了一個洞並且工作方式與通常的元素不同,它將我的圖像背景繪制為黑色,我不需要它,因為我有自定義背景。

我找到了一個解決方案,以編程方式為GLSurfaceView設置LayoutParams ,我使用Bitmap寬度和高度,但最終得到一個較小的圖像。 我需要顯示圖像的確切寬度和高度 - 而不是ImageView因為它有透明區域。

PS:我不是OpenGL專家,如果你有一個適用於OpenGL的解決方案,請分享。

一個)

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

b)

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

我的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);

像素到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);
}

使GLSurfaceView的寬度和高度:wrap_content。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM