【问题标题】:Using the Glide library to load image to fill up the screen使用 Glide 库加载图像以填满屏幕
【发布时间】:2020-05-15 09:59:57
【问题描述】:

我正在尝试使用 glide 库加载背景图像,以便它填满整个屏幕。我有以下用于布局的 xml。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"   >

    <ImageView
        android:adjustViewBounds="true"
        android:id="@+id/backgroundImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

  <include layout="@layout/signup_layout"/>

</RelativeLayout> 

我在使用 Glide 库的活动中的代码是

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_loginscreen);

    // Setup the email field
    mEmailView = (AutoCompleteTextView) findViewById(R.id.email);

    // Setup password field
    mPasswordView = (EditText) findViewById(R.id.password);
ImageView imageView = (ImageView) findViewById(R.id.backgroundImage);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Glide.with(this)
        .load(R.drawable.green_field)
        .override(width,height)
        .centerCrop()
        .into(imageView);

运行代码后,图像在屏幕的左右两侧都有空白,但它填满了图像的整个高度。我曾尝试同时使用 .fitCenter() 和 .centerCrop() 但图像变得像素化。

【问题讨论】:

    标签: java android android-glide


    【解决方案1】:

    因为你使用它无法填满整个屏幕

    android:adjustViewBounds="true"
    

    哪些订单(docs):

    如果您希望 ImageView 将其边界调整为 保留其可绘制对象的纵横比

    这也是 API17 之前的错误,如上述文档警告。

    如果你想填满整个屏幕,你应该删除android:adjustViewBounds,也许考虑使用

    android:scaleType="fitCenter"
    

    【讨论】:

    • 你一定是喝醉了,没有centerFit scaleType
    • 这非常有效,高度设置为包裹内容和宽度匹配父级,我的图像与宽度和动态高度完美匹配。
    猜你喜欢
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2021-03-21
    相关资源
    最近更新 更多