Android ImageView动画启动不工作
我正在构建一个应用程序,我想为一些Android ImageView动画启动不工作,android,animation,imageview,Android,Animation,Imageview,我正在构建一个应用程序,我想为一些imageView元素设置动画。问题是,在动画结束时,元素在y轴上得到意外的提升,而不是定义的提升 我知道在动画过程结束时,动画元素会返回到其原始的x,y。我在onAnimationEnd方法中手动添加了动画偏移。出于某种奇怪的原因,动画元素向上跳跃了大约40个像素,我知道我没有错误地将其添加到任何地方。这里怎么了 public class MyActivity extends Activity { private ImageView logo,m
imageView
元素设置动画。问题是,在动画结束时,元素在y轴上得到意外的提升,而不是定义的提升
我知道在动画过程结束时,动画元素会返回到其原始的x,y。我在onAnimationEnd
方法中手动添加了动画偏移。出于某种奇怪的原因,动画元素向上跳跃了大约40个像素,我知道我没有错误地将其添加到任何地方。这里怎么了
public class MyActivity extends Activity {
private ImageView logo,moto;
private int[] imageView_XY;
public float logoXpos,logoYpos;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verification_activity);
logo = (ImageView) findViewById(R.id.imageView1);
//animates the logo
animate(logo,30);
}
public void animate(final ImageView imageView, int amount){
//gets the current x,y of the widget
imageView_XY = new int[2];
imageViewElement.getLocationInWindow(imageView_XY);
logoXpos = (int) imageViewElement.getX();
logoYpos = imageView_XY[1];
Log.d("Animation XY: "," current location "+"("+logoXpos+","+logoYpos+")"); //prints out 0,0
//animates the widget 100 px up
TranslateAnimation anim = new TranslateAnimation(0,0,0,-amount);
logoAnim.setDuration(1000);
logoAnim.setFillAfter(false);
logoAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
Log.d("Animation: ","animation ended");
logoXpos = imageView_XY[0];
logoYpos = imageView_XY[1];
Log.d("Animation ended XY: "," current location "+"("+logoXpos+","+logoYpos+")"); //prints out 0,0 again
imageViewElement.setY(logoYpos+30);
}
@Override
public void onAnimationRepeat(Animation animation) {
Log.d("Animation: ","animation repeat");
}
});
imageViewElement.startAnimation(anim);
}
好的,我把它弄好了。对于未来的谷歌用户:
我最终设置了
logoaim.setFillAfter(false)
到true
并在动画imageViewElement.setY(logoYpos+30)结束时取消手动移位代码>谢谢steve,睡了一个好觉后,我觉得这不是添加到回调中的最明智的事情:)此外,必须将setFillAfter设置为true,以便将动画元素保留在适当的位置。但奇怪的是,当我调用backPressed方法上的animate方法时,我无法将该元素动画化回其原始位置。有什么想法吗?