我正在创建具有指定半径的半圆图像移动动画。我的代码工作正常。但是它在动画结束后移动到它的初始位置。我的代码如下:
内部活动:
image = (ImageView) findViewById(R.id.image);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation anim = new MyAnimation(image, 150); // radius = 150
anim.setDuration(3000);
anim.setFillEnabled(true);
anim.setFillAfter(true);
image.startAnimation(anim);
}
});
动画类:
public class MyAnimation extends Animation {
private View view;
private float cx, cy; // center x,y position of circular path
private float prevX, prevY; // previous x,y position of image during animation
private float r; // radius of circle
private float prevDx, prevDy;
/**
* @param view - View that will be animated
* @param r - radius of circular path
*/
public MyAnimation(View view, float r){
this.view = view;
this.r = r;
}
@Override
public boolean willChangeBounds() {
return true;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
// calculate position of image center
int cxImage = width / 2;
int cyImage = height / 2;
cx = view.getLeft() + cxImage;
cy = view.getTop() + cyImage;
// set previous position to center
prevX = cx+r;
prevY = cy;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 0){
t.getMatrix().setTranslate(prevDx, prevDy);
return;
}
float angleDeg = (interpolatedTime * 180f ) % 180;
float angleRad = (float) Math.toRadians(angleDeg);
// r = radius, cx and cy = center point, a = angle (radians)
float x = (float) (cx + r * Math.cos(angleRad));
float y = (float) (cy + r * Math.sin(angleRad));
float dx = prevX - x;
float dy = prevY - y;
prevX = x;
prevY = y;
prevDx = dx;
prevDy = dy;
t.getMatrix().setTranslate(dx, dy);
}
}
请指导我完成此任务。
答案 0 :(得分:0)
像这样开始动画
image.startAnimation(fadeInAnimation);
并添加侦听器以进行查看,并在onAnimationEnd上更改视图位置
fadeInAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
答案 1 :(得分:0)
你可以设置监听器来解决这个问题。
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {
image.setVisibility(View.GONE);
}
});
image.startAnimation(mAnimation);
答案 2 :(得分:0)
结束动画监听器image.setX(image.getX()+2*radius)
。这将改变图像的位置。
答案 3 :(得分:-1)
@Rathi J
不知道Excat Answer ..但我正在尝试。
我认为你应该替换:
anim.setFillAfter(true);
用这个:
anim.setFillAfter(false);
希望这会有所帮助..(: