您的位置:首页 > 其它

第三方开源库:Picasso-图片加载框架

2016-11-18 17:39 316 查看
参考:

Android 图片加载框架Picasso Glide

Picasso使用详解

简介:

Picasso是Square公司出品的一个强大的图片下载和缓存图片库。

Picasso特点:

加载载网络或本地资源

自动的内存和硬盘缓存

在adapter中回收和取消当前的下载

图形转换操作,如变换大小,旋转等,提供了接口来让用户可以自定义转换操作

官网地址:http://square.github.io/picasso/

使用

添加引用

dependencies {
...
compile 'com.squareup.picasso:picasso:2.5.2'
...
}


步骤

Picasso.with(context).load(url1).into(iv);


1 加载网络图片

Picasso.with(context).load(url1).into(iv);


2 加载资源图片

Picasso.with(context).load(R.mipmap.b).into(iv);


3 加载文件图片

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/Screenshots/a.png";
File file = new File(path);
Picasso.with(context).load(file).into(iv);


4 裁剪图片

Picasso.with(context).load(url1).resize(300, 300).centerInside().into(iv);


5 占位图片

Picasso.with(context).load(url1).placeholder(R.mipmap.b).error(R.mipmap.b).into(iv);


6 加载文件图片

Picasso.with(context).load(url1).transform(transformation).into(iv);


transformation:

private Transformation transformation = new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
//自定义裁剪source
//      Bitmap bitmap = ImageUtil.getRoundedCornerBitmap(source, 1000.0f);//圆
Bitmap bitmap = ImageUtil.createReflectionImageWithOrigin(source);//倒影
source.recycle();//原始图片要及时回收
return bitmap;
}

//key()不可返回null
@Override
public String key() {
return "rounded";
}
};


ImageUtils.java

public class ImageUtil {
// 将Drawable转化为Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);
return bitmap;

}

// 放大缩小图片
public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidht = ((float) w / width);
float scaleHeight = ((float) h / height);
matrix.postScale(scaleWidht, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
return newbmp;
}

// 获得圆角图片的方法
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}

// 获得带倒影的图片方法
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();

Matrix matrix = new Matrix();
matrix.preScale(1, -1);

Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

return bitmapWithReflection;
}
}


源码

PicassoDemo01

其他

第三方开源库:Picasso-图片加载框架

第三方开源库:Glide-图片加载框架

第三方开源库:imageLoader的使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐