Glide ImageView适合CircleTransformed大小

时间:2015-12-30 16:06:43

标签: android geometry android-imageview android-glide

我正在使用带有CircleTransform的Glide将圆形图像添加到此ImageView中。

ImageView具有wrap_content属性,但ImageView不适合CircleTransformed Image的宽度。

这是我的代码: 我的图片加载:

int itemSizeInDp = (int) mContext.getResources().getDimension(R.dimen.spacing_social);
    int itemSizeInPx = MetricsHelper.convertDpToPx(mContext, itemSizeInDp);
    Glide.with(mContext)
            .load(mSocialPhotos.get(position))
            .transform(new CircleTransform(mContext))
            .override(itemSizeInPx, itemSizeInPx)
            .into(holder.mSocialPhoto);

我的CircleTransform课程:

public class CircleTransform extends BitmapTransformation {

public CircleTransform(Context context) {
    super(context);
    Log.d("photo", "transform");
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap source, int outWidth, int outHeight) {
    return ImageUtils.getCircularBitmapImage(source);
}
@Override
public String getId() {
    return "Glide_Circle_Transformation";
}}

public class ImageUtils {
public static Bitmap getCircularBitmapImage(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, Bitmap.Config.ARGB_8888);
    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;
}}

和图片视图: the image view (the black is the full imageview content)

有没有办法让我的imageview适合圆宽?

修改

我可以像下面那样直接设置ImageView的LayoutParams,但我认为肯定有一种更优雅的方式来做到这一点。

holder.mSocialPhoto.getLayoutParams().height = itemSizeInPx;
holder.mSocialPhoto.getLayoutParams().width = itemSizeInPx;

1 个答案:

答案 0 :(得分:0)

由于我还没有找到更好的方法,

holder.mSocialPhoto.getLayoutParams().height = itemSizeInPx;
holder.mSocialPhoto.getLayoutParams().width = itemSizeInPx;

工作得很好。