是否有可能使用TransitionDrawable的动作条?有可能、动作、TransitionDrawable

2023-09-12 02:52:45 作者:那一夜的温柔

我想用TransitionDrawable做一些色彩动画操作栏上的。

I'm trying to do some color animation on the action bar by using a TransitionDrawable.

在code我试图为pretty的简单的onCreate期间,我把过渡绘制的动作条背景:

The code I'm trying is pretty simple, during onCreate, I put the transition drawable as the actionbar background:

Drawable d = getResources().getDrawable(R.drawable.actionbar);
actionbarDrawable = new TransitionDrawable(new Drawable[] { d, d });
getActionBar().setBackgroundDrawable(actionbarDrawable);

然后在事件中,我更换TransitionDrawable的第二绘制,并要求制作动画。

then on the event I replace the second drawable of the TransitionDrawable and ask to animate it.

    actionbarDrawable.setDrawableByLayerId(1, d);
    actionbarDrawable.startTransition(666);

我已经试过同一code。关于我的活动一个RelativeLayout的,它似乎很好地工作,任何想法,为什么动作条不希望合作,以及如何使它工作?

I've tried the same code on a RelativeLayout on my activity and it seems to work fine, any ideas why the ActionBar doesn't want to cooperate and how to make it work?

感谢。

推荐答案

试试看:

在值/字符串:

<color name="blue">#FF4682B4</color>
<color name="red">#FFC30F0F</color>

在你的类:

ColorDrawable blue = new ColorDrawable(getResources().getColor(R.color.blue));
ColorDrawable red = new ColorDrawable(getResources().getColor(R.color.red));
ColorDrawable[] color = {blue, red}; 
TransitionDrawable trans = new TransitionDrawable(color);
actionBar.setBackgroundDrawable(trans);
trans.startTransition(500);