【问题标题】:Why does the center of the image vary from one Android device to another?为什么图像的中心因一台 Android 设备而异?
【发布时间】:2018-09-02 07:33:31
【问题描述】:

首先,这是style/splash_screen.xml

<layer-list xmlns:android="htt..." 
            android:opacity="opaque">

    <item
        android:drawable="@color/colorSplash"/>

    <item
        android:width="100dp"
        android:height="100dp"
        android:gravity="center">
        <bitmap
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="fill"
            android:src="@drawable/logo_512" />
    </item>

</layer-list>

下面的代码是layout/activity_splash.xml的一部分

<android.support.constraint.ConstraintLayout 
    xmlns:android="http...
    tools:context="com.example.SplashActivity">

<ImageView
    android:id="@+id/splash_logo"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/logo_512"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

我将徽标图像放在每个屏幕的中心

还有AndroidManifest.xml

<activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme">

当从启动器单击应用程序时,我让我的应用程序显示带有初始加载图像的初始屏幕。


但问题是,样式文件和布局文件的标志位置不同。


当我从 GalaxyS8 启动应用程序时,
初始加载屏幕的徽标图像低于启动屏幕,低至 statusbar_height

但是当我从我的 AVD 启动应用程序时,
初始加载屏幕的徽标图像高于启动屏幕之一,高于 statusbar_height

resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
statusbar_height = getResources().getDimensionPixelSize(resourceId);



我还是不明白,为什么在相同的代码、不同的设备上会出现这种差异。
我该如何解决这个问题?

PS:因为我需要在启动画面中添加动画,所以我需要让启动图像和启动画面都必须具有相同的徽标位置。

【问题讨论】:

    标签: android center splash-screen


    【解决方案1】:

    您可以使用 LinearLayout 代替 ConstraintLayout 并在 ImageView 中使用 layout_gravity 属性:

    <LinearLayout>
       <ImageView
          android:layout_gravity="center" />
     </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      您正在从 XML 加载图像。发生的情况是,Android 会根据您提供的资源为您缩放图像。

      另外,您使用的是 DP 尺寸,也就是密度像素,它们会受到设备屏幕密度的影响。

      在所有设备上获得相同结果的一种快速方法是从 resources/rawresources/nodpi 文件夹加载您的图像,并使用 PX 而不是 DP 大小。但是,非常不推荐这种解决方案。相反,您应该为每个应用程序支持的分辨率创建一个专用资源。另外,如果您想更有活力,可以使用vector graphics

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-15
        • 2016-09-14
        • 2018-10-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多