[英]Vector splash screen image with a half of screen size on Android
我有一个带有大小为 480dp 和径向渐变的矢量图像的初始屏幕:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<!-- <solid android:color="#FF07862c"/> -->
<gradient
android:type="radial"
android:startColor="#086824"
android:endColor="#098a2f"
android:gradientRadius="480dp"
android:centerX="0.5"
android:centerY="0.5"/>
</shape>
</item>
<!-- <item android:drawable="@drawable/background0" android:gravity="center"/> -->
<item android:drawable="@drawable/logo"
android:width="480dp"
android:height="480dp"
android:gravity="center"/>
</layer-list>
如何使图像大小
min(screen_width, screen_height) / 2
(在伪代码中)?
apptheme.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash</item>
</style>
</resources>
AndroidManifest.xml
:
<activity android:name="com.myapp.MainActivity"
android:theme="@style/AppTheme"
您可以通过将LayerDrawable
的图层属性修改为运行时的几种方式来执行此操作。 对于您的drawable.xml
,您可以修改索引 1 处的图层,如下所示。 那么你需要在运行时设置windowBackground
。
val layerDrawable = ContextCompat.getDrawable(this,R.drawable.test) as LayerDrawable
val size = resources.displayMetrics.widthPixels.coerceAtMost(resources.displayMetrics.heightPixels) /2
layerDrawable.setLayerGravity(1,Gravity.CENTER)
layerDrawable.setLayerHeight(1,size)
layerDrawable.setLayerWidth(1,size)
window.setBackgroundDrawable(layerDrawable)
同样,您可以根据需要修改任何图层。 如果您需要更改第一层,则第一层将是GradientDrawable
获取layerDrawable
的 drawable 并将其转换为GradientDrawable
。 然后你可以改变它的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.