1. 程式人生 > >使用ImageLoader設定圓角時無效或者乾脆圖片不顯示

使用ImageLoader設定圓角時無效或者乾脆圖片不顯示

在專案中使用ImageLoader來載入圖片,現在有個需求是,這個視訊封面需要微微的有圓角。
我的程式碼剛開始時這樣寫的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.wjq.myimageloaderdemo.MainActivity">
<TextView android:id="@+id/iv_load" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable
="true" android:text="點選用ImageLoader載入圖片" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias
="0.293" />
<ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@mipmap/ic_launcher" app:layout_constraintVertical_bias="0.498"/> </android.support.constraint.ConstraintLayout>

對於ImagerLoader載入圖片時預設的設定程式碼如下:

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.search_video_default) // 設定圖片下載期間顯示的圖片
                .showImageForEmptyUri(R.mipmap.search_video_default) // 設定圖片Uri為空或是錯誤的時候顯示的圖片
                .showImageOnFail(R.mipmap.search_video_default) // 設定圖片載入或解碼過程中發生錯誤顯示的圖片
                .cacheInMemory(true) // 設定下載的圖片是否快取在記憶體中
                .cacheOnDisk(true) // 設定下載的圖片是否快取在SD卡中
                .displayer(new RoundedBitmapDisplayer(20))//這一句就是設定讓ImageLoader載入的圖片有圓角的地方
                .build(); // 構建完成

結果就是,ImageLoader什麼也沒有加載出來。我還檢查好久,看看是不是我哪裡寫錯了。直到找到原因前,我一直都認為是我哪裡寫錯了。但檢查好幾遍發現。有區別的地方就是:

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher"
        app:layout_constraintVertical_bias="0.498"/>

別人的寫法,ImageView有明確的寬高,而我是wrap_content,結果我就試著隨便寫了個寬高,真的他媽的能顯示出來了,圓角也有了。具體什麼原因,得去看ImageLoader的原始碼了,不過我先記下來。原始碼以後有時間再看。

總結:如果使用使用ImageLoader給ImageView載入的圖片設定圓角時無效或者乾脆圖片不顯示,可能的原因就是沒有給ImageView設定具體的layout_width和layout_height的值。