2014-12-20 68 views
1

我需要訪問ClipDrawable來設置剪輯級別。這裏是繪製:ClipDrawable getDrawable()返回一個StateListDrawable

@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?

+0

它的確是一個statelistdrawable,其第一個元素是「你的drawable」 – rupps

回答

2

您的left_arrowStateListDrawable,因爲它有一個<selector>作爲它的根元素。如果您希望將其作爲ClipDrawable,則應將<clip>元素直接用作XML的根,而不要使用<selector><item>標籤。