欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

图片下载框架Glide

程序员文章站 2022-03-10 20:14:21
...
图片下载框架Glide :
1.权限(联网权限)
改变图片格式声明:
图片下载框架Glide
2.引入依赖 :com.github.bumptech.glide:glide:3.7.0
代码:
可以当做工具类(旋转动画代码)
public class RotateAnimation implements ViewPropertyAnimation.Animator{
    private int repeatCount;
    private int duration;
    private int startAngle;
    private int endAngle;

    public RotateAnimation(int repeatCount, int endAngle, int startAngle, int duration) {
        this.repeatCount = repeatCount;
        this.endAngle = endAngle;
        this.startAngle = startAngle;
        this.duration = duration;
    }

    @Override
    public void animate(View view) {

        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", startAngle, endAngle);
        animator.setDuration(duration);
        animator.setRepeatCount(repeatCount);
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
    }
}
可以当做工具类(裁剪成圆形图片代码)
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;

        // TODO this could be acquired from the pool too
        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.setAntiAlias(true);

        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);

        return result;
    }

    @Override public String getId() {
        return getClass().getName();
    }
}
可以当做工具类(制作成圆角矩形图片代码)
public class GlideRoundTransform extends BitmapTransformation {

    private static float radius = 0f;

    public GlideRoundTransform(Context context) {
        this(context, 4);       // 圆角矩形的半径4            
    }

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

    @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);
    }
}
public class MainActivity extends AppCompatActivity {

    private ImageView iv1;
    private Button iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv1 = ((ImageView) findViewById(R.id.iv1));
        iv = ((Button) findViewById(R.id.iv));
        intoData();
    }

    private void intoData() {
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String url="http://img1.imgtn.bdimg.com/it/u=2705169360,4047174077&fm=23&gp=0.jpg";
//                String url = "http://images.17173.com/2014/news/2014/12/15/2014cpb1215gif04.gif";
                Glide.with(getBaseContext())
                        .load(Uri.parse(url))
                        //配置缓存
                        .skipMemoryCache(true)
                        //设置图片加载之前的占位符
                        .placeholder(R.mipmap.ic_launcher)
                        //设置图片加载错误的占位符
                        .error(R.mipmap.error_i)
                        //淡入淡出
                        .crossFade(3000)
                        //显示风格
                        .centerCrop()
                       //动画
                        .animate(new RotateAnimation(ValueAnimator.INFINITE,360,0,5000))
                        //变形
                      .transform(new GlideCircleTransform(getBaseContext()))
//                        .transform(getCricleBitmapTransformation())
//                        .transform(getRoundBitmapTransformation())
                        .into(iv1);
            }

          /*  @NonNull
            private BitmapTransformation getCricleBitmapTransformation() {
                return new BitmapTransformation(getBaseContext()) {
                    @Override
                    protected Bitmap transform(BitmapPool pool, Bitmap source, int outWidth, int outHeight) {
                        //得到正方形的边长
                        int min = Math.min(source.getHeight(), source.getWidth());
                        //在原图上截取正方形图片(将来裁剪该正方形的图片,得到圆形图片)
                        Bitmap squareBitmap = Bitmap.createBitmap(source, 0, 0, min, min);
                        //创造与正方形图片等尺寸的空白图片(目的是基于该空白图片,目的是得到等尺寸的空白画布,目的是在该画布上绘制圆形裁剪区域)
                        Bitmap bitmap = Bitmap.createBitmap(min,min, Bitmap.Config.ARGB_8888);
                        //创建画笔画布
                        Paint paint=new Paint();
                        paint.setAntiAlias(true);
                        Canvas canvas=new Canvas(bitmap);
                        //将正方形图片的内容复制到空白图片并绘制裁剪区域
                        Shader shader = paint.setShader(new BitmapShader(squareBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
                        canvas.drawCircle(min/2,min/2,min/2,paint);
                        // //返回加工后的空白图片
                        return bitmap;
                    }

                    @Override
                    public String getId() {
                        return getClass().getSimpleName();
                    }
                };
            }*/




        });
    }

   /* @NonNull
    private BitmapTransformation getRoundBitmapTransformation() {
        return new BitmapTransformation(getBaseContext()) {
            @Override
            protected Bitmap transform(BitmapPool pool, Bitmap source, int outWidth, int outHeight) {
                if (source == null) return null;

                int radius=30;
                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 "yk";
            }
        };
    }*/

    public void aaa(View view) {

        String url="http://img1.imgtn.bdimg.com/it/u=2705169360,4047174077&fm=23&gp=0.jpg";
//                String url = "http://images.17173.com/2014/news/2014/12/15/2014cpb1215gif04.gif";
        Glide.with(getBaseContext())
                .load(Uri.parse(url))
                //配置缓存
                .skipMemoryCache(true)
                //设置图片加载之前的占位符
                .placeholder(R.mipmap.ic_launcher)
                //设置图片加载错误的占位符
                .error(R.mipmap.error_i)
                //淡入淡出
                .crossFade(3000)
                //显示风格
                .centerCrop()
                //动画
                .animate(new RotateAnimation(ValueAnimator.INFINITE,360,0,5000))
                //变形
//                     .transform(getCricleBitmapTransformation())
                .transform(new GlideRoundTransform(getBaseContext()))
                .into(iv1);
    }
    }
public class Yk implements GlideModule {
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {

        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
    }

    @Override
    public void registerComponents(Context context, Glide glide) {

    }
}
布局文件:
<?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"
    android:orientation="vertical"
    tools:context="com.example.glide_test.MainActivity">

    <ImageView
        android:id="@+id/iv1"
        android:layout_weight="1"
        android:layout_margin="50dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
         />

    <Button
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:text="加工成圆形图片"
        android:layout_margin="10dp"
        android:layout_height="wrap_content" />

    <Button
        android:onClick="aaa"
        android:layout_width="match_parent"
        android:text="加工成圆角图片"
        android:layout_margin="10dp"
        android:layout_height="wrap_content" />

</LinearLayout>
运行效果:
图片下载框架Glide图片下载框架Glide图片下载框架Glide