1

y 軸に沿って相対的なレイアウトをアニメーション化したいので、y 軸上の位置を取得したいと考えています。私は次のような多くのことを試しました:

getY()
getTop()
getScreenLocation()

などですが、すべてが 0 を返しています。

どうすれば位置を見つけることができますか?

以下はコードです

ImageView advancetab = (ImageView) findViewById(R.id.imageView4);
RelativeLayout advancelayout = (RelativeLayout) findViewById(R.id.relativeLayout2);
final ObjectAnimator anim = ObjectAnimator.ofFloat(advancelayout, "Y", 
            advancelayout.getY(),advancelayout.getY()-100);
    anim.setDuration(1000);     
advancetab.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            anim.start();
        }
    });

画像をクリックすると、レイアウトが 0,0 の位置から上にアニメーション化されます。

4

3 に答える 3

3

これが私の答えです。

private int[] getViewLocations(View view) {
  int[] locations = new int[2];
  view.getLocationOnScreen(locations);
  return locations;
}

このように使って、

int[] locations = getViewLocations(yourcustomview);
int x = locations[0]; // x position of left
int y = locations[1]; // y position of top

楽しむ。@。@

于 2015-06-02T08:30:37.857 に答える
0

「キャンバス」を作成してから、オプションを使用してx座標とy座標の位置を見つける必要があるかどうかはわかりません。他のオプションがあるかもしれませんが、これはこれだけを使用して行いました。

于 2011-06-13T06:13:34.367 に答える
0

アクティビティ クラスの onwindowsfocuschange() 内にコードを記述してください。任意のビューの getLocationonscreen() メソッドを使用して、画面上の位置に関するパラメータを取得します。

于 2013-11-11T09:45:08.227 に答える