Android 显示自动适合的闪屏图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12607756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show splash screen image with auto fit
提问by Telavian
I am using the following style to display a splash screen for an android application written in MonoDroid. However, it seems to take the image and maximize it to fit the whole screen while messing up the aspect ratio. Thus, the image looks huge and awful.
我正在使用以下样式为用 MonoDroid 编写的 android 应用程序显示启动画面。然而,它似乎拍摄图像并将其最大化以适应整个屏幕,同时弄乱了纵横比。因此,图像看起来巨大而可怕。
Is there a way to get it to maximize, but maintain the aspect ratio so it still looks good?
有没有办法让它最大化,但保持纵横比让它看起来仍然不错?
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowBackground">@drawable/splashscreenimage</item>
<item name="android:windowNoTitle">true</item>
</style>
This is the activity in C# which creates the splash screen and goes to the login.
这是 C# 中的活动,它创建初始屏幕并转到登录。
[Activity(MainLauncher = true, Theme = "@style/Theme.Splash", NoHistory = true)]
public class SplashScreenActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Start our real activity
StartActivity(typeof(LoginActivity));
}
}
回答by profexorgeek
There are several types of drawables in Android including actual bitmaps, nine-patch, and XML files. Try wrapping your image file with an XML file that gives it attributes and then use the XML file as drawable instead of the source image.
Android 中有几种类型的可绘制对象,包括实际位图、九个补丁和 XML 文件。尝试使用赋予它属性的 XML 文件包装图像文件,然后将 XML 文件用作可绘制对象而不是源图像。
Let's assume your xml file is called scaled_background and your original image is simply background.png:
让我们假设您的 xml 文件名为 scaled_background 并且您的原始图像只是 background.png:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:src="@drawable/background" />
Instead of setting your background to reference @drawable/background
you'd set it to reference the XML file: @drawable/scaled_background
in this example. Details on the scaling modes are mentioned in the Drawable documentation.
不是将背景设置为引用,@drawable/background
而是将其设置为引用 XML 文件:@drawable/scaled_background
在此示例中。Drawable 文档中提到了有关缩放模式的详细信息。
回答by Kaushik NP
It seems to take the image and maximize it to fit the whole screen while messing up the aspect ratio. Thus, the image looks huge and awful.
Is there a way to get it to maximize, but maintain the aspect ratio so it still looks good?
它似乎拍摄图像并将其最大化以适应整个屏幕,同时弄乱了纵横比。因此,图像看起来巨大而可怕。
有没有办法让它最大化,但保持纵横比让它看起来仍然不错?
The reason the image gets enlarged is because the image you use has higher pixelsthan the resolution of the device screen you are testing the app in. For solving this, the best method is to create images of proper size (pixels) for different device screenand then put these in drawablefiles (drawable-ldpi,drawable-mdpi,drawable-hdpi,etc) accordingly.
图像被放大的原因是因为您使用的图像的像素高于您正在测试应用程序的设备屏幕的分辨率。为了解决这个问题,最好的方法是为不同的设备屏幕创建适当大小(像素)的图像然后将它们相应地放入可绘制文件(drawable-ldpi、drawable-mdpi、drawable-hdpi 等)中。
Following is a list of minimum screen sizefor various displays as provided by Google (all in pixels) :-
以下是Google 提供的各种显示器的最小屏幕尺寸列表(均以像素为单位):-
For Android Mobile Devices
对于Android 移动设备
LDPI- 426 x 320
LDPI- 426 x 320
MDPI- 470 x 320
MDPI- 470 x 320
HDPI- 640 x 480
HDPI- 640 x 480
XHDPI- 960 x 720
XHDPI- 960 x 720
For Android Tablet Devices
对于安卓平板设备
LDPI- 200 x 320
LDPI- 200 x 320
MDPI- 320 x 480
MDPI- 320 x 480
HDPI- 480 x 800
HDPI- 480 x 800
XHDPI- 720 x 1280
XHDPI- 720 x 1280
Now create an XML drawablein res/drawable which will provide the splash activity's backgroundin the theme(Theme.Splash) using layer-list
. The image is loaded in <bitmap>
and is centeraligned using the gravity attribute.
现在创建一个XML绘制在res /绘制,这将提供飞溅活动的背景中使用的主题(Theme.Splash) layer-list
。图像被加载<bitmap>
并使用重力属性居中对齐。
<!-- background_splash.xml -->
<?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/splashscreenimage"/>
</item>
</layer-list>
Now modify your style(Theme.Splash)and set background_splash.xmlas your splash activity's backgroundin the theme.
现在修改您的样式(Theme.Splash)并将background_splash.xml设置为主题中您的启动活动的背景。
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowBackground">@drawable/background_splash.xml</item>
<item name="android:windowNoTitle">true</item>
</style>
With that, the image should be set at center with aspect ratio maintained.
这样,图像应设置在中心并保持纵横比。
Note:For image resizing, you can use Adobe Photoshop (I find it easier to work with). A bit of experimenting will have to be done to get the correct image size you want for the splash screen.
注意:要调整图像大小,您可以使用 Adobe Photoshop(我发现它更易于使用)。必须进行一些实验才能获得您想要的初始屏幕的正确图像大小。
According to preference, you may also use 9 Patch imageso that the image's border can stretch to fit the size of the screen without affecting the static area of the image.
根据喜好,您也可以使用9 Patch 图像,这样图像的边框可以拉伸以适应屏幕的大小,而不会影响图像的静态区域。
Reference Links :
参考链接:
Building Splash Screen:https://plus.google.com/+AndroidDevelopers/posts/Z1Wwainpjhd
构建启动画面:https : //plus.google.com/+AndroidDevelopers/posts/Z1Wwainpjhd
Splash Screen Image sizes:android splash screen sizes for ldpi,mdpi, hdpi, xhdpi displays ? - eg : 1024X768 pixels for ldpi
启动画面图像尺寸:ldpi、mdpi、hdpi、xhdpi 显示的 android 启动画面尺寸?- 例如:ldpi 为 1024X768 像素