0

我尝试设置ViewHelper.setPivotY(test, 0);方法onClick(),但它不起作用。动画仍然缩放到 centerY。这是我的代码。我不知道我错在哪里?

公共类 NewClassTest 扩展 Activity{

LinearLayout test;
FrameLayout content;
Button btn;

boolean toggle = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_activity);

    test = (LinearLayout) findViewById(R.id.test);

    content = (FrameLayout) findViewById(R.id.content);
    content.setVisibility(View.INVISIBLE);

    btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // ViewHelper.setPivotY(test, 0); does not work
            runAnimation(toggle);
            toggle = !toggle;
        }
    });
    ViewHelper.setPivotY(test, 0); // work

}

private void runAnimation(boolean in){
    AnimatorSet animSet = new AnimatorSet();
    ObjectAnimator anim = null, anim2 = null;
            // ViewHelper.setPivotY(test, 0); also does not work
    if(in){
        anim = ObjectAnimator.ofFloat(test, "scaleY", 1, 1 - (((float)content.getHeight()) / ((float) test.getHeight())));
        anim2 = ObjectAnimator.ofFloat(content, "translationY", content.getHeight(), 0);
        animSet.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {
                // TODO Auto-generated method stub

                content.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationCancel(Animator animation) {
                // TODO Auto-generated method stub

            }
        });
    }else {
        anim = ObjectAnimator.ofFloat(test, "scaleY", 1 - ((float)content.getHeight()) / ((float) test.getHeight()), 1);
        anim2 = ObjectAnimator.ofFloat(content, "translationY", 0, content.getHeight());
        animSet.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                // TODO Auto-generated method stub
                content.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                // TODO Auto-generated method stub

            }
        });
    }
    animSet.setDuration(1000).play(anim2).with(anim);
    animSet.start();
}

感谢您的帮助!。

4

1 回答 1

1

也发生在我身上。在带有 android 4.1 的 HTC One S 上测试它并没有奏效,但在带有 android 2.2 的三星 Galaxy I 中它按预期工作。

改成ViewHelper.setPivotY(test, 0.00000001f);解决了=\

不知道是HTC、android还是nineoldandroids的bug

于 2013-02-14T14:49:23.600 回答