gpt4 book ai didi

几次 setAnimation 调用后 Android View 旋转没有发生

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:46:31 25 4
gpt4 key购买 nike

我一直在尝试编写一个将图像旋转到特定角度的 Activity 方法。旋转有效,但问题是该方法大约每秒调用一次,但仅在前几次旋转。在某一时刻,动画图像只是静止不动,即使动画正在设置并以不同的值开始!它应该像前几次那样移动图像。

private void animateNeedle(final float sfAngle)
{
final View viewNeedle = findViewById(R.id.needle);
final RotateAnimation needleAnimation;

RotateAnimation anim;

anim = new RotateAnimation( 0,
sfAngle,
m_nNeedleWidth / 2,
m_nNeedleHeight);
anim.setDuration(200);
anim.setFillAfter(true);
viewNeedle.setAnimation(anim);
anim.setAnimationListener(new AnimationListener()
{

@Override
public void onAnimationEnd(final Animation animation)
{
viewNeedle.clearAnimation();
}

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

@Override
public void onAnimationStart(final Animation animation)
{
// TODO Auto-generated method stub
}
});

anim.start();
findViewById(android.R.id.content).postInvalidate();
}

我做错了什么?我确保该函数在动画期间不会被调用两次,但它没有帮助。

最佳答案

问题是它没有在主线程上运行,为了修复它我们使用了:

anim = new RotateAnimation( m_lastAngle,
sfAngle,
viewNeedle.getWidth() / 2,
viewNeedle.getHeight());
anim.setDuration(1000);
anim.setFillAfter(true);
viewNeedle.setAnimation(anim);
anim.setAnimationListener(new AnimationListener()
{
@Override
public void onAnimationEnd(final Animation animation)
{
// viewNeedle.clearAnimation();
}

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

}

@Override
public void onAnimationStart(final Animation animation)
{
m_lastAngle = sfAngle;
}
});

runOnUiThread(new Runnable()
{

@Override
public void run()
{
viewNeedle.startAnimation(anim);
findViewById(android.R.id.content).invalidate();
}
});

我们还使用了:

    final View viewNeedle = findViewById(R.id.needle);
viewNeedle.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
scaleView(viewNeedle);
}

/**
* Scale a view.
*
* @param viewToScale - View to scale.
*/
private void scaleView(final View viewToScale)
{
int height = (int) (viewToScale.getHeight() / GraphicsPoint
.getScalePoint().y);
int width = (int) (viewToScale.getWidth() / GraphicsPoint
.getScalePoint().x);
final LayoutParams layoutParams = viewToScale
.getLayoutParams();
layoutParams.height = height;
layoutParams.width = width;
viewToScale.setLayoutParams(layoutParams);
viewToScale.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
});

关于几次 setAnimation 调用后 Android View 旋转没有发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19711419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com