我正在创建一个应用程序,其资源可以重复使用(因为按钮总是相同,但是镜像或旋转)。我确实想要使用相同的资源,所以我不必添加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>
我想重复使用drawable,因为它会相同但旋转90º和45º并且我将按钮分配为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" />
我知道我可以用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>
我可以在XML中使用rotate:
<?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定义的旋转动画。使用fromDegrees
,您可以定义初始旋转状态。 toDegrees
是动画序列中drawable的最终旋转状态,但如果您不想使用动画,则可以是任何内容。
我不认为它为动画分配资源,因为它不必作为动画加载。作为drawable,它呈现为初始状态,应该放在drawable
资源文件夹中。要将它用作动画,你应该将它放在anim
资源文件夹中,并可以像这样开始动画(只是一个例子):
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
rotation.setRepeatCount(Animation.INFINITE);
myView.startAnimation(rotation);
如果使用基于矢量的drawable,结合ImageView,样式和颜色状态列表,您的按钮可以重构如下:
注意:矢量drawables明显小于图像,因此额外的显式定义不会产生太多开销,并且会产生清晰明确的代码(尽管我已经读过应该避免修改矢量资产的手,我宁愿处理更新几个文件的开销,而不是在一个文件上进行转换):
注意:Android Studio是矢量资产的绝佳来源。
水库\值\ styles.xml
<!--ImageView-->
<style name="Details_Buttons_Top_Left_Button">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:tint">@color/button_csl</item>
</style>
水库\颜色\ button_csl.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="@color/grey_disabled"/>
<item android:state_pressed="true" android:color="@color/orange_hilite"/>
<item android:color="@color/black"/>
</selector>
details_menu_large_button.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>
Details_Buttons_Top_Left_Button
<ImageView android:id="@+id/Details_Buttons_Top_Left_Button"
style="@style/Details_Buttons_Top_Left_Button"
android:src="@drawable/details_menu_large_button" />
and_card_details_button_down_left.xml(ic_play_arrow_black_24dp.xml)
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M8,5v14l11,-7z"/>
</vector>
and_card_details_button_down_left_onclick.xml(修改了ic_play_arrow_black_24dp.xml)
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<group android:name="rotationGroup"
android:pivotX="12"
android:pivotY="12"
android:rotation="90" >
<path
android:fillColor="#FF000000"
android:pathData="M8,5v14l11,-7z"/>
</group>
</vector>