如何动画的动作条的ActionMode的背景是什么? [英] How to animate background of ActionMode of the ActionBar?

查看:184
本文介绍了如何动画的动作条的ActionMode的背景是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

有可能改变动作条的背景,和两种颜色之间甚至动画,因为这样的:

 公共静态无效animateBetweenColors(最后的动作条动作条,最终诠释colorFrom,最终诠释colorTo,
        最终诠释durationInMs){
    最后ValueAnimator colorAnimation = ValueAnimator.ofObject(新ArgbEvaluator(),colorFrom,colorTo);
    colorAnimation.addUpdateListener(新AnimatorUpdateListener(){
        ColorDrawable colorDrawable =新ColorDrawable(colorFrom);

        @覆盖
        公共无效onAnimationUpdate(最终ValueAnimator动画){
            colorDrawable.setColor((整数)animator.getAnimatedValue());
            actionBar.setBackgroundDrawable(colorDrawable);
        }
    });
    如果(durationInMs> = 0)
        colorAnimation.setDuration(durationInMs);
    colorAnimation.start();
}
 

问题

我不能找到一种方式来获得的动作模式的观点,这样我就可以改变它的背景上某些情况下(而它的显示)。

我试过

我唯一发现是假设作用模式的ID将保持不变,甚至这样的工作只是为完成按钮(就是那个看起来像的观点一个黑客-Y方式V,实际上更像是取消)。

我还发现了如何通过主题改变它,但是这不是我所需要的,因为我需要以编程方式做到这一点。

问题

我如何获得actionMode,或者更$ P $的观点pcisely,我怎么可以使用动画改变它的背景?

解决方案
  

我如何获得actionMode,或者更$ P $的观点pcisely,哪能   使用动画改变它的背景是什么?

您有两个选择,遗憾的是这两者都不涉及本地 ActionMode 的API:

的<一个href="https://github.com/android/platform_frameworks_base/blob/dbc51de44fe4f9a7f81528204250de32ec405d39/core/java/com/android/internal/widget/ActionBarContextView.java"><$c$c>ActionBarContextView负责控制 ActionMode

  1. 使用 Resources.getIdentifier 来调用 Activity.findViewById ,并通过在系统中使用了标识<$ C C $> ActionBarContextView
  2. 使用反射来访问到字段的<一个href="https://github.com/android/platform_frameworks_base/blob/dbccd44a638ae8705a5b14bff8b2dd74abc26045/core/java/com/android/internal/app/ActionBarImpl.java#L81"><$c$c>ActionBarImpl

下面是两个例子:

使用 Resources.getIdentifier

 私人无效animateActionModeViaFindViewById(INT colorFrom,诠释colorTo,INT持续时间){
    最终诠释际= getResources()则getIdentifier(action_context_bar,ID,机器人)。
    animateActionMode(findViewById(际),colorFrom,colorTo,持续时间);
}
 

使用反射

 私人无效animateActionModeViaReflection(INT colorFrom,诠释colorTo,INT持续时间){
    最后的动作条动作条= getActionBar();
    尝试 {
        最后一个字段contextView = actionBar.getClass()getDeclaredField(mContextView)。
        animateActionMode((查看)contextView.get(动作条),colorFrom,colorTo,持续时间);
    }赶上(最终异常忽略){
        // 没事做
    }
}
 


 私人无效animateActionMode(最终查看actionMode,从最终诠释,诠释为,INT持续时间){
    最后ValueAnimator VA = ValueAnimator.ofObject(新ArgbEvaluator(),从,到);
    最后ColorDrawable actionModeBackground =新ColorDrawable(从);
    va.addUpdateListener(新AnimatorUpdateListener(){

        @覆盖
        公共无效onAnimationUpdate(最终ValueAnimator动画){
            actionModeBackground.setColor((整数)animator.getAnimatedValue());
            actionMode.setBackground(actionModeBackground);
        }

    });
    va.setDuration(持续时间);
    va.start();
}
 

结果

下面是结果从 Col​​or.BLACK 动画为 Col​​or.BLUE 的<$的持续时间的GIF C $ C> 2500 :

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?

解决方案

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

You have two choices, unfortunately neither of which involve native ActionMode APIs:

The ActionBarContextView is responsible for controlling the ActionMode

  1. Use Resources.getIdentifier to call Activity.findViewById and pass in the ID the system uses for the ActionBarContextView
  2. Use reflection to access to Field in ActionBarImpl

Here's an example of both:

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

Using reflection:

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

Here's a gif of the results animating from Color.BLACK to Color.BLUE at a duration of 2500:

这篇关于如何动画的动作条的ActionMode的背景是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆