gpt4 book ai didi

java - Android - 显示/隐藏 ActionBar 项目时淡入/淡出

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:58 26 4
gpt4 key购买 nike

有什么简单的方法可以在显示/隐藏时使用淡入/淡出动画为 ActionBar 项目设置动画?也许是这样的:

@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();
}
}

谢谢,托尼。

最佳答案

我发现这个问题真的很有趣,所以我决定找到一个解决方案(所以请原谅我,如果这个解决方案不是最佳的,它只是一个展示方法的例子)。

经过一些研究,我决定使用 Android timer : 全局的想法是有一个计时器,它会定期更新 actionBar 背景(所以为了制作 fade_in 效果,我只需要保持相同的背景并改变它的不透明度)。

这里是我的实现:

首先:我的自定义类将完成大部分工作:

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;
}
});
}
}

当你有这个类时,你可以从任何 Activity 或 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;
}

更新:

我已经做了一个实现更多功能的示例应用程序(例如,您可以选择淡入淡出或淡入淡出),您可以找到源代码here .

关于java - Android - 显示/隐藏 ActionBar 项目时淡入/淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19796229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com