我需要访问 ClipDrawable
设置剪辑电平。这是可绘制对象:
@drawable/left_arrow
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<clip
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/rotated_square"
android:clipOrientation="horizontal"
android:gravity="left" />
</item>
</selector>
@drawable/rotated_square
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="45">
<shape
android:shape="rectangle">
<size
android:width="250dp"
android:height="250dp"/>
<solid
android:color="#ffffff"/>
</shape>
</rotate>
</item>
</selector>
当我尝试将可绘制对象 left_arrow
作为 ClipDrawable
检索时像这样:
ClipDrawable drawable = (ClipDrawable) getResources().getDrawable(R.drawable.left_arrow);
我在 LogCat 中收到以下错误:
Caused by: java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.ClipDrawable
at com.vcapra1.pingpongscoreboard.MainActivity.onCreate(MainActivity.java:89)
为什么我的ClipDrawable
作为 StateListDrawable 返回?
最佳答案
你的 left_arrow
是 StateListDrawable
因为它有一个 <selector>
作为它的根元素。如果你想把它作为 ClipDrawable
你应该使用 <clip>
元素直接作为 XML 的根而不使用 <selector>
和 <item>
标签。
关于java - ClipDrawable getDrawable() 返回一个 StateListDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27585329/