简体   繁体   English

翻译动画无法与OnClickListener一起使用

[英]Translate Animation Not Working with OnClickListener

I am using the following translate animation to bring an imageView onto the screen in the onCreate method of my activity : 我使用下面的翻译动画带来imageView到在屏幕onCreate我的活动的方法:

    mScanner = (ImageView)findViewById(R.id.logo_img);
    Display display = getWindowManager().getDefaultDisplay();
    final int height =  display.getHeight();
    mAnimation = new TranslateAnimation(0, 0, -300, height * 2/10);
    mAnimation.setDuration(2500);
    mAnimation.setFillAfter(true);
    mScanner.setAnimation(mAnimation);
    mScanner.setVisibility(View.VISIBLE);

This works fine. 这很好。 Now I have two buttons at the bottom of my screen inside of a linear layout, like this : 现在,在屏幕底部的线性布局中有两个按钮,如下所示:

<LinearLayout
    android:orientation="vertical"
    android:layout_gravity="bottom"
    android:layout_height="0px"
    android:layout_weight="25"
    android:weightSum="100"
    android:layout_width="fill_parent">
    <Button
        android:layout_height="0px"
        android:layout_weight="50"
        android:id="@+id/log_in_btn"
        android:background="@drawable/btn_back"
        android:text="LOG IN"
        android:onClick="logIn"
        android:textSize="25dp"
        android:textColor="#FFFFFF"
        android:layout_width="fill_parent">

    </Button>
    <Button
        android:layout_height="0px"
        android:layout_weight="50"
        android:textColor="#FFFFFF"
        android:background="@drawable/btn_back"
        android:textSize="25dp"
        android:text="SIGN UP"
        android:layout_width="fill_parent">

    </Button>
</LinearLayout>

I added the following onClickListener to the first button. 我将以下onClickListener添加到第一个按钮。 My intention is to move the whole linear layout up the screen to the height where the logo previously was (20% of screen height). 我的意图是将整个线性布局在屏幕上移动到徽标以前的高度(屏幕高度的20%)。 However, when I press the button nothing happens. 但是,当我按下按钮时,什么也没有发生。

 Button button = (Button) findViewById(R.id.log_in_btn);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LinearLayout login = (LinearLayout) view.getParent();
            login_anim = new TranslateAnimation(0,0,0, height* 2/10);
            login_anim.setDuration(2500);
            login_anim.setFillAfter(true);
            login.setAnimation(login_anim);
            login_anim.start();


        }

    });

Your problem is that you aren't actually calling the animation on the LinearLayout. 您的问题是您实际上没有在LinearLayout上调用动画。 Notice you never attach the animation to the view itself. 注意,您永远不会将动画附加到视图本身。 Instead of login_anim.start() try calling login.startAnimation(login_anim) instead. 取而代之的login_anim.start()尝试调用login.startAnimation(login_anim)来代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM