是否有可能在xml描述中旋转drawable?

我正在创build一个应用程序,可以重复使用的资源(因为button总是相同的,但镜像或旋转)。 我想要使​​用相同的资源,所以我不必添加3个更多的资源,就像原来的,但旋转。 但是我也不想把代码和XML中可以声明的东西混合起来,或者用一个会花费处理时间的matrix进行转换。

我有一个XML中声明的两个状态button。

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/and_card_details_button_down_left_onclick" /> <!-- pressed --> <item android:drawable="@drawable/and_card_details_button_down_left" /> <!-- default --> </selector> 

我想重复使用drawable,因为它将会是相同的,但是旋转了90º和45º,我把这个button分配给drawable。

 <Button android:id="@+id/Details_Buttons_Top_Left_Button" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/details_menu_large_button" /> 

我知道我可以用RotateDrawableMatrix来旋转它,但正如我已经解释过的,我不喜欢这种方法。

是否有可能直接在XML上实现,或者你认为这将是最好的方法吗? 把所有的资源,但旋转,旋转他们的代码?

—编辑— @dmaxi的答案很好,这是如何将它与一个项目列表:)

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <rotate android:fromDegrees="90" android:toDegrees="90" android:pivotX="50%" android:pivotY="50%" android:drawable="@drawable/and_card_details_button_up_onclick"/> </item> <item> <rotate android:fromDegrees="90" android:toDegrees="90" android:pivotX="50%" android:pivotY="50%" android:drawable="@drawable/and_card_details_button_up_onclick"/> </item> </selector> 

我可以在XML中旋转 :

 <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="90" android:toDegrees="90" android:pivotX="50%" android:pivotY="50%" android:drawable="@drawable/mainmenu_background"> </rotate> 

fromDegrees是重要的。

基本上这是一个用XML定义的旋转animation。 用fromDegrees定义初始的旋转状态。 toDegrees是animation序列中drawable的最终旋转状态,但如果不想使用animation,则可以是任何东西。

我不认为它为animation分配资源,因为它不必作为animation加载。 作为一个可绘制的呈现,因为它的初始状态,应放在drawable资源文件夹。 要将其用作animation,您应该将其放置在anim资源文件夹中,并可以像这样启动animation(只是一个示例):

 Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation); rotation.setRepeatCount(Animation.INFINITE); myView.startAnimation(rotation); 

我可以在XML中向右旋转左边的箭头:

 <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="180" android:toDegrees="0" android:drawable="@drawable/left"> </rotate> 

附上图片供参考。

在这里输入图像说明