1

画像の元のサイズではなく、画面に表示される画像のサイズを取得しようとしています。

私はすでにそれについていくつかの調査を行っており、2年前の未解決の投稿で同じ質問を見つけたので、おそらく今それを行う新しい方法があると考えていました.

私はこの3つの解決策を試しました:

//Get the same result for all my images (whereas images have difference sizes)
imageView.getWidth() + imageView.getHeight()

//Get the original size of the images  
imageView.getDrawable().getIntrinsicWidth() + imageView.getDrawable().getIntrinsicHeight()
bitMap.getWidth() + bitMap.getHeight()

私のxml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dip" >

<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/descr_image"
android:layout_alignParentBottom="true" />

<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>

更新 1

ViewTreeObserver viewTreeObserver = imageView.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
     viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
       @SuppressWarnings("deprecation")
       public void onGlobalLayout() {      
           imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this)               
           System.out.println("TEST :" + imageView.getWidth() + " " + imageView.getHeight());
        }
    });
}
4

1 に答える 1

2

あなたImageViewの幅と高さはに設定されています

android:layout_width="fill_parent"
android:layout_height="fill_parent"

画像ビューは親を埋め、getWidth()それらgetHeight()の値を提供します。

ImageView適切に表示するには、何らかの形でラップする必要がありますが、ImageView幅/高さの値を次のように指定します

android:layout_width="wrap_content"
android:layout_height="wrap_content"

この問題も解決すると思われるImageView ビットマップ スケール ディメンションに関する回答もあります。

https://stackoverflow.com/users/321697/kcoppockによる回答から:

ImageView iv = (ImageView)findViewById(R.id.imageview);
int scaledHeight, scaledWidth;
iv.getViewTreeObserver().addOnPreDrawListener(
    new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        Rect rect = iv.getDrawable().getBounds();
        scaledHeight = rect.height();
        scaledWidth = rect.width();
        iv.getViewTreeObserver().removeOnPreDrawListener(this);
        return true;
    }
});
于 2012-12-26T18:55:11.627 に答える