2017-07-28 80 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圖片(如果您只有兩個圖像類別