简体   繁体   中英

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 :

    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. My intention is to move the whole linear layout up the screen to the height where the logo previously was (20% of screen height). 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. Notice you never attach the animation to the view itself. Instead of login_anim.start() try calling login.startAnimation(login_anim) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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