Problem with animation in Android Studio

Asked

Viewed 261 times

0

I created a Float Buttom to start an animation, but when I click on it, the animation does not occur. I have already done the debug to check the method that starts the animation and it occurs normally. I believe it may be some XML problem.

the XML of the animation:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator">

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="2000" >
    </alpha>

</set>

The Listener of Float Buttom in Main Activity:

botaoAjuda.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Animation animation = AnimationUtils.loadAnimation(MainActivity.this,
                R.anim.tutorialanim);
        animation.start();
        Log.d("anim: ",animation+"");
    }
});

and that Float Buttom is in a layout that I call on layout of Main Activity with include:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/helpButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:foregroundGravity="bottom"
    app:fabSize="normal"
    app:layout_anchor="@+id/include"
    app:layout_anchorGravity="bottom|right" />

The include na Main Activity:

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

2 answers

2

Instead of:

animation.start();

you must use:

botaoAjuda.startAnimation(animation);

Note that you did not assign any animation to the button, simply created a variable of type Animation within it.

  • I understood! but in my case, I would need to darken the whole screen and do not know how to call her to give this start

  • @Mikhaelaraujo It’s one thing to put action on the button, it’s another thing to get the button to perform an action. You need to set this in your question. I suggest you ask a question or edit this by further detailing your problem accordingly.

1

The problem is that your animation is not applied to any View, except in itself, which is not a view.

When you create an animation via XML, your goal is to animate some view belonging to your layout, and this will not occur if you do not point your animation to any of them.

Behold:

Animation animation = AnimationUtils.loadAnimation(MainActivity.this,
            R.anim.tutorialanim);

ImageView mView = (ImageView) findViewById(R.id.view22);
mView.startAnimation(animation); // uma view qualquer definida no seu layout
  • ah yes... understood now. but in case, I wanted to darken the whole screen

  • Use Objectanimation, with it you can easily make the fade effect on an object, the screen would darken little by little, recommit the video of Thiengo Calopsita on youtube, it talks about it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.