Android动画错误的坐标

时间:2013-12-16 15:34:07

标签: android animation

我正试图像FB的应用程序一样制作启动画面,徽标向上移动并显示登录内容。我不知道为什么,但这不能正常工作:

public class Login extends Activity {
ImageView logo;
int width, height;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    logo = (ImageView) findViewById(R.id.logo);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (logo != null) {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        width = size.x / 2;
        height = size.y / 2;

        TranslateAnimation animation = new TranslateAnimation(width, width,
                height, height / 2);
        animation.setDuration(2500);
        animation.setFillAfter(false);
        animation.setAnimationListener(new MyAnimationListener());

        logo.startAnimation(animation);
        // /////
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

private class MyAnimationListener implements AnimationListener {

    @Override
    public void onAnimationEnd(Animation animation) {
        logo.clearAnimation();
        LayoutParams lp = new LayoutParams(logo.getWidth(),
                logo.getHeight());
        lp.setMargins((int) width, (int) height / 2, 0, 0);
        logo.setLayoutParams(lp);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationStart(Animation animation) {
    }

}

}

我正试图从屏幕中心开始垂直移动ImageView:

  width = size.x / 2;
  height = size.y / 2;

而不是那样,ImageView从右下角垂直移动。  1.为什么划分屏幕尺寸/ 2不会将ImageView置于其中心?  2.动画结束后,我将新的LayoutParams设置为ImageView:lp.setMargins((int) width, (int) height / 2, 0, 0);。不应该把它放在动画结束的地方吗?而不是那样,它被推向左边。

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:gravity="center"
tools:context=".Login" >

<ImageView
    android:id="@+id/logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/logo" />

提前致谢:)

0 个答案:

没有答案
相关问题