是否可以旋转的XML描述的可绘制?XML

2023-09-06 01:30:48 作者:苍白了誰的等待

我创建一个应用程序,与该可重复使用(因为按键始终是相同的,但镜像或旋转)资源。我也想用相同的资源,所以我不必再添加3更多的资源,这是完全一样的原始,但旋转。但我也不想混code与可以在XML中声明事物或做出变革与矩阵,这将花费的处理时间。

我有一个XML声明的两个国家的按钮。

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目的android:STATE_ pressed =真
          机器人:可绘制=@可绘制/ and_card_details_button_down_left_onclick/> <  - !pressed  - >
    <项目机器人:可绘制=@可绘制/ and_card_details_button_down_left/> <! - 默认 - >
< /选择器>
 

和我想重用绘制东阳这将是相同的,但旋转90度和45度,我分配到按钮作为绘制。

 <按钮机器人:ID =@ + ID / Details_Buttons_Top_Left_Button
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:背景=@可绘制/ details_menu_large_button/>
 

我知道,我可以用RotateDrawable或用 矩阵 但我已经说明我不喜欢这样的方式。

是否有可能实现直接对XML或者你觉得这将是做到这一点的最好方法是什么?把所有的资源,而且旋转,旋转它们在code?

英语不是我的母语,你可以很容易猜到,所以很抱歉关于这个问题的任何错误。

---编辑--- @dmaxi的答案的伟大工程,这是如何将其与项目列表结合:)

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <项目的android:STATE_ pressed =真正的>
        <旋转
        机器人:fromDegrees =90
        机器人:toDegrees =90
        机器人:pivotX =50%
        机器人:pivotY =50%
        机器人:可绘制=@可绘制/ and_card_details_button_up_onclick/>
    < /项目>

    <项目>
        <旋转
        机器人:fromDegrees =90
        机器人:toDegrees =90
        机器人:pivotX =50%
        机器人:pivotY =50%
        机器人:可绘制=@可绘制/ and_card_details_button_up_onclick/>
    < /项目>

< /选择器>
 
xml可扩展标记语言简单介绍

解决方案

我可以rotate在XML:

 < XML版本=1.0编码=UTF-8&GT?;
<旋转的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:fromDegrees =90
        机器人:toDegrees =90
        机器人:pivotX =50%
        机器人:pivotY =50%
        机器人:可绘制=@可绘制/ mainmenu_background>
< /旋转>
 

fromDegrees 是很重要的。

这基本上是在XML中定义一个旋转的动画。随着 fromDegrees 定义初始旋转的状态。该 toDegrees 在动画序列中绘制的最终旋转的状态,但可以是任何东西,如果你不希望使用动画。

我不认为它分配的动漫资源,它并没有被加载动画。作为绘制它呈现为它的初始状态,应放在绘制资源文件夹。 要使用它作为一个动画,你应该把它放在动画资源文件夹,可以这样开始动画(只是一个例子):

 动画旋转= AnimationUtils.loadAnimation(这一点,R.anim.rotation);
rotation.setRepeatCount(Animation.INFINITE);
myView.startAnimation(旋转);
 

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 to add 3 more resources that are exactly like the original but rotated. But I also don't want to mix the code with things that can be declared in the XML or make transformations with a matrix that will cost processing time.

I've got a two state button declared in an 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>

and i want to reuse the drawable beacause it will be the same but rotated 90º and 45º and i assign to the button as a 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" />

I know that I can rotate it with a RotateDrawable or with a Matrix but as I already explained I don't like that approach.

Is it possible to achieve that directly on the XML or what do you think that will be the best way to do it? Put all resources but rotated, rotate them in the code?

English is not my native language as you can easily guess, so sorry for any errors on the question.

--- EDIT --- The answer of @dmaxi works great, this is how to combine it with an item list :)

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

解决方案

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