6

Background

It's possible to change the background of the actionbar, and even animate between two colors, as such:

public static void animateBetweenColors(final ActionBar actionBar, final int colorFrom, final int colorTo,
        final int durationInMs) {
    final ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
    colorAnimation.addUpdateListener(new AnimatorUpdateListener() {
        ColorDrawable colorDrawable = new ColorDrawable(colorFrom);

        @Override
        public void onAnimationUpdate(final ValueAnimator animator) {
            colorDrawable.setColor((Integer) animator.getAnimatedValue());
            actionBar.setBackgroundDrawable(colorDrawable);
        }
    });
    if (durationInMs >= 0)
        colorAnimation.setDuration(durationInMs);
    colorAnimation.start();
}

The problem

I can't find a way to get the view of the action mode, so that I could change its background on some cases (while it's showing).

What I tried

Only thing I found is a hack-y way which assumes that the id of the action mode will stay the same, and even this would work just for the view of the "done" button (the one that looks like an "V" and is actually more like "cancel").

I also found how to change it via themes, but that's not what I need, since I need to do it programmatically.

The question

How do I get the view of the actionMode, or, more precisely, how can I change its background using an animation?

4

1 回答 1

11

如何获得 actionMode 的视图,或者更准确地说,如何使用动画更改其背景?

您有两个选择,不幸的是,这两种选择都不涉及原生ActionModeAPI:

负责ActionBarContextView控制ActionMode

  1. 用于Resources.getIdentifier调用Activity.findViewById并传入系统用于ActionBarContextView
  2. 使用反射访问FieldinActionBarImpl

这是两者的示例:

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

结果

这是动画从Color.BLACKColor.BLUE持续时间为的结果的 gif 2500

例子

于 2014-05-05T00:13:47.940 回答