是否可以在 xml 描述中旋转 drawable? [英] Is it possible to rotate a drawable in the xml description?

查看:25
本文介绍了是否可以在 xml 描述中旋转 drawable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,其中的资源可以重复使用(因为按钮总是相同的,但被镜像或旋转).我确实想使用相同的资源,因此我不必再添加 3 个与原始资源完全相同但已轮换的资源.但我也不想将代码与可以在 XML 中声明的东西混合在一起,或者使用会花费处理时间的矩阵进行转换.

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.

我有一个在 XML 中声明的两个状态按钮.

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>

我想重用可绘制对象,因为它是相同的,但旋转了 90º 和 45º,我将按钮指定为可绘制对象.

and I want to reuse the drawable because 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" />

我知道我可以使用 RotateDrawable 或带有 Matrix 但正如我已经解释过的那样,我不喜欢这种方法.

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.

是否可以直接在 XML 上实现,或者您认为最好的方法是什么?把除了轮换的所有资源,在代码里轮换?

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?

--- 编辑---@dmaxi 的答案很好用,这是将它与项目列表结合的方法:)

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

推荐答案

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

fromDegrees 很重要.

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

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.

我认为它不会为动画分配资源,因为它不必作为动画加载.作为可绘制对象,它在初始状态下呈现,应放在 drawable 资源文件夹中.要将其用作动画,您应该将其放在 anim 资源文件夹中,并且可以像这样启动动画(仅作为示例):

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

这篇关于是否可以在 xml 描述中旋转 drawable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆