简体   繁体   English

Android drawable内部旋转按钮

[英]Android drawable rotate inside button

I need to have a button series with inside text and icon. 我需要一个带有内部文本和图标的按钮系列。 The icon inside the 4 buttons must rotate to cover every poly. 4个按钮内的图标必须旋转以覆盖每个多边形。

Follow an example with two buttons where the top is create with the original (vector drawable) icon and the "left" button rotating the vector. 举一个带有两个按钮的示例,其中两个按钮的顶部是原始图标(可绘制矢量的图标),而“左”按钮则是旋转矢量。

        <Button
            style="@style/Buttons.Small"
            android:drawableLeft="@drawable/ic_vertical_align_top_white_12dp"
            android:text="@string/optional" />

        <Button
            style="@style/Buttons.Small"
            android:drawableLeft="@drawable/arrow_left"
            android:text="@string/optional" />

The vector drawable: ic_vertical_align_top_white_12dp 可绘制的向量: ic_vertical_align_top_white_12dp

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
     android:width="12dp"
     android:height="12dp"
     android:viewportHeight="24.0"
     android:viewportWidth="24.0">
     <path
         android:fillColor="#FFFFFF"
         android:pathData="M8,11h3v10h2V11h3l-4,-4 -4,4zM4,3v2h16V3H4z" />    
 </vector>

The rotate drawable: arrow_left.xml 旋转可绘制对象: arrow_left.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/ic_vertical_align_top_white_12dp">
</rotate>

The style 样式

<style name="Buttons.Small">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@color/buttonSmallBackground</item>
    <item name="android:layout_margin">2dp</item>
    <item name="android:minHeight">0dp</item>
    <item name="android:minWidth">0dp</item>
    <item name="android:textSize">12sp</item>
    <item name="android:drawablePadding">4dp</item>
    <item name="android:paddingLeft">4dp</item>
    <item name="android:paddingRight">4dp</item>
</style>

And following the result 然后结果

Nougat (PERFECT) 牛轧糖 (完美)

在此处输入图片说明

Marshmallow (WRONG) 棉花糖 (错误)

在此处输入图片说明

I already tried to play with the vector viewport without success. 我已经尝试过使用矢量viewport但没有成功。

Thanks 谢谢

I solved with a trick. 我解决了一个难题。 Appling directly the rotation inside the vector. 直接将旋转应用于向量内部。

Now arrow_left.xml become 现在arrow_left.xml成为

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="12dp"
    android:height="12dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">
    <group android:rotation="-90"
        android:pivotX="12"
        android:pivotY="12">
        <path
            android:fillColor="#FFFFFF"
            android:pathData="M8,11h3v10h2V11h3l-4,-4 -4,4zM4,3v2h16V3H4z" />
    </group>
</vector>

I don't know why in Marshmallow the rotation not work properly but this solve my case. 我不知道为什么在棉花糖中旋转无法正常工作,但这解决了我的问题。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM