Glide原生方法加載圓形圖片

  • 項目中需要用圓形圖片,我們知道,Android原生是沒有好用的圓形圖片的,何況還要網絡加載什么的。今天找了下Glide加載圓形圖片的方法,基本都是這種,說得倒是很明白的,但是我自己加載時候沒有找到相應的方法啊,后來恍然,原來現在版本的Glide已經放棄了之前的一些方法。
  • 先奉上Glide傳送門Glide開源項目
  • 當然 新版本的Glide已經給我們提供了展示 圓形、圓角圖片實現方法,看效果:
    1.png
  • 實現如上效果比價簡單:
  1. 添加依賴:在module的gradle中添加:
  implementation 'com.github.bumptech.glide:glide:4.7.1'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
  1. 開放網絡權限
 <uses-permission android:name="android.permission.INTERNET"/>
  1. 準備用以展示圖片的布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center">

    <ImageView
        android:id="@+id/circle"
        android:layout_width="120dp"
        android:layout_height="120dp" />
    <ImageView
        android:id="@+id/round1"
        android:layout_width="120dp"
        android:layout_height="120dp" />
    <ImageView
        android:id="@+id/round2"
        android:layout_width="120dp"
        android:layout_height="120dp" />

</LinearLayout>
  1. 代碼中使用,MainActivity.java:
public class MainActivity extends AppCompatActivity {
    ImageView circle, round1, round2;

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

        initView();
    }

    private void initView() {
        circle = findViewById(R.id.circle);
        round1 = findViewById(R.id.round1);
        round2 = findViewById(R.id.round2);

        Glide.with(this)
                .load("http://img5.duitang.com/uploads/item/201506/07/20150607110911_kY5cP.jpeg")
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(circle);

        Glide.with(this)
                .load("http://img.jiuzheng.com/memberlogo/s/57/0a/570af0f48f1e0327178b468d.jpg")
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))//圓角半徑
                .into(round1);

        Glide.with(this)
                .load("http://img.jiuzheng.com/memberlogo/s/57/0a/570af0f48f1e0327178b468d.jpg")
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(60)))//圓角半徑
                .into(round2);
    }
}
  • 這樣,我們用Glide 原生方法便實現了加載圓形、圓角圖片的功能。關鍵代碼在于:
  1. 圓形圖片
 .apply(RequestOptions.bitmapTransform(new CircleCrop()))
  1. 圓角圖片
.apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))//圓角半徑

over.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容