使用动画时如何避免视图闪烁?

5

我在onCreate事件中使用翻译和旋转动画来定位FrameLayout中的View。我希望动画能够立即执行,因此我将它们的持续时间设置为0。但是当应用程序启动时,我的View会在屏幕左上角短暂地闪烁,然后根据动画参数定位。如何避免这种闪烁?

5个回答

6

我花了一整天的时间来解决这个问题。fillAfter和fillBefore与此无关。在动画开始之前尝试使用以下方法:

view.setVisibility(View.GONE); // view is our View we trying to animate

然后为您的动画设置动画侦听器:
    animation.setAnimationListener(new AnimationListener(){
        public void onAnimationEnd(Animation animation) {
        }

        public void onAnimationRepeat(Animation animation) {
        }

        public void onAnimationStart(Animation animation) {
            v.setVisibility(View.VISIBLE);
        }

1
v和view是一样的吗? - Myat Min Soe

4

根据您的需求使用animation.setFillAfter(true)animation.setFillBefore(true),这应该解决闪烁问题。


谢谢,它已经设置为true了,但这并不能解决我的问题。我希望动画是瞬间的,这样用户只能看到View的结束位置,而不会看到它的起始位置。 - Pavlo Viazovskyy
那么,为什么你需要动画呢?为什么不直接将你的视图显示到那个位置呢? - Buda Gavril
我需要旋转一个视图,但不知道如何在不使用动画的情况下实现。 - Pavlo Viazovskyy
2
创建一个自定义视图,在onDraw()方法中旋转你的画布。 - Buda Gavril

2

在动画文件的父标签内使用一个<style>标签会导致这种情况发生,请尝试只使用一个<style>标签,您会看到不同。我正在处理这个问题。


0

将可见性设置在动画执行代码之后,而不是在“onAnimationEnd”中,还尝试设置持续时间

view.setVisibility(View.VISIBLE); // view is our View we trying to animate

0
Odaym的回答实际上是这个问题的解决方案。如果你有类似这样的东西:
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="true">
    <set
        android:duration="1000">
        <translate
            android:fromXDelta="100%"
            android:toXDelta="0"
            />
    </set>
</set>

将其更改为:

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="true" android:duration="1000">
        <translate
            android:fromXDelta="100%"
            android:toXDelta="0"
            />
</set>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接