Android Glide 圓角圖片與圓形圖片

廢話不多說,先貼程式碼

圓角圖片:






activity_main.xml
<?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=".MainActivity">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="36dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
MainActivity.java
RequestOptions options = new RequestOptions()
                .transform(new MultiTransformation<bitmap>(new CenterCrop(), new RoundedCorners(50)))
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .priority(Priority.NORMAL);

Glide.with(MainActivity.this)
                .load("https://www.penghu-nsa.gov.tw/FileDownload/Album/Big/20161012162551758864338.jpg")
                .apply(options)
                .into(imageView);

圓角圖片注意事項:

使用CenterCrop置中填滿imageview並且裁減多出來的部分,並且使用圓角
***重點要使用MultiTransformation才能使CenterCrop和圓角效果並行
***不可對調CenterCrop 和 RoundedCorners,否則圓角會失效

圓形圖片:

RequestOptions options = new RequestOptions()
                .transform(new MultiTransformation<bitmap>(new CenterCrop(), new CircleCrop()))
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .priority(Priority.NORMAL);

圓形圖片注意事項:

跟圓角圖片一樣,不可對調CenterCrop 和 CircleCrop


如果對調CenterCrop 和 CircleCrop,則會變成這樣

不過有一個問題,假如我今天想要毛玻璃高斯模糊效果或者圓角圖片,只有下面左右兩邊是圓角,那該怎麼辦呢,我只能說那就只能用 glide-transformations ,或許是我沒有找到能用的方法。
如果知道的工程師朋友可以留言告訴我,感恩

留言

這個網誌中的熱門文章

Android - 使用 adb 安装apk

Android ContentProvider 實現多個應用程式共享資料

Android TextView autosizing 自動調整大小