注:有关Picasso的基本介绍,请参考我的上篇博客
Picasso支持自定义Transformation
public class CropSquareTransformation 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 result = Bitmap.createBitmap(source, x, y, size, size);
if (result != source) {
source.recycle();
}
return result;
}
@Override public String key() { return "square()"; }
}
使用
Picasso.with(context)
.load(url)
.transform(new CropSquareTransformation())
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
参考:RoundedImageView
RoundedImageView:https://github.com/vinc3m1/RoundedImageView
实现RoundedTransformationBuilder
Transformation transformation = new RoundedTransformationBuilder()
.borderColor(Color.TRANSPARENT)
.borderWidthDp(0)
.oval(false)
.build();
Picasso.with(context)
.load(persons.get(position).getPictureUrl()
.transform(transformation)
.placeholder(R.mipmap.image_default)//没有加载图片时显示的默认图像
.error(R.mipmap.image_default)// 图像加载错误时显示的图像
.into(holder.mImg);// 被加载的控件
注意:
图片的scaleType=centerCrop时没有圆角的效果
android:scaleType="fitXY"
才有圆角效果
实现效果如下:
RoundedTransformationBuilder.java代码
/*
* Copyright (C) 2015 Vincent Mi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zhoujian.picasso.transform;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.ImageView;
import com.squareup.picasso.Transformation;
import java.util.Arrays;
public final class RoundedTransformationBuilder {
private final DisplayMetrics mDisplayMetrics;
private float[] mCornerRadii = new float[] { 0, 0, 0, 0 };
private boolean mOval = false;
private float mBorderWidth = 0;
private ColorStateList mBorderColor =
ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
private ImageView.ScaleType mScaleType = ImageView.ScaleType.FIT_CENTER;
public RoundedTransformationBuilder() {
mDisplayMetrics = Resources.getSystem().getDisplayMetrics();
}
public RoundedTransformationBuilder scaleType(ImageView.ScaleType scaleType) {
mScaleType = scaleType;
return this;
}
/**
* Set corner radius for all corners in px.
*
* @param radius the radius in px
* @return the builder for chaining.
*/
public RoundedTransformationBuilder cornerRadius(float radius) {
mCornerRadii[Corner.TOP_LEFT] = radius;
mCornerRadii[Corner.TOP_RIGHT] = radius;
mCornerRadii[Corner.BOTTOM_RIGHT] = radius;
mCornerRadii[Corner.BOTTOM_LEFT] = radius;
return this;
}
/**
* Set corner radius for a specific corner in px.
*
* @param corner the corner to set.
* @param radius the radius in px.
* @return the builder for chaning.
*/
public RoundedTransformationBuilder cornerRadius(int corner, float radius) {
mCornerRadii[corner] = radius;
return this;
}
/**
* Set corner radius for all corners in density independent pixels.
*
* @param radius the radius in density independent pixels.
* @return the builder for chaining.
*/
public RoundedTransformationBuilder cornerRadiusDp(float radius) {
return cornerRadius(
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radius, mDisplayMetrics));
}
/**
* Set corner radius for a specific corner in density independent pixels.
*
* @param corner the corner to set
* @param radius the radius in density independent pixels.
* @return the builder for chaining.
*/
public RoundedTransformationBuilder cornerRadiusDp(int corner, float radius) {
return cornerRadius(corner,
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radius, mDisplayMetrics));
}
/**
* Set the border width in pixels.
*
* @param width border width in pixels.
* @return the builder for chaining.
*/
public RoundedTransformationBuilder borderWidth(float width) {
mBorderWidth = width;
return this;
}
/**
* Set the border width in density independent pixels.
*
* @param width border width in density independent pixels.
* @return the builder for chaining.
*/