Get specific drawable from state list drawable

℡╲_俬逩灬. 提交于 2019-11-30 19:35:59

There is no public API to get the drawable from the state.

There are some methods in StateListDrawable but they are @hide with the comment "pending API council".

You can invoke them by reflection... but it's at your own risk !!!. (it may change in future releases)

Those methods are :

Here is how to proceed (exceptions omitted) :

int[] currentState = view.getDrawableState();
StateListDrawable stateListDrawable = (StateListDrawable)view.getBackground();
Method getStateDrawableIndex = StateListDrawable.class.getMethod("getStateDrawableIndex", int[].class);
Method getStateDrawable = StateListDrawable.class.getMethod("getStateDrawable", int.class);
int index = (int) getStateDrawableIndex.invoke(stateListDrawable,currentState);
Drawable drawable = (Drawable) getStateDrawable.invoke(stateListDrawable,index);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!