如何在Android XML中旋转线?

Don*_*mmy 18 android android-manifest android-layout android-xml android-drawable

我正在尝试使用XML在Android应用中绘制对角线,但它无效.它只是绘制一条水平线.

main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".TestActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" 
        style="@style/diagonalStyle">
    </RelativeLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="diagonalStyle">
        <item name="android:background">@drawable/background</item>
    </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

background.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <rotate
            android:fromDegrees="0"
            android:toDegrees="45"
            android:pivotX="50%"
            android:pivotY="50%" >
            <shape
                android:shape="line"
                android:top="1dip" >
                <stroke
                    android:width="1dip"
                    android:color="#FF0000" />
            </shape>
        </rotate>
    </item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

Hal*_*alR 43

你真的只需要一个数字更改就可以让它发挥作用.只需将fromDegrees更改为45:

<item>
    <rotate
            android:fromDegrees="45"
            android:toDegrees="45"
            android:pivotX="50%"
            android:pivotY="50%" >
        <shape
                android:shape="line"
                android:top="1dip" >
            <stroke
                    android:width="1dip"
                    android:color="#FF0000" />
        </shape>
    </rotate>
</item>
Run Code Online (Sandbox Code Playgroud)

旋转绘图http://developer.android.com/reference/android/graphics/drawable/RotateDrawable.html

实际上使用属性动画格式http://developer.android.com/guide/topics/resources/animation-resource.html

虽然你正在制作一个非动画对角线,但你希望它从45度开始,最后也是45度.所以将它们都设置为45是常态.