为什么从事件调用时 view.startAnimation(animation) 不起作用?

发布于 2024-12-07 15:59:39 字数 1622 浏览 3 评论 0原文

我创建了一个自定义视图,它使用虚拟 TranslateAnimation 来设置一些布局属性。我使用 Interpolator 来计算高度,并将其应用到 TranslateAnimation 的 applyTransformation() 方法内的视图。

如果我从我的活动中触发动画,这工作得很好。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Log.i("test", "onCreate()");
    view.expand(); // This method starts the animation
}

当我尝试使用触摸事件执行相同操作时,没有任何反应。

@Override
// This method is touch handler of the View itself
public boolean onTouch(View v, MotionEvent event) {
    Log.i("test", "onTouch()");
    this.expand(); // onTouch is part of the view itself and calls expand() directly
    return true;
}

我的展开方法如下所示:

public void expand() {
    Log.i("test", "Expand!");

    TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 0) {

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            Log.i("test", "applyTransformation()");

            super.applyTransformation(interpolatedTime, t);

            // do something
        }

    };
    anim.setDuration(500);
    anim.setInterpolator(new AccelerateDecelerateInterpolator());
    this.someInternalView.startAnimation(anim);
}

创建活动后 Logcat 显示“onCreate()” 在我的触摸事件 Logcat 中显示“onTouch()” 在 Expand() 方法内部,Logcat 显示“Expand!” - 从活动或事件中调用。

在方法 applyTransformation() Logcat 中显示“applyTransformation()” - 但是!仅当从 onCreate() 调用 Expand() 时。任何尝试从事件启动动画的尝试都会失败。

在我看来,这像是某种线程问题。这可能是吗?我有什么遗漏的吗?据我从其他帖子中看到,从事件启动动画应该没有任何问题......

提前致谢!

I have created a custom view which uses an dummy TranslateAnimation to setup some layout properties. I use the Interpolator to calculate height, and apply it to a view inside the applyTransformation() method of the TranslateAnimation.

This is working quite well, if i trigger the animation from my Activity.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Log.i("test", "onCreate()");
    view.expand(); // This method starts the animation
}

When I try to do the same using a touch event, nothing happens.

@Override
// This method is touch handler of the View itself
public boolean onTouch(View v, MotionEvent event) {
    Log.i("test", "onTouch()");
    this.expand(); // onTouch is part of the view itself and calls expand() directly
    return true;
}

My expand method looks like this:

public void expand() {
    Log.i("test", "Expand!");

    TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 0) {

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            Log.i("test", "applyTransformation()");

            super.applyTransformation(interpolatedTime, t);

            // do something
        }

    };
    anim.setDuration(500);
    anim.setInterpolator(new AccelerateDecelerateInterpolator());
    this.someInternalView.startAnimation(anim);
}

Once my activity is created Logcat shows "onCreate()"
Inside my touch event Logcat shows "onTouch()"
Inside the expand() method Logcat shows "Expand!" - either called from the activity or from an event.

Inside the method applyTransformation() Logcat shows "applyTransformation()" - BUT! only if expand() is called from the onCreate(). Any attempt in trying to start the animation from an event failed.

This looks to me like some sort of threading problem. Could this be? Is there anything I am missing? As far as I see from other posts, starting animations from events should work without any problems...

Thanks in advance!

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

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

发布评论

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

评论(1

凉城凉梦凉人心 2024-12-14 15:59:39

试试这个:

public void expand() {
    Log.i("test", "Expand!");
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 0) {

                @Override
                protected void applyTransformation(float interpolatedTime, Transformation t) {
                    Log.i("test", "applyTransformation()");

                    super.applyTransformation(interpolatedTime, t);

                        // do something
                    }

                };
                anim.setDuration(500);
                anim.setInterpolator(new AccelerateDecelerateInterpolator());
                this.someInternalView.startAnimation(anim); 
            }
        });
    }

try this:

public void expand() {
    Log.i("test", "Expand!");
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 0) {

                @Override
                protected void applyTransformation(float interpolatedTime, Transformation t) {
                    Log.i("test", "applyTransformation()");

                    super.applyTransformation(interpolatedTime, t);

                        // do something
                    }

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