首页 文章

如何在Android中在相同直径的不同屏幕(设备)上绘制圆圈

提问于
浏览
0

无论屏幕大小如何,我都试图在每个Android设备中绘制一个直径为2.5英寸(或任何其他值)的圆圈 . 所有设备的直径尺寸必须相同,以下是我的尝试 .

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@drawable/light"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="3in"
        android:layout_height="3in"
        android:src="@drawable/oval_shape"
        android:id="@+id/imageView"
        android:scaleType="matrix"/>

</RelativeLayout>

oval_shape.xml

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

    <solid
        android:color="#666666"/>

    <size
        android:width="2.5in"
        android:height="2.5in"/>
</shape>

这个代码的问题是当我在屏幕(真实手机)中使用4.5时,直径是2.5英寸,但是当在4英寸屏幕上测试直径小于2.5英寸时,它有一点点差异 . 我尝试使用毫米(mm),但它也不能很好地工作 .

那么如何在每部手机中达到相同尺寸圈的要求呢?

1 回答

  • -1

    //您正在使用oval_shape.xml中的修复静态大小来显示所有屏幕 . 这就是为什么不同屏幕的变化 . 你可以在尺寸中使用它 . //在oval_shape.xml中使用此代码

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
    
        <solid
            android:color="#666666"/>
    
        <size
            android:width="@dimen/inch_size"
            android:height="@dimen/inch_size"/>
    </shape>
    

    //你可以把dimens.xml放进去

    1) values-xlarge
    
    2) values-small
    
    3) values-normal
    
    4) values-large
    
    And give different sizes in dimens.xml within corresponding folders according to densities.
    

    //正常时为2.5in,正常值为1.5x(表示3.75in),xlarge中正常值的2x(表示为5in),小于正常值的0.75x(表示为aprox中的1.875)

    //希望它对你有帮助 .

相关问题