【问题标题】:How to animate background of ActionMode of the ActionBar?如何为ActionBar的ActionMode背景设置动画?
【发布时间】:2014-05-05 18:38:05
【问题描述】:

背景

可以更改操作栏的背景,甚至可以在两种颜色之间设置动画,例如:

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

问题

我找不到获取动作模式视图的方法,因此我可以在某些情况下更改其背景(当它显示时)。

我尝试了什么

我发现的唯一一件事是一种 hack-y 方式,它假设动作模式的 id 将保持不变,即使这仅适用于“完成”按钮的视图(看起来像“ V”,实际上更像是“取消”)。

我还找到了如何通过主题更改它,但这不是我需要的,因为我需要以编程方式进行。

问题

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

【问题讨论】:

标签: android background android-actionbar android-actionmode


【解决方案1】:

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

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

ActionBarContextView负责控制ActionMode

  1. 使用Resources.getIdentifier 调用Activity.findViewById 并传入系统用于ActionBarContextView 的ID
  2. 使用反射访问Field in ActionBarImpl

以下是两者的示例:

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

结果

这是在2500 的持续时间内从Color.BLACKColor.BLUE 的结果动画:

【讨论】:

  • 是否可以使用自定义视图,它将使用默认视图?
  • @androiddeveloper 不,ActionMode.setCustomView 只替换标题布局。
  • 标题布局?这只是显示的文本区域(图像中的“文本选择”)吗?
  • @androiddeveloper 是的。
  • 你可能会忘记 contextView.setAccessible(true);
猜你喜欢
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
相关资源
最近更新 更多