picasso开源库了解一下
简单粗暴点这一 一行就够啦
url:网络图片资源
imagview:待填充图片的控件
with:执行布局
load:加载图片
fit:自适应
into:填充
Picasso.with(MainActivity.this).load(url).fit().into(imageView);
添加依赖
implementation 'com.squareup.picasso:picasso:2.5.2'
package fengxing.primer.picasso;
import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;
/**
* Picasso图片工具
*/
public class PicassoUtil {
/**
* fit :图片自适应大小
* into:填充图片到控件
* laod:加载图片url
* resize:重定义大小
* crop:裁剪图片
*/
/**
* 下载并裁剪
* @param context
* @param url
* @param width
* @param height
* @param imageView
*/
public static void loadImageWithResize(Context context, String url, int width, int height, ImageView imageView){
Picasso.with(context).load(url).resize(width,height).centerCrop().fit().into(imageView);
}
/**
*
* @param context
* @param url
* @param resID
* @param imageView
*/
public static void loadImageWithSlower(Context context,String url,int resID,ImageView imageView){
Picasso.with(context).load(url).fit().placeholder(resID).into(imageView);
}
/**
* 正方形自定义的裁剪
* @param context
* @param url
* @param imageView
*/
public static void loadImageWithSquareCrop(Context context,String url,ImageView imageView){
Picasso.with(context).load(url).fit().transform(new CropSquareImage()).into(imageView);
}
/**
* 裁剪类 (正方向裁剪)
*/
public static class CropSquareImage implements Transformation{
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(),source.getHeight());
int cropX = (source.getWidth()-size)*2;
int cropY = (source.getHeight()-size)*2;
Bitmap bitmap = Bitmap.createBitmap(source,cropX,cropY,size,size);
if (source!=bitmap){
source.recycle();
}
return bitmap;
}
@Override
public String key() {
return "square()";
}
}
}