Java 在Android中如何在直径相同的不同屏幕(设备)上绘制圆圈
我试图画一个圆圈,无论屏幕大小如何,每个安卓设备的直径都是2.5英寸(或任何其他值)。所有设备的直径大小必须相同,以下是我尝试过的 活动\u main.xmlJava 在Android中如何在直径相同的不同屏幕(设备)上绘制圆圈,java,android,android-imageview,android-shape,Java,Android,Android Imageview,Android Shape,我试图画一个圆圈,无论屏幕大小如何,每个安卓设备的直径都是2.5英寸(或任何其他值)。所有设备的直径大小必须相同,以下是我尝试过的 活动\u 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/too
<?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>
<?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>
椭圆形.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>
<?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),但效果也不好
那么,我如何才能在每部手机中实现相同大小圆圈的要求呢?//您正在为所有屏幕使用oval_shape.xml中的fix static size。这就是为什么它在不同的屏幕上是不同的。 你可以在dimens中使用这个。 //在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>
//正常情况下为2.5英寸,
正常值的1.5倍大(表示3.75英寸),
2x正常值,单位为xlarge(表示5in),
.75倍正常值(表示1.875英寸近似值)
//希望对您有所帮助。您可以使用
xdpi
和ydpi
来计算所需物理尺寸的正确像素数。@ElliottFrisch:-您能给我一个代码示例吗?您是否尝试过dp
?二角不能用于画布绘图!因此,在您的回答中,您再次给出了不同设备的不同尺寸。我到处都需要2.5分。