Android背景尺寸-调整大小以适合所有设备

时间:2020-01-30 12:00:48

标签: android

这是一个与之前提出的其他背景图片大小调整问题略有不同的问题。在该片段上,当用户选择一个图标时,每次单击背景图像都会动态变化。例如,如果用户单击太阳图标,则背景将更改为晴天,如果用户单击云图标,则背景将更改为阴天,依此类推。

我使用一个简单的switch语句来实现这一点:

@Override
public void onMoodClick(int position) {

    switch (position) {
        case 0:
            moodBefore = 1;
            backgroundChanger.setImageResource(R.mipmap.background_clouds_excited);
            hasSelected = true;
            break;

        case 1:
            moodBefore = 2;
            backgroundChanger.setImageResource(R.mipmap.background_clouds_happy);
            hasSelected = true;
            break;

问题是:在设置为像素2的仿真器上,它看起来很完美。但是,当我将应用程序加载到Galaxy s10上时,我注意到背景的一部分被切掉了。因此,就像我的s10只能看到一半的阳光,而在Pixel 2模拟器上可以看到整个背景。谁能解决这个问题,所以背景会更动态一些并且可以容纳不同的屏幕尺寸吗?

布局:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/FragmentWorkoutMood2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragments.WorkoutFragment">

<com.example.feelingfit.utils.CropImageView
    android:id="@+id/workoutBackground"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="matrix"
    android:src="@mipmap/background_clouds_workout_default"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

1 个答案:

答案 0 :(得分:0)

您应在XML文件中为android:scaleType元素设置backgroundChanger。您检查此链接,您将看到每种比例类型的图像。您可以选择最适合自己的。 https://thoughtbot.com/blog/android-imageview-scaletype-a-visual-guide