我正在创建一个应用程序,其中包含可以重复使用的资源(因为按钮始终相同,但是镜像或旋转)。 我确实想要使用相同的资源,这样我就不必添加3个完全像原始资源但旋转的资源。 但是我也不想将代码与可以在XML中声明的内容混合,或者使用矩阵进行转换,这将耗费处理时间。
我在XML中声明了一个具有两种状态的按钮。
<?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>
我想重复使用这个可绘制对象,因为它将是相同的,只不过旋转了90度和45度,并且我将其分配给按钮作为一个可绘制对象。
<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" />
我知道可以使用 RotateDrawable
或者 Matrix
来旋转图片,但正如我之前解释的那样,我不喜欢这种方法。
是否可以直接在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>