如何在 AnimatedStateListDrawable 中定义(以编程方式)过渡

How to define (programmatically) a transition in AnimatedStateListDrawable

新的可绘制选择器 AnimatedStateListDrawable 允许在选择器从一种状态变为另一种状态时定义动画。
在文档中,到目前为止在网络上,只有 XML 这个新 class.
定义的示例 我的问题是:我想以编程方式创建它:

    AnimatedStateListDrawable astld= new AnimatedStateListDrawable();
    astld.addState(KEY_STATE_PRESSED, pressedKey);
    astld.addState(StateSet.WILD_CARD, normalKey);
    astld.addTransition(??????????????);

但我找不到在哪里可以获取或设置转换所需的状态 ID(fromIdtoId)。

public void addTransition (int fromId, int toId, T transition, boolean reversible)
Added in API level 21
Adds a new transition between keyframes.

Parameters:
fromId Unique identifier of the starting keyframe
toId Unique identifier of the ending keyframe
transition An Animatable drawable to use as a transition, may not be null
reversible Whether the transition can be reversed

我终于发现有一个新方法addState() 具有不同的签名,其中包括 ID 的参数。

public void addState (int[] stateSet, Drawable drawable, int id)
Added in API level 21
Add a new drawable to the set of keyframes.

Parameters
stateSet An array of resource IDs to associate with the keyframe
drawable The drawable to show when in the specified state, may not be null
id The unique identifier for the keyframe