Commit b32c1faf by gao.chao

完善加载图片

parent 54d20ace
package com.mayi.fastdevelop.view;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import com.squareup.picasso.Transformation;
/**
* 圆角切片
*/
public class CircleCornerForm implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
int widthLight = source.getWidth();
int heightLight = source.getHeight();
Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paintColor = new Paint();
paintColor.setFlags(Paint.ANTI_ALIAS_FLAG);
RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight));
canvas.drawRoundRect(rectF, widthLight / 5, heightLight / 5, paintColor);
Paint paintImage = new Paint();
paintImage.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
canvas.drawBitmap(source, 0, 0, paintImage);
source.recycle();
return output;
}
@Override
public String key() {
return "roundcorner";
}
}
package com.mayi.fastdevelop.view;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import com.squareup.picasso.Transformation;
/**
* 圆形图片
*/
public class CircleTransform implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap,
BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
return bitmap;
}
@Override
public String key() {
return "circle";
}
}
package com.mayi.fastdevelop.view;
import android.text.TextUtils;
import android.widget.ImageView;
import com.mayi.fastdevelop.R;
......@@ -14,27 +15,70 @@ public class LoadingPictures {
public static int error = R.mipmap.loading_pictures_error_icon;
public static int placeholder = R.mipmap.loading_pictures_placeholder_icon;
//加载圆角图片
public static void loadUriCircleCornerForm(String url, ImageView img) {
if (TextUtils.isEmpty(url)) {
Picasso.with(img.getContext()).load(error).transform(new CircleCornerForm()).into(img);
} else {
Picasso.with(img.getContext())
.load(url)
.resize(2000, 2000)
.onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
.transform(new CircleCornerForm())
.placeholder(placeholder)
.error(error)
.into(img);
}
}
//加载圆形图片
public static void loadUriCircleTransform(String url, ImageView img) {
if (TextUtils.isEmpty(url)) {
Picasso.with(img.getContext()).load(error).transform(new CircleTransform()).into(img);
} else {
Picasso.with(img.getContext())
.load(url)
.resize(2000, 2000)
.onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
.transform(new CircleTransform())
.placeholder(placeholder)
.error(error)
.into(img);
}
}
//加载网络图片
public static void loadUri(String url, ImageView img) {
Picasso.with(img.getContext())
.load(url)
.resize(2000, 2000)
.onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
.placeholder(placeholder)
.error(error)
.into(img);
if (TextUtils.isEmpty(url)) {
Picasso.with(img.getContext()).load(error).into(img);
} else {
Picasso.with(img.getContext())
.load(url)
.resize(2000, 2000)
.onlyScaleDown() // 如果图片规格大于2000*2000,将只会被resize
.placeholder(placeholder)
.error(error)
.into(img);
}
}
//加载文件图片
public static void loadFile(File file, ImageView img) {
Picasso.with(img.getContext())
.load(file)
.resize(2000, 2000)
.placeholder(placeholder)
.error(error)
.into(img);
if (file == null) {
Picasso.with(img.getContext()).load(error).into(img);
} else {
Picasso.with(img.getContext())
.load(file)
.resize(2000, 2000)
.placeholder(placeholder)
.error(error)
.into(img);
}
}
//加载文件图片
public static void loadFile(String pathname, ImageView img) {
loadFile(new File(pathname),img);
loadFile(new File(pathname), img);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment