Android Glide加载网络图片!圆形图片,圆角图片

    一:先说下glide的优点吧!

  •  使用简单
  • 可配置度高,自适应程度高
  • 支持常见图片格式 Jpg png gif webp
  • 支持多种数据源  网络、本地、资源、Assets 等
  • 高效缓存策略    支持Memory和Disk图片缓存 默认Bitmap格式采用RGB_565内存使用至少减少一半
  • 生命周期集成   根据Activity/Fragment生命周期自动管理请求
  • 高效处理Bitmap  使用Bitmap Pool使Bitmap复用,主动调用recycle回收需要回收的Bitmap,减小系统回收压力

   二:glide加载网络图片的步骤

1.导入jar包

 glide -3.7.0 jar包文章底部奉上!!!!

把jar包复制粘贴到lib文件夹下就OK了!然后选中jar包点击鼠标右键


点击Add As Library添加依赖!!

2.下面是布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context="com.example.lyd.myapplication.MainActivity">
<TextView
    android:gravity="center"
    android:textSize="18sp"
    android:textColor="#323232"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Glide加载图片"
    />
  <Button
      android:id="@+id/b1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="正常图片"
      />

  <ImageView
      android:id="@+id/iv1"
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:src="@mipmap/ic_launcher"
      />

  <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆形图片"
        />
    <ImageView
        android:id="@+id/iv2"
        android:src="@mipmap/ic_launcher"
        android:layout_width="100dp"
        android:layout_height="100dp"

        />
    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圆角图片"
        />
    <ImageView
        android:id="@+id/iv3"
        android:src="@mipmap/ic_launcher"
        android:layout_width="100dp"
        android:layout_height="100dp"

        />
</LinearLayout>


布局呢就随便布了一下,不要在意这些细节!主要是演示glide加载图片!

3.   编写2个类,这两个类的功能就是对加载来的图片进行处理使其变成圆形的或者圆角的!copy就行

处理成圆形图片的类

package com.example.lyb.demo.bitmap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

/**
 * 类描述
 * 类传参
 * 创建人:Lyb
 * 创建时间:2018/3/9
 */

public class GlideCircleTransform extends BitmapTransformation {
    public GlideCircleTransform(Context context) {
        super(context);
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return circleCrop(pool, toTransform);
    }

    private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        int size = Math.min(source.getWidth(), source.getHeight());
        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);

        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);
        return result;
    }

    @Override
    public String getId() {
        return getClass().getName();
    }

}

下面是圆角的!!!!!

package com.example.lyb.demo.bitmap;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

/**
 * 类描述
 * 类传参
 * 创建人:Lyb
 * 创建时间:2018/3/9
 */

public class GlideRoundTransform extends BitmapTransformation {

    private static float radius = 0f;

    public GlideRoundTransform(Context context) {
        this(context, 4);
    }

    public GlideRoundTransform(Context context, int dp) {
        super(context);
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

    @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return roundCrop(pool, toTransform);
    }

    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        return result;
    }

    @Override public String getId() {
        return getClass().getName() + Math.round(radius);
    }
}



4.接下里就是java文件的编写了也很简单

package com.example.lyd.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView imageView1;
    ImageView imageView2 ;
    ImageView imageView3 ;
    String url="http://d.hiphotos.baidu.com/image/pic/item/f9198618367adab45913c15e87d4b31c8601e4e8.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1 = (Button) findViewById(R.id.b1);
        Button button2 = (Button) findViewById(R.id.b2);
        Button button3 = (Button) findViewById(R.id.b3);
        imageView1 = (ImageView) findViewById(R.id.iv1);
        imageView2 = (ImageView) findViewById(R.id.iv2);
         imageView3 = (ImageView) findViewById(R.id.iv3);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.b1:
                Glide.with(this).load(url).into(imageView1);
                break;
            case R.id.b2:
                Glide.with(this).load(url).centerCrop().transform(new GlideCircleTransform(this)).into(imageView2);
                break;
            case R.id.b3:
                Glide.with(this).load(url).centerCrop().transform(new GlideRoundTransform(this, 30)).into(imageView3);
                break;


        }
    }
}
最后千万别忘了添加联网权限!
<uses-permission android:name="android.permission.INTERNET"/>

好啦!!一个简单的glide加载图片就完成了!有不懂的小伙伴可以私聊q 1778934152!!!














发布了18 篇原创文章 · 获赞 9 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览