React Native 之 Icon / 启动图设置

发布于 2024-03-09 10:21:14 字数 8078 浏览 73 评论 0

生成不同尺寸的启动图和图标

方式一

借助 Ionic 生成不同尺寸的启动图和图标

方式二

各种尺寸 Icon 图标生成

使用图标工厂生成 https://icon.wuruihong.com/

  1. 安卓下替换 andriod/app/src/main/res/ 下的 mipmap 文件即可
  2. iOS 下替换如下

各种尺寸启动图图标生成

启动图也可以使用图标工厂生产了 https://icon.wuruihong.com/splash

  1. 安卓下拷贝生成的文件到 andriod/app/src/main/res/ 目录下


  1. iOS 下拷贝生成的该文件夹替换即可

安卓端

修改图标和名称

找到根目录 /android/app/src/main/res

启动页

把生成好的启动页按这个格式处理即可

iOS 端

修改 app 名称

编辑 ios/test/Info.plist 文件

<key>CFBundleDisplayName</key>
- <string>$(PRODUCT_NAME)</string>
+ <string>测试程序</string>

修改应用图标

应用图标对尺寸有要求,比较简单地方式是准备一张 1024*1024 的图片,然后使用 图标工厂 在线生成

这里直接从 Sketch iOS 图标设计模板中选取了一张图片,生成后的结果如下


我们可以直接用生成好的内容替换默认的图标即可

替换 ios/test/Images.xcassets/AppIcon.appiconset 中的内容。如果不需要全部尺寸,可以用 XCode 打开项目,点击 Images.xcassets>AppIcon 拖入相应尺寸的图标

添加启动页

添加启动页可以使用 react-native-splash-screen 库,通过它可以控制启动页的显示和隐藏

$ yarn add react-native-splash-screen

$ react-native link react-native-splash-screen

Android

编辑 MainActivity.java ,添加显示启动页的代码

import android.os.Bundle; // here
import com.facebook.react.ReactActivity;
// react-native-splash-screen >= 0.3.1
import org.devio.rn.splashscreen.SplashScreen; // here
// react-native-splash-screen < 0.3.1
import com.cboy.rn.splashscreen.SplashScreen; // here

public class MainActivity extends ReactActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.show(this); // here
super.onCreate(savedInstanceState);
}
// ...other code
}

android/app/src/main/res/layout 文件夹下创建启动页布局文件 launch_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/launch_image">
</LinearLayout>

将启动页图片放置在 drawable 文件夹下

drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
  • Android 会自动缩放 drawable 下的图片,所以我们不必为所有分辨率的设备准备启动图
  • 完成上述操作后,重新打包应用,再启动时就可以看到启动页了。不过,启动页显示之前会有短暂的白屏,我们可以通过设置透明背景来处理。编辑 android/app/src/main/res/values/styles.xml 文件,修改如下
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
+ <item name="android:windowIsTranslucent">true</item>
</style>
</resources>

iOS

图标配置参考

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import "RNSplashScreen.h" // here

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...other code

[RNSplashScreen show]; // here
return YES;
}

@end

用 XCode 打开项目,选中 LaunchScreen.xib 中的 View ,取消选中 Use Launch Screen

选中项目,在 General 配置中设置 Launch Images Srouce ,点击 Use Asset Catalog ,弹出对话框中使用默认即可(此操作会在 Images.xcassets 中创建 LaunchImage ),然后设置 Launch Screen File 为空

点击 Images.xcassets > LaunchImage ,在右侧属性栏处选择要支持的设备。接着,添加对应分辨率的图片,分辨率对照如下

设备分辨率
iOS 11+1125*2436
iOS 8+ Retina HD 5.51242*2208
iOS 8+ Retina HD 4.7750*1334
iOS 7+ 2x640*960
iOS 7+ Retina 4640*1136
iOS 5,6 1x320*480
iOS 5,6 2x640*960
iOS 5,6 Retina 4640*1136

安卓的尺寸

设备分辨率
mdpi:375*667
hdpi:563*1001
xhdpi:750*1334
xxhdpi:1125*2001
xxxhdpi:1500*2668

完成上述操作之后,重新安装 APP 再启动时就可以看到启动页。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

话少心凉

暂无简介

文章
评论
27 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文