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

Goo*_*ead 99 android drawable android-layout android-xml

我正在创建一个应用程序,其资源可以重复使用(因为按钮总是相同,但是镜像或旋转).我确实想要使用相同的资源,所以我不必再添加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>
Run Code Online (Sandbox Code Playgroud)

我想重复使用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" />
Run Code Online (Sandbox Code Playgroud)

我知道我可以用一个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>
Run Code Online (Sandbox Code Playgroud)

小智 131

我可以用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>
Run Code Online (Sandbox Code Playgroud)

fromDegrees很重要.

基本上这是一个用XML定义的旋转动画.随着fromDegrees您定义的初始旋转状态.这toDegrees是动画序列中drawable的最终旋转状态,但如果您不想使用动画,则可以是任何内容.

我不认为它为动画分配资源,因为它不必作为动画加载.作为drawable,它呈现为初始状态,应该放在drawable资源文件夹中.要将它用作动画,你应该将它放在anim资源文件夹中,并可以像这样开始动画(只是一个例子):

Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
rotation.setRepeatCount(Animation.INFINITE);
myView.startAnimation(rotation);
Run Code Online (Sandbox Code Playgroud)


小智 30

我可以在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>
Run Code Online (Sandbox Code Playgroud)

附图供参考.

在此输入图像描述


sam*_*mis 17

如果将基于矢量的可绘制对象与ImageView,样式和颜色状态列表一起使用,则可以按以下方式重构按钮:

注意:矢量可绘制对象比图像要小得多,因此额外的显式定义不会产生太多开销,并且可以使代码清晰明了(尽管我读过应该避免手工修改矢量资产,但我宁愿处理更新几个文件的开销,而不是对一个文件进行转换):

注意: Android Studio是矢量资产的绝佳来源。

res \ values \ 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>
Run Code Online (Sandbox Code Playgroud)

res \ color \ 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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

  • “ rotationGroup”属性的不错答案,它可以很好地旋转矢量 (3认同)

Din*_*esh 6

如果你想在文件rotation中绘制,那么简单地添加xmlandroid:rotation="180"ImageView

<ImageView
    android:id="@+id/imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_dropdown"
    android:rotation="180"/>
Run Code Online (Sandbox Code Playgroud)

以编程方式

val image = findViewById(R.id.imageview)
image.rotation = 180f
Run Code Online (Sandbox Code Playgroud)