Android - Fade in / Fade out ActionBar Element when to show / hide

Is there any easy way to animate an ActionBar element using Fade In / Fade out animations when showing / hiding? Maybe with something like this:

@Override public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.menu, menu); if (visible) { menu.findItem(R.id.randomItemID).setVisible(true); else { menu.findItem(R.id.randomItemID).setVisible(false); } return true; } private void showHideActionItem() { if (visible) { // Fade Out animation here visible = false; invalidateOptionsMenu(); } else { // Fade In animation here visible = true; invalidateOptionsMenu(); } } 

Thank you, Tony.

+6
source share
1 answer

I found this question really interesting, so I decided to find a solution (so forgive me if this solution is not as optimal as it may be, this is just an example to show the way).

After some research, I decided to use the Android timer : the global idea is to have a timer that updates the actionBar background at a regular interval (so to create the fade_in effect, I just need to keep the same background and change its opacity).

Here is my implementation:

First: my custom class, which will do most of the work:

 public class ToolbarAnimator { private final static String TAG = ToolbarAnimator.class.getSimpleName(); private final int ALPHA_MAX = 255;//just look at the documentation private final int NUMBER_OF_TICK = 255;//can go from 1 to 255, it the number of tick private final int ALPHA_PER_TICK = ALPHA_MAX / NUMBER_OF_TICK;//alpha we'll remove/add on every tick private long DELAY = 1000;//amount of time in milliseconds before animation execution. private final AppCompatActivity mActivity; /* ** Private field */ private ActionBar mActionBar; private Timer mTimer; private int mCurrentAlpha; private int mActionBarBackgroundColor; /* ** Constructor */ public ToolbarAnimator(@NonNull AppCompatActivity activity, @NonNull final ActionBar actionBar, final int actionBarBackgroundColor) { mActivity = activity; mActionBar = actionBar; mTimer = new Timer(); mCurrentAlpha = 0; mActionBarBackgroundColor = actionBarBackgroundColor; } /* ** Public method */ public void start(final long duration) { final long period = duration / NUMBER_OF_TICK;//time beetwen 2 run() call Log.d(TAG, "start"); Log.d(TAG, "delay = " + DELAY); Log.d(TAG, "period = " + period); Log.d(TAG, "duration = " + duration); Log.d(TAG, "alphaPerTick = " + ALPHA_PER_TICK); //init a timer which will updateActionBarColor on every each period mTimer.schedule(new TimerTask() { @Override public void run() { //update the actionBar updateActionBarColor(); } }, DELAY, period); } /* ** Private method */ private void updateActionBarColor() { //We have to go to the main thread for updating the interface. mActivity.runOnUiThread(new TimerTask() { @Override public void run() { //check if the animation is finish if (mCurrentAlpha > 255 || mCurrentAlpha < 0) { Log.d(TAG, "cancel timer"); mTimer.cancel(); mTimer.purge(); return; } //create the new backgroundColorDrawable final Drawable backgroundDrawable = new ColorDrawable(mActionBarBackgroundColor); backgroundDrawable.setAlpha(mCurrentAlpha); //apply the new color mActionBar.setBackgroundDrawable(backgroundDrawable); //upgrade alpha mCurrentAlpha += ALPHA_PER_TICK; } }); } } 

When you have this class, you can start the animation from any activity or fragment:

 @Override public boolean onCreateOptionsMenu(Menu menu) { //just inflate the actionBar getMenuInflater().inflate(R.menu.menu_main, menu); //Check if the supportActionBar has been enable final ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { //Start a 2s animation on the actionBar new ToolbarAnimator(this, actionBar, Color.RED).start(2 * 1000); } return true; } 

UPDATE:

I made an example application that implements much more functions (for example, you can choose fade_in or fade_out), you can find the source code here .

+1
source

Source: https://habr.com/ru/post/957448/


All Articles