自定义 TranslateAnimation 导致闪烁

发布于 2024-10-30 03:21:09 字数 1014 浏览 1 评论 0原文

我的自定义 TranslateAnimation 确实垂直移动视图。除了开始时有奇怪的短暂闪烁之外,一切都很好。如果视图在意外位置闪烁(比动画应该开始的位置高得多),则似乎只有一个可见帧。

注意:当我调用 super(0,0,0,0) 但没有动画时,不会发生闪烁。

这是我的代码的简短版本:

public class ExTranslateAnimation extends TranslateAnimation implements AnimationListener
{
    private View myView;
    public ExTranslateAnimation (...)
    {
        // delta is how much it gets moved
        super(0, 0, -delta, 0);
        this.setAnimationListener(this);
        this.setDuration(duration);
        toY =  view.getTop() + delta;
        myView = view;
    }

    @Override
    public void onAnimationEnd(Animation animation)
    {}

    @Override
    public void onAnimationRepeat(Animation animation)
    {}

    @Override
    public void onAnimationStart(Animation animation)
    {

        LayoutParams lp = (LayoutParams) myView.getLayoutParams();
        lp.leftMargin = toX;
        lp.topMargin = toY;
        myView.setLayoutParams(lp);
        myView.layout(toX, toY, 0, 0);      
    }
}

My custom TranslateAnimation does move a view vertically. It is fine besides a strange short flickering in the beginning. It seems that it is only one visible frame were the view flashes at a unexpected position (much higher then the animation should start).

Note: That flickering doesn't happen when i call super(0,0,0,0) but then there is no animation.

Here is a short version of my code:

public class ExTranslateAnimation extends TranslateAnimation implements AnimationListener
{
    private View myView;
    public ExTranslateAnimation (...)
    {
        // delta is how much it gets moved
        super(0, 0, -delta, 0);
        this.setAnimationListener(this);
        this.setDuration(duration);
        toY =  view.getTop() + delta;
        myView = view;
    }

    @Override
    public void onAnimationEnd(Animation animation)
    {}

    @Override
    public void onAnimationRepeat(Animation animation)
    {}

    @Override
    public void onAnimationStart(Animation animation)
    {

        LayoutParams lp = (LayoutParams) myView.getLayoutParams();
        lp.leftMargin = toX;
        lp.topMargin = toY;
        myView.setLayoutParams(lp);
        myView.layout(toX, toY, 0, 0);      
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抹茶夏天i‖ 2024-11-06 03:21:09

它发生在模拟器还是设备上..因为我曾经遇到过同样的问题,但仅限于设备。在模拟器上它工作正常。

Is it happening on emulator or device.. because i faced the same issue once but it was only in case of device. On emulator it was working fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文