[英]Android - Fade in / Fade out ActionBar Item when show / hide it
Is there any simple way to animate an ActionBar Item with Fade In / Fade out animations when show / hide it? 是否有任何简单的方法可以在显示/隐藏动画时使用淡入/淡出动画来设置ActionBar项目的动画效果? 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();
}
}
Thanks, Tony. 谢谢,托尼。
I've found this question really interesting so I've decided to find out a solution (so forgive me if the solution, it's not as optimal as it can be, it's just an example for showing the way to go). 我发现这个问题非常有趣,所以我决定找出一个解决方案(如果解决方案原因,请原谅我,它不是最佳的,它只是展示方法的一个例子)。
After some research I've decided to use the Android timer : the global idea is to have a timer which will update the actionBar background on a regular interval (so into making a fade_in effect, I just have to keep the same background and change its opacity). 经过一些研究后,我决定使用Android计时器 :全球的想法是有一个计时器,它会定期更新actionBar背景(所以要制作一个fade_in效果,我只需要保持相同的背景并改变它不透明度)。
Here 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's 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've done a sample application which implements much more features (for example, you can choose fade_in or fade_out), you can find the source code here . 我已经完成了一个实现更多功能的示例应用程序(例如,您可以选择fade_in或fade_out),您可以在此处找到源代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.