一部の 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 を使用して、すべて正常に動作しているようです。どうして !?!?