2017-07-28 75 views
0

是否可以创建一个启动画面,其中的徽标可以保持其纵横比,而背景图像可以独立调整大小?我目前正在使用几个不同分辨率的.png文件,并且在大多数设备上看起来都很棒。不过,有几款手机严重扭曲了我的标志(即三星S8,移动垂直屏幕空间)。没有标志失真的Android启动画面

我可以处理我的闪屏背景的一些失真,但瘦/压扁的标志是不可接受的。有谁知道如何做到这一点?矢量可绘制的标志会比新的布局的.png更好吗?

+0

是你的形象(imageview)有重量和身高的wrap_content? – DroiDev

+0

不,它是match_parent。 – deMouser

+0

如果我明白,你有一个背景图像,然后在它上面的图像。和图像(你的图标)在某些设备上变形? – DroiDev

回答

1

如果您只有一个肖像图像为您的启动画面,这是一个解决方案,这是一个全屏的肖像,并居中,但不是在edit: 肖像 景观:

splash_screen.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layoutSplash" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@color/splash_screen_background"> 

    <ImageView 
     android:contentDescription="@string/splash_screen_description" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center_vertical" 
     style="@style/SplashScreen" /> 

</LinearLayout> 

值/ styles.xml

<style name="SplashScreen"> 
    <item name="android:src">@drawable/splash</item> 
    <item name="android:adjustViewBounds">true</item> 
    <item name="android:scaleType">fitCenter</item> 
</style> 

值端口/ styles.xml

<style name="SplashScreen"> 
    <item name="android:src">@drawable/splash</item> 
    <item name="android:adjustViewBounds">true</item> 
    <item name="android:scaleType">centerCrop</item> 
</style> 
1
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg" 
    android:gravity="center"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/logo"/> 

</RelativeLayout> 

这将做到这一点

enter image description here
这是在Nexus 10

enter image description here

0

无需根本不使用布局,使用LayeredList并设置i t作为背景。这是一个例子;

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@color/gray"/> 
    <item> 
     <bitmap android:gravity="center" android:src="@drawable/splash_image"/> 
    </item> 
</layer-list> 
1

在layout.xml中呈现splash视图而不是作为主题背景(与其他建议一样)将花费更长的时间来加载。这是因为应用程序的主题首先被加载,然后Activity被构造并且布局最后膨胀。由于启动画面的目的是在应用程序加载时显示某些内容,因此加载闪屏时延迟约0.5秒会导致明显的延迟。将splash添加为主题windowBackground效果更好(点击启动器图标后几乎立即渲染),但有一些限制需要解决。根据屏幕宽度定义多个徽标大小的当前方法是处理这种情况的正确方法。下面是一个例子的结构,它处理前,后的Android V21之间的差异:

值/的themes.xml:

<style name="SplashThemeCommon" parent="Theme.AppCompat.NoActionBar"> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowBackground">@drawable/splash_logo_theme</item> 
</style> 

<style name="SplashTheme" parent="SplashThemeCommon"> 
    <item name="android:windowFullscreen">true</item> 
</style> 

值-V21 /的themes.xml:

<style name="SplashTheme" parent="SplashThemeCommon"> 
    <item name="android:windowTranslucentStatus">true</item> 
    <item name="android:windowTranslucentNavigation">true</item> 
</style> 

抽拉/ splash_logo_theme.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <bitmap 
      android:gravity="center" 
      android:src="@drawable/splash_logo"/> 
    </item> 
</layer-list> 

接下来,您可以包含“splash_logo”的副本。。PNG”为不同的屏幕配置你有几个合理的选择这里一个详尽的方法可能包括针对每个屏幕尺寸不同的标识:

  • 绘制-sw320dp-xxxhdpi/splash_logo.png
  • 绘制-sw400dp-xxxhdpi /splash_logo.png
  • 抽拉-sw480dp-xxxhdpi/splash_logo.png
  • 抽拉-sw600dp-xxxhdpi/splash_logo.png
  • 抽拉-sw720dp-xxxhdpi/splash_logo.png
  • 抽拉-sw820dp-xxxhdpi/SPL ash_logo.png

“swXXXdp”表示XXX是dp中的最小屏幕尺寸(宽度或高度)。 820dp将是一个巨大的平板电脑,320dp将是一个旧的小型设备,今天大多数旗舰设备将在400dp类别。 450dp宽的设备将使用400dp图像。

为什么我在这里定义xxxhdpi?因为如果你没有指定,Android会认为它是一个mdpi图像,并尝试升级/调整高分辨率设备的大小(目前大多数旗舰手机都是xxhdpi)。这种放大将导致您的图像块状,并比原来的大。通过在这里指定xxxhdpi,Android只会尝试缩小图像尺寸,以确保图像不会变得块状;这是一个优化,所以我们不需要为ldpi,mdpi,hdpi,xhdpi等定义单独的图像。

虽然APK中有6个图像,但这种方式很浪费。我们可以进一步优化此方法,以便在应用中包含更少的图像并确保更小的APK大小。相反,让我们仅包括以下两个图像:

  • 抽拉-sw600dp-xxxhdpi/splash_logo.png
  • 抽拉-xxxhdpi/splash_logo.png

为600dp和更大(片剂)具有任何装置一个较大的图像副本以利用较大的屏幕空间,而所有其他手机使用较小的副本,并且没有屏幕宽度限定符。这里的缺点是,由于每个垃圾箱都覆盖了如此大范围的屏幕宽度,因此4英寸设备上的徽标将占用屏幕的大部分,而不是6英寸设备。这是减小APK大小的妥协方案,只要图像在最小的设备上永远不会被切断,大多数用户都不会注意到。

那么我们应该包括多大的png?它们需要在主题中显示特定大小,因为与layout.xml不同,我们无法在XML中指定宽度/高度(可绘制的宽度/高度支持仅在API 23中添加)。这意味着如果你包含一个非最优的PNG大小,它会显得太小或两个大;如果它太大,它实际上会被切断。一个好的计划,以获得正确尺寸是:

  • 只有默认的LinearLayout和Android Studio中不可见的内容
  • 打开它创建一个布局/的test.xml文件,并选择在布局编辑器的设计选项卡左下角
  • 更改主题SplashTheme在布局编辑器的顶部栏
  • 更改设备,以“像素XL”
  • 下发生绘制-sw400dp/splash_logo的图像。png
  • 在外部图像编辑器(如Photoshop或Gimp)中打开图像,更改大小并保存,然后检查Android Studio中的布局(您可能需要关闭并重新打开编辑器才能使更改生效)。重复,直到图像在布局中看起来大小正确
  • 按比例生成其他屏幕宽度的图像;例如sw320dp图像应该是320/400或400dp图像大小的67%,而720dp图像应该是720/400或400dp图像大小的180%。切记不要升级您的400dp图像以获得更大的图像,因为您会失去质量。相反,基于您的高品质原件
  • 将所有的图片上面
  • 在布局编辑器不同屏幕尺寸的选择设备(包括平板电脑)为指定的不同的绘制资源目录中生成的图像,并确保他们的标志对于所有屏幕尺寸都足够大但不会中断

您如何知道哪个设备屏幕分辨率属于哪个swXXXdp类别?这需要一个简单的计算。让我们看一下像素XL:

  • 屏幕尺寸是1440x2560
  • 因此1440
  • 屏幕dpi是534
  • 以英寸的屏幕宽度为五百三十四分之一千四百四十零= 2.69" 的最小尺寸是
  • 打开网站:http://angrytools.com/android/pixelcalc/
  • 输入在2.69英寸( “IN”)框中
  • 一个在左边会显示DP的绿框,在THI s案例431
  • 431 dp宽屏幕的Pixel XL将使用drawable-sw400dp/splash_logo.png图片(如果您拥有全部6个图片类别),或者使用drawable-xxxhdpi/splash_logo.png图片(如果您只有两个图像类别