1

ImageView の一辺の実際の測定値を取得して正方形にする方法を知りたいです。

例えば:

私はこのImageViewを持っています

<ImageView
 android:id="@+id/imageView1"
 android:layout_width="fill_parent"
 android:layout_height="0dp"
 android:layout_weight="50"
 android:background="@drawable/image"/>

私がやりたいことは、このコードを使用して正方形を作成するために、実際の高さの測定値 (DP または PX) を取得することです。

ImageView imageView1 = (ImageView) this.findViewById(R.id.imageView1);
imageView1.getLayoutParams().width = imageView1.getHeight();

しかし、これらのコードのいずれかを使用すると、常に「0」になります。

   imageView1.getHeight();
   imageView1.getLayoutParams().height;
   imageView1.getMeasuredHeight();

layout_height "0dp" を 50% の layout_weight に設定したためです。

ImageViewを正方形にする方法はありますか??

たくさんのタック...

4

2 に答える 2

0

これを試して:

 public class Something extends Activity {
   ImageView imageView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_something);
      imageView = (ImageView) findViewById(R.id.yourImage);
      getDimensions();
  }

    private void getDimensions() {
      imageView.measure(imageView.getMeasuredWidth(),
      imageView.getMeasuredHeight());
      int Width = getMeasuredWidth();
      int Height = getMeasuredHeight();
      Toast.makeText(this, "width: " + Width + " height: " + Height,
            Toast.LENGTH_SHORT).show();
  }

}

API 4 / 17、ターゲットAPI 10を使用して、私にとってはうまくいきます。結果はpxです。

于 2013-01-12T02:58:43.030 に答える
0

これを使って

imageView.measure(imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
imageView.getHeight();
imageView.getWidth();
于 2012-07-13T07:37:54.487 に答える