我尝试设置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();
}
感谢您的帮助!。