在XML中翻转Android图像

78

我想在xml中为按钮的背景翻转图像。我看过如何做这个的示例,但它是以编程方式实现的:http://xjaphx.wordpress.com/2011/06/26/image-processing-image-flipping-mirroring。 无论如何,我有一个如下的xml文件(button_left_state.xml):

<?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="180.0" android:toDegrees="180.0" 
    android:pivotX="50%" android:pivotY="50%" android:drawable="@drawable/buttonrightpressed" />    
</item>    

<item>
  <rotate android:fromDegrees="180.0" android:toDegrees="0.0" 
    android:pivotX="50%" android:pivotY="50%" android:drawable="@drawable/buttonright"/>
</item>   
</selector>

但是这段代码只能将图像旋转180度。在XML中是否有可能翻转图像?


你尝试过我的链接吗? - Raman
1
这个问题解决了吗?我也遇到了同样的问题。我有一个带有渐变的箭头按钮。我翻转了它,使箭头面向另一个方向,基本上使用相同的<rotate>代码。然而,现在我的渐变颜色是倒置的。 - Frank Sposaro
请看下文。我发布了答案。 - Nolesh
4个回答

304

在ImageView中使用比例属性。

android:scaleX="-1" //To flip horizontally or
android:scaleY="-1" //To flip vertically

19
预览时无法显示工作状态,但在设备上可以正常工作,谢谢。 - John
2
这太优雅了,帮助我解决了一个不对称小部件背景的RTL镜像问题。 - Mattias
2
这也会反转内容(如果是布局)。 - Gilian
阴影也是反转的。如果您将其用于 FAB 中,则阴影将出现在顶部。 - q126y
不错,谢谢。 - Matan Dahan

36

这是一个非常简短且易于理解的解决方案。

将以下内容添加到imageView中:

 android:rotationY="180"

这将水平翻转imageView(左<->右)。

要垂直翻转,使用以下代码:

android:rotationX="180"

示例:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="original image:"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/test"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="flip horizontally :"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rotationY="180"
        android:src="@drawable/test"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="flip vertically:"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rotationX="180"
        android:src="@drawable/test"/>

</LinearLayout>

这是结果(图像来自我制作的JNI库,可通过JNI进行操作):

输入图像描述


4
这只适用于对称图像。旋转并不等同于翻转/镜像一幅图像!! - muetzenflo
3
@muetzenflo 更新了答案,包括截图。请注意它确实有效。 - android developer
1
很酷,我没想到可以在三维轴上旋转。感谢你的努力。 - muetzenflo

14

我通过使用 layer-list 解决了我的问题:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
                <shape android:shape="rectangle">
                    <gradient android:startColor="#9f9" android:centerColor="#000"
                        android:endColor="#0f0" android:angle="-90" />
                    <stroke android:width="1.0px" android:color="#444" />
                    <corners android:bottomRightRadius="7dip"
                        android:bottomLeftRadius="0.1dp"
                        android:topLeftRadius="0.1dp"
                        android:topRightRadius="7dip"/>
                </shape>
            </item>
            <item>
                <rotate android:fromDegrees="180.0" android:toDegrees="180.0"
                    android:pivotX="50%" android:pivotY="50%" android:drawable="@drawable/arrow_right" />
            </item>
        </layer-list>
    </item>
</selector>

@LoungeKatt,首先看一下回答的日期。最好的答案是在我发布问题将近一年后给出的。 - Nolesh
看起来您同意如果在接受自己的答案之前提供了https://dev59.com/Hmox5IYBdhLWcg3wOx_9#20565940,那么它将成为被接受的答案,因此这对于未来的读者是有用的信息。 - Abandoned Cart

9
这是最简单和不需解释的答案,原答案链接:https://dev59.com/3VcP5IYBdhLWcg3w0tM0#43783080
这段代码是用于水平翻转的。
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24"
android:viewportWidth="24">

<group
    android:name="rotationGroup"
    android:pivotX="12"
    android:scaleX="-1" >

    <path
        android:fillColor="#FFFFFF"
        android:pathData="M15,14C17.67,14 23,15.33 23,18V20H7V18C7,15.33 12.33,14 15,14M15,12A4,4 0 0,1 11,8A4,4 0 0,1 15,4A4,4 0 0,1 19,8A4,4 0 0,1 15,12M5,9.59L7.12,7.46L8.54,8.88L6.41,11L8.54,13.12L7.12,14.54L5,12.41L2.88,14.54L1.46,13.12L3.59,11L1.46,8.88L2.88,7.46L5,9.59Z" />
</group>

如果您需要垂直翻转,请使用

<group android:pivotY ="half of viewPortHeight"
android:ScaleY =" -1">

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接