【问题标题】:Is it possible to rotate a drawable in the xml description?是否可以在 xml 描述中旋转可绘制对象?
【发布时间】:2012-02-01 17:31:22
【问题描述】:

我正在创建一个应用程序,其资源可重复使用(因为按钮始终相同,但镜像或旋转)。我确实想使用相同的资源,因此我不必再添加 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" />

我知道我可以用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>

【问题讨论】:

  • 无需为你的英语道歉就可以了。欢迎来到 SO!
  • 在此线程stackoverflow.com/questions/14727426/… 上查看相同的问题,任何建议都会很棒!
  • 基于矢量的可绘制对象大大简化了问题(答案如下)。

标签: android android-layout drawable android-xml


【解决方案1】:

我可以在 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 资源文件夹中。 要将它用作动画,您应该将它放在anim 资源文件夹中,并且可以像这样启动动画(只是一个示例):

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

【讨论】:

  • 谢谢,太好了!我将它与项目及其正是我需要的东西结合起来,我想发布代码,我不知道编辑你的答案或我的问题是否更好......并且为了镜像图像我必须使用枢轴x & y?
  • 好吧,我很高兴能提供帮助,如果您愿意,可以编辑答案。 pivotX 和 pivotY 定义了旋转的中心点。对于镜像我不知道,因为这个 XML 只能定义 2D 旋转。
  • @dmaxi 这是通过旋转动画旋转可绘制对象,不是吗?那岂不是有点低效?
  • 我做了这个,但是从 0 到 360 度,因为我想要一个完整的旋转,问题是在小屏幕上它旋转变形,有什么线索吗?
  • Android M 中有一个 bug 影响了这个精确的旋转 drawable,它完全消失了,所以如果你选择这个解决方案,那么它会在 M 中被破坏。已为 N 修复。
【解决方案2】:

我可以将 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>

附上图片供参考。

【讨论】:

  • 我正在这样做,但我无法设置背景来布局任何想法?
【解决方案3】:

如果使用基于矢量的可绘制对象,结合 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>

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>

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>

【讨论】:

  • rotationGroup 属性的好答案,它可以很好地旋转矢量
【解决方案4】:

如果你想在xml 文件中绘制rotation,那么只需在ImageView 中添加android:rotation="180"

<ImageView
    android:id="@+id/imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_dropdown"
    android:rotation="180"/>

以编程方式

val image = findViewById(R.id.imageview)
image.rotation = 180f

【讨论】:

  • 谢谢!花了很长时间才找到这个答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-27
  • 2012-07-27
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多