ClipDrawable getDrawable()返回StateListDrawable

时间:2014-12-20 23:28:49

标签: java android

我需要访问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>

当我尝试像ClipDrawable这样检索可绘制的left_arrow时:

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返回?

1 个答案:

答案 0 :(得分:2)

您的left_arrowStateListDrawable,因为它的根元素为<selector>。如果您希望将其作为ClipDrawable,则应将<clip>元素直接用作XML的根,而不使用<selector><item>标记。