TranslateAnimation 后按钮不可点击 [英] Button is not clickable after TranslateAnimation

查看:42
本文介绍了TranslateAnimation 后按钮不可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在点击时移动按钮(带动画).我希望它在第一次点击时向底部移动 100 像素,在第二次点击时向上移动 100 像素,在第三次点击时向底部移动 100 像素,依此类推.我有简单的布局文件(main.xml):

I'm trying to move button (with animation) upon click. I want it to move 100 pixels to the bottom on first click, 100 pixels up on second, 100 pixels to the bottom on third and so on. I have simple layout file (main.xml):

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Press to begin animation" />

我的启动活动如下:

public class TestActivity extends Activity {
public final String TAG="TestActivity";
boolean toTop=false;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    Button b=(Button)findViewById(R.id.button);
    b.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Toast.makeText(TestActivity.this, "left="+v.getLeft()+"\nright="+v.getRight(), Toast.LENGTH_SHORT).show();

            Animation translateAnimation;
            if(toTop) translateAnimation=new TranslateAnimation(0, 0, 0, -100); 
            else translateAnimation=new TranslateAnimation(0, 0, 0, 100);
            translateAnimation.setDuration(1000);
            translateAnimation.setFillEnabled(true);
            translateAnimation.setFillAfter(true);

            v.startAnimation(translateAnimation);
            toTop=!toTop;
        }
    });
}
}

当我按下按钮时,我可以看到它移动到底部.但是当我第二次按下它时,没有任何反应.我必须单击按钮的初始矩形才能再次开始动画.似乎按钮已按预期绘制,但实际视图仍保留在同一位置.我想知道如何完全移动视图,而不仅仅是它的视觉部分.此外,我使用 Toast.maketext.show 来确保按钮的坐标不会从点击到点击发生变化.

When I press the button, I can see it moving to the bottom. But when I press it for the second time, nothing happens. I have to click to initial rectangle of the button to begin animation again. It seems like button is drawn as expected, but actual view is remained on the same position. I want to know how I can move a view entirely, not just its visual part. Besides, I use Toast.maketext.show to ensure that coordinates of the button aren't changed from click to click.

推荐答案

是的,这是正常行为.这是因为动画只是重新渲染 View 的像素,但它在显示器上的位置保持不变.如果你想把你的 View 重新定位到你的动画结束的地方,你需要调用 View.layout() 方法并传递 4 个参数,这些参数描述了 查看在其布局上的新位置.请记住,View.layout() 获取相对于 View 的父级的参数.

Yes, this is normal behavior. This is because animation just rerenders View's pixels, but it's position on the display remains the same. If you want to relocate your View to the place where your animation ends, you need to call View.layout() method and pass there 4 parameters, which describe Views new position on it's layout. Keep in mind that View.layout() gets params relative to Views parent.

这篇关于TranslateAnimation 后按钮不可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆