Android闪屏矢量纵横比
如何保持向量可绘制(Android闪屏矢量纵横比,android,drawable,Android,Drawable,如何保持向量可绘制(@drawable/ic\u splash\u screen)的纵横比水平合适 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@android:color/white" android:
@drawable/ic\u splash\u screen
)的纵横比水平合适
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@android:color/white"
android:gravity="fill" />
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/logo" />
</item>
<item
android:drawable="@drawable/ic_splash_screen"
android:gravity="bottom|fill_horizontal" />
</layer-list>
在我的代码中,SVG可绘制向量在底部水平填充
,但保持其原始高度
我想在启动屏幕中使用它,并希望保持纵横比。您可以尝试进行以下更改:
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@android:color/white"
android:gravity="fill" />
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/logo" />
</item>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/splash" />
</item>
</layer-list>
您可以尝试进行以下更改:
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@android:color/white"
android:gravity="fill" />
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/logo" />
</item>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/splash" />
</item>
</layer-list>
试试这个
第一个imageView是您的背景,它将向四周延伸。使用adjustViewBounds将保持其高度,不会导致图像拉伸
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_splash_screen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@mipmap/logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
试试这个
第一个imageView是您的背景,它将向四周延伸。使用adjustViewBounds将保持其高度,不会导致图像拉伸
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_splash_screen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@mipmap/logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
作为@deepthink,我也认为您最好的选择是使用重心=中心,因为这是我知道的保持纵横比的唯一方法。
然而,如果你把你的绘图包在一个位图中(正如他所建议的那样),它在不同于21、22和23的API上就不起作用了。在这些相同的API上,如果不将图像包装到位图中,图像始终看起来是拉伸的。这就是为什么我建议您创建两个版本的可绘制层列表,并将其中一个放在drawable-v21中,另一个放在drawable-v24中
对于v24,您可以尝试:
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@android:color/white"
android:gravity="fill" />
<item
android:gravity="center"
android:src="@mipmap/logo" >
</item>
<item
android:gravity="center"
android:src="@drawable/splash" >
</item>
</layer-list>
对于您使用的drawable-v21
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@android:color/white"
android:gravity="fill" />
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/logo" />
</item>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/splash" />
</item>
</layer-list>
至于在不破坏纵横比的情况下水平填充视图,我真的不知道如何在xml中实现这一点。我能想到的唯一一件事就是通过设置android:height和android:width参数来过大您的splash_screen.xml文件的大小,并且面对这样一个事实:在一些更纤细的设备上,您的splash screen左右的大部分边缘将被切掉
…但是,如果你真的很想让splash_screen.xml完全适应屏幕的所有宽度,那么总有一个完全多余的解决方案。为此,我建议您阅读以下内容。这意味着您将根据屏幕dp尺寸(即drawable-sw360dp)创建一组替代可绘制资源,drawable-sw720dp等,然后将不同版本的splash_screen.xml放入其中,每个屏幕上都有android:height和android:width标记。作为@deepthink,我还认为您最好的选择是使用gravity=center,因为这是我知道的保持纵横比的唯一方法。
然而,如果你把你的绘图包在一个位图中(正如他所建议的那样),它在不同于21、22和23的API上就不起作用了。在这些相同的API上,如果不将图像包装到位图中,图像始终看起来是拉伸的。这就是为什么我建议您创建两个版本的可绘制层列表,并将其中一个放在drawable-v21中,另一个放在drawable-v24中
对于v24,您可以尝试:
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@android:color/white"
android:gravity="fill" />
<item
android:gravity="center"
android:src="@mipmap/logo" >
</item>
<item
android:gravity="center"
android:src="@drawable/splash" >
</item>
</layer-list>
对于您使用的drawable-v21
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@android:color/white"
android:gravity="fill" />
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/logo" />
</item>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/splash" />
</item>
</layer-list>
至于在不破坏纵横比的情况下水平填充视图,我真的不知道如何在xml中实现这一点。我能想到的唯一一件事就是通过设置android:height和android:width参数来过大您的splash_screen.xml文件的大小,并且面对这样一个事实:在一些更纤细的设备上,您的splash screen左右的大部分边缘将被切掉
…但是,如果你真的很想让splash_screen.xml完全适应屏幕的所有宽度,那么总有一个完全多余的解决方案。为此,我建议您阅读以下内容。这意味着您需要根据屏幕dp维度(即drawable-sw360dp、drawable-sw720dp等)创建一系列可供选择的可绘制资源,然后将不同版本的splash_screen.xml放入其中,每个屏幕上都有android:height和android:width标签。对于启动屏幕,你不想要布局-OP大概想要坚持可绘制的
s。对于启动屏幕,你不想要布局-OP大概想要坚持可绘制的
s。我不得不处理类似的事情,最终设法让我的splashscreen在包括21和22在内的所有API上工作。我会在下面发布一个总结性的回答,但这里有一个链接,我不得不处理一些类似的事情,并最终设法使我的splashscreen在所有API上工作,包括21和22。我会在下面发布一个总结的答案,但这里是链接