2

今、私は円であるImgeViewを持っていて、その円の中心点を見つけたいので、次のコードを試しましたが、機能していないようです

int[] location = new int[2];
            imageView.getLocationOnScreen(location);

int radius=imageView.getHeight()/2;
                    int[] center=new int[2];
                    center[0]=location[0]+radius;
                    center[1]=location[1]+radius;

それで、私が「場所」によって得た座標は、imageviewのトップポイントであるかどうか?

イメージビューの中心点を取得する方法を教えてください。

4

3 に答える 3

10

以下の状況を想定しました。

ImageView を (xml またはコードから) ロードし、myImageView(たとえば) で同じ参照を取得します。ここで、画面上の位置に関係なく、myImageView の中心を計算したいと考えています。

私の仮定が正しければ、おそらく以下があなたが探している解決策です:

    int centerXOnImage=myImageView.getWidth()/2;
    int centerYOnImage=myImageView.getHeight()/2;

    int centerXOfImageOnScreen=myImageView.getLeft()+centerXOnImage;
    int centerYOfImageOnScreen=myImageView.getTop()+centerYOnImage;
于 2011-07-14T06:59:48.913 に答える
0

Chandanの答えに加えて、同じコードを使用しているとします:

int centerXOnImage=myImageView.getWidth()/2;
int centerYOnImage=myImageView.getHeight()/2;

int centerXOfImageOnScreen=myImageView.getLeft()+centerXOnImage;
int centerYOfImageOnScreen=myImageView.getTop()+centerYOnImage;

0 を返す場合myImageView.getWidth()は、少し変更する必要があります。

myImageView.measure(0,0);
int centerXOnImage=myImageView.getMeasuredWidth()/2;
int centerYOnImage=myImageView.getMeasuredHeight()/2;

myImageView がコードで作成され、ルート ビューに追加された後で、これを実行する必要があることに注意してください。

于 2013-09-19T03:54:57.257 に答える
0

次を使用して、左上隅の座標を取得できます。

ImageView iv = (ImageView)findViewById(R.id.image_view);
Rect rect = iv.getDrawable().getRect();
int xOffset = rect.left;
int yOffset = rect.top;

height/2 と width/2 に基づいて、 ImageView の中心を確立できます。

于 2011-07-14T06:48:13.883 に答える