我有下课。取决于我应该动态启动/停止动画的东西。我可以通过调用startAnimation()
方法来运行动画,但是当我调用stopAnimation()
时,动画不会停止。有趣的是,在调用ivSonar1
方法后,偶数ivSonar2
和stopAnimation()
仍然可见。
任何想法都会受到赞赏。感谢。
public class SonarView extends RelativeLayout
{
private static final int ANIM_LENGTH_IN_MS = 1500;
private ImageView ivSonar1;
private ImageView ivSonar2;
private AnimationSet animationSet1;
private AnimationSet animationSet2;
public SonarView(Context context)
{
super(context);
}
public SonarView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SonarView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SonarView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// try to make it 25% smaller than its real size to make enough room for items of biding list
super.onMeasure(widthMeasureSpec * 3 / 4, heightMeasureSpec * 3 / 4);
ivSonar1 = (ImageView) findViewById(R.id.ivSonar1);
ivSonar1.setVisibility(INVISIBLE);
ivSonar2 = (ImageView) findViewById(R.id.ivSonar2);
ivSonar2.setVisibility(INVISIBLE);
float maxWidth = findViewById(R.id.ivBackground).getMeasuredWidth();
animationSet1 = createAnimationSet(ivSonar1, maxWidth / ivSonar1.getMeasuredWidth());
animationSet2 = createAnimationSet(ivSonar2, maxWidth / ivSonar2.getMeasuredWidth());
animationSet2.setStartOffset(150);
}
@Override
protected void onFinishInflate()
{
super.onFinishInflate();
LayoutInflater.from(this.getContext()).inflate(R.layout.widget_sonar_view, this, true);
}
private AnimationSet createAnimationSet(final View v, final float toScale)
{
// Set animation
final AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
animationSet.setDuration(SonarView.ANIM_LENGTH_IN_MS);
animationSet.setAnimationListener(new Animation.AnimationListener()
{
@Override
public void onAnimationStart(Animation animation)
{
v.setAlpha(1.0f);
}
@Override
public void onAnimationEnd(Animation animation)
{
v.setAlpha(0.0f);
animationSet.setStartOffset(0);
v.startAnimation(animationSet);
}
@Override
public void onAnimationRepeat(Animation animation)
{
}
});
// TODO The scale animation should ideally scaled to the size of the outer ring, hacking it here instead to use alpha so that I don't have to the calculation right now. Will come back to this.
// Alpha animation
animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
// Scale animation
animationSet.addAnimation(new ScaleAnimation(0.0f, toScale, 0.0f, toScale, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f));
return animationSet;
}
public void stopAnimation()
{
if (ivSonar1 == null || ivSonar2 == null)
{
return;
}
ivSonar1.setVisibility(INVISIBLE);
ivSonar2.setVisibility(INVISIBLE);
animationSet1.cancel();
animationSet2.cancel();
ivSonar1.clearAnimation();
ivSonar2.clearAnimation();
}
public void startAnimation()
{
if (ivSonar1 == null || ivSonar2 == null)
{
return;
}
ivSonar1.setVisibility(VISIBLE);
ivSonar2.setVisibility(VISIBLE);
animationSet1.reset();
animationSet2.reset();
animationSet2.setStartOffset(150);
ivSonar1.startAnimation(animationSet1);
ivSonar2.startAnimation(animationSet2);
}
}
答案 0 :(得分:0)
要停止AnimationSet
,您可能还需要删除所有侦听器:
animationSet.setAnimationListener(null);
animationSet.cancel();