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

查看:144
本文介绍了按钮是不是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" />

我发起的活动如下:

My launching activity is as follows:

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;
        }
    });
}
}

当我preSS的按钮,我可以看到它移动到下方。但是,当我preSS是第二次,没有任何反应。我必须点击初始按钮的矩形重新开始动画。这似乎是被绘制按钮如预期的,但实际的观点被保持在相同的位置。我想知道我可以移动视图完全,不只是它的视觉部分。 此外,我使用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.layout()方法并传递有4个参数,它描述了查看 S上它新的位置的布局。请记住, View.layout()得到PARAMS相对于查看的母公司。

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天全站免登陆