我如何获得 actionMode 的视图,或者更准确地说,我如何才能
使用动画更改其背景?
你有两个选择,不幸的是,这两个选择都不涉及本地ActionMode
APIs:
The ActionBarContextView https://github.com/android/platform_frameworks_base/blob/dbc51de44fe4f9a7f81528204250de32ec405d39/core/java/com/android/internal/widget/ActionBarContextView.java负责控制ActionMode
- Use
Resources.getIdentifier
打电话Activity.findViewById
并传入系统使用的 IDActionBarContextView
- 使用反射来访问
Field
in ActionBarImpl https://github.com/android/platform_frameworks_base/blob/dbccd44a638ae8705a5b14bff8b2dd74abc26045/core/java/com/android/internal/app/ActionBarImpl.java#L81
这是两者的示例:
Using Resources.getIdentifier
:
private void animateActionModeViaFindViewById(int colorFrom, int colorTo, int duration) {
final int amId = getResources().getIdentifier("action_context_bar", "id", "android");
animateActionMode(findViewById(amId), colorFrom, colorTo, duration);
}
使用反射:
private void animateActionModeViaReflection(int colorFrom, int colorTo, int duration) {
final ActionBar actionBar = getActionBar();
try {
final Field contextView = actionBar.getClass().getDeclaredField("mContextView");
animateActionMode((View) contextView.get(actionBar), colorFrom, colorTo, duration);
} catch (final Exception ignored) {
// Nothing to do
}
}
private void animateActionMode(final View actionMode, final int from, int to, int duration) {
final ValueAnimator va = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
final ColorDrawable actionModeBackground = new ColorDrawable(from);
va.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(final ValueAnimator animator) {
actionModeBackground.setColor((Integer) animator.getAnimatedValue());
actionMode.setBackground(actionModeBackground);
}
});
va.setDuration(duration);
va.start();
}
Results
这是动画结果的 gifColor.BLACK
to Color.BLUE
在持续时间为2500
: