44

一部の UI 要素の実際のサイズをピクセル単位で特定しようとしています。

これらの要素は .xml ファイルからインフレートされ、dip の幅と高さで初期化されるため、GUI は最終的に複数の画面サイズと dpi をサポートします ( android の仕様で推奨されているように)。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="150dip"
android:orientation="vertical">
    
<ImageView
    android:id="@+id/TlFrame" 
    android:layout_width="110dip" 
    android:layout_height="90dip"
    android:src="@drawable/timeline_nodrawing"
    android:layout_margin="0dip"
    android:padding="0dip"/></LinearLayout>

この前の xml は 1 つのフレームを表します。しかし、ここで説明する水平レイアウト内に多くを動的に追加します。

<HorizontalScrollView 
        android:id="@+id/TlScroller"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_margin="0dip"
        android:padding="0dip"
        android:scrollbars="none"
        android:fillViewport="false"
        android:scrollbarFadeDuration="0"
        android:scrollbarDefaultDelayBeforeFade="0"
        android:fadingEdgeLength="0dip"
        android:scaleType="centerInside">
        
        <!-- HorizontalScrollView can only host one direct child -->
        <LinearLayout 
            android:id="@+id/TimelineContent" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_margin="0dip"
            android:padding="0dip"
            android:scaleType="centerInside"/>
   
    </HorizontalScrollView > 

私のJavaコード内に1つのフレームを追加するために定義されたメソッド:

private void addNewFrame()
{       
    LayoutInflater inflater     = (LayoutInflater) _parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup root      = (ViewGroup) inflater.inflate(R.layout.tl_frame, null);
    TextView frameNumber = (TextView) root.findViewById(R.id.FrameNumber);
    Integer val = new Integer(_nFramesDisplayed+1); //+1 to display ids starting from one on the user side 
    frameNumber.setText(val.toString());
    
    ++_nFramesDisplayed;
    _content.addView(root);
// _content variable is initialized like this in c_tor
// _content = (LinearLayout) _parent.findViewById(R.id.TimelineContent);
}

次に、コード内で、実際の実際のサイズをピクセル単位で取得しようとします。これは、opengl のものを描画するために必要だからです。

LayoutInflater inflater     = (LayoutInflater) _parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup root      = (ViewGroup) inflater.inflate(R.layout.tl_frame, null);
    ImageView frame = (ImageView) root.findViewById(R.id.TlFrame);
    
    frame.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    frame.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    final int w = frame.getMeasuredWidth();
    final int h = frame.getMeasuredHeight();

これらの値が ImageView の実際のピクセル サイズよりもはるかに大きいことを除いて、すべて正常に動作しているようです。

getWindowManager().getDefaultDisplay().getMetrics(metrics); から報告された情報 は次のとおりです: 密度 = 1,5 密度Dpi = 240 widthPixel = 600 heightPixel = 1024

今、私はアンドロイドからのルールが次のとおりであることを知っています:ピクセル=ディップ*(dpi / 160)。しかし、返された値には何の意味もありません。(90dip X 110dip) の ImageView の場合、measure() メソッドの戻り値は (270 x 218) であり、ピクセル単位であると仮定しました。

誰もが理由を知っていますか? 返される値はピクセルですか?

ちなみに、私は同じコードをテストしてきましたが、ImageView ではなく TextView を使用して、すべて正常に動作しているようです。どうして !?!?

4

7 に答える 7

60

あなたはmeasure間違って電話をかけています。

measureMeasureSpecによって特別にパックされた値を取りますMeasureSpec.makeMeasureSpecmeasureLayoutParams を無視します。測定を行う親は、独自の測定とレイアウト戦略、および子の LayoutParams に基づいて MeasureSpec を作成する必要があります。

WRAP_CONTENTほとんどのレイアウトで通常機能する方法を測定したい場合は、次のmeasureように呼び出します。

frame.measure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST),
        MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST));

最大値がない場合 (たとえば、無限のスペースを持つ ScrollView のようなものを書いている場合)、次のUNSPECIFIEDモードを使用できます。

frame.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
于 2011-05-27T21:21:02.720 に答える
18

それを行う:

frame.measure(0, 0);

final int w = frame.getMeasuredWidth();
final int h = frame.getMeasuredHeight();

解決しました!

于 2012-06-28T22:46:58.457 に答える
5

Ok !ここで自分の質問に答えるようなものです...しかし、完全ではありません

1 - 一部のデバイスでは、ImageView の測定値が正確な値を提供しないようです。たとえば、Nexus や Galaxy デバイスでこのようなことが起こっているという報告をたくさん見てきました。

2 -私が思いついた回避策:

xml コード内で ImageView の幅と高さを「wrap_content」に設定します。

コード内のレイアウトを膨らませます (通常は、UI の初期化であると思います)。

LayoutInflater inflater     = (LayoutInflater) 
_parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root      = (ViewGroup) inflater.inflate(R.layout.tl_frame, null);
ImageView frame = (ImageView) root.findViewById(R.id.TlFrame);

一般的なAndroid の計算に基づいて、画像ビューの独自の比率を計算します

//ScreenDpi can be acquired by getWindowManager().getDefaultDisplay().getMetrics(metrics);
 pixelWidth = wantedDipSize * (ScreenDpi / 160)

計算されたサイズを使用して、コード内で ImageView を動的に設定します

frame.getLayoutParams().width = pixeWidth;

そして出来上がり!ImageView が必要な Dip サイズになりました;)

于 2011-05-30T15:42:50.277 に答える
4
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
 @SuppressLint("NewApi")
 @SuppressWarnings("deprecation")
 @Override
  public void onGlobalLayout() {
   //now we can retrieve the width and height
   int width = view.getWidth();
   int height = view.getHeight();


   //this is an important step not to keep receiving callbacks:
   //we should remove this listener
   //I use the function to remove it based on the api level!

    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN){
        view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }else{
        view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    }
  }
 });

ビューの幅/高さを取得する方法に行く必要があります

于 2015-02-01T14:02:37.877 に答える
2

Custom Textview を作成してレイアウトで使用し、 getActual height 関数を使用して実行時に高さを設定する必要があります

public class TextViewHeightPlus extends TextView {
    private static final String TAG = "TextViewHeightPlus";
    private int actualHeight=0;


    public int getActualHeight() {
        return actualHeight;
    }

    public TextViewHeightPlus(Context context) {
        super(context);
    }

    public TextViewHeightPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public TextViewHeightPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

   @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        actualHeight=0;

        actualHeight=(int) ((getLineCount()-1)*getTextSize());

    }

}
于 2013-10-24T11:42:58.230 に答える
1

おそらく、 AndroidManifest.xml ( link ) ファイルにあるものと、xml ファイルがどの drawable-XXX ディレクトリからのものかにより、Android はスケーリング操作でリソースをロードします。仮想の"dip" ( link ) 次元単位を使用することに決め、実際の値 (px) は異なる場合があります。

于 2011-05-27T21:05:57.993 に答える