Is it possible to rotate a drawable in the xml description?

后端 未结 4 695
天涯浪人
天涯浪人 2020-12-08 00:12

I am creating an app, with resources that can be reused (because buttons are always the same, but mirrored or rotated). I do want to use the same resource so I don\'t have t

相关标签:
4条回答
  • 2020-12-08 00:17

    I could rotate left arrow right in XML as:

    <?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>
    

    Attached image for reference.

    0 讨论(0)
  • 2020-12-08 00:17

    If vector based drawables are used, in conjunction with an ImageView, style, and color state list, your button can be refactored as follows:

    Note: Vector drawables are significantly smaller than images, so extra, explicit definitions don't incur much overhead, and makes for clear, explicit code (although I've read that hand modifying vector assets should be avoided, I'd rather deal with the overhead of updating a couple of files than having transformations on one):

    Note: Android Studio is a great source for vector assets.

    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 modified)

    <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>
    
    0 讨论(0)
  • 2020-12-08 00:20

    If you want to rotation drawable in xml file then simple add android:rotation="180" in ImageView

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_dropdown"
        android:rotation="180"/>
    
    0 讨论(0)
  • 2020-12-08 00:23

    I could rotate in 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>
    

    The fromDegrees is important.

    Basically this is a rotate animation defined in XML. With fromDegrees you define the initial rotated state. The toDegrees is the final rotated state of the drawable in the animation sequence but can be anything if you don't want to use animation.

    I don't think it allocates resources for animation as it doesn't have to be loaded as animation. As a drawable it is rendered as it's initial state and should be put in the drawable resource folder. To use it as an animation you should put it in anim resource folder and can start the animation like this (just an example):

    Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
    rotation.setRepeatCount(Animation.INFINITE);
    myView.startAnimation(rotation);
    
    0 讨论(0)
提交回复
热议问题