使用ImageLoader进行顶级裁剪 - Android

时间:2016-02-08 11:31:04

标签: android universal-image-loader

我正在将图像从URL加载到我的ImageView中,如此 -

ImageLoader.getInstance().displayImage(url, imageview, display-options)

现在我想将图像裁剪到中间顶部。我尝试使用https://gist.github.com/arriolac/3843346,但我认为它通过drawable期望图像,我想使用ImageLoader来设置图像。

有人可以帮我理解我是否可以将https://code.google.com/archive/p/android-query/(Android查询)与ImageLoader结合使用?

有没有办法在Android(除了centerCrop)本地处理裁剪?

1 个答案:

答案 0 :(得分:1)

用ProfileImageView替换你的ImageView并享受视图:)

public class ProfileImageView extends ImageView {
private Bitmap bm;

public ProfileImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    bm = null;
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    if (bm != null) {
        setImageBitmap(bm);
        bm = null;
    }
}

@Override
public void setImageBitmap(Bitmap bm) {
    int viewWidth = getWidth();
    int viewHeight = getHeight();
    if (viewWidth == 0 || viewHeight == 0) {
        this.bm = bm;
        return;
    }
    setScaleType(ScaleType.MATRIX);

    Matrix m = getImageMatrix();
    m.reset();
    int min = Math.min(bm.getWidth(), bm.getHeight());
    int cut;
    if (bm.getWidth() > viewWidth && bm.getWidth() > bm.getHeight()) {
        cut = Math.round((bm.getWidth() - viewWidth) / 2.f);
    } else {
        cut = 0;
    }
    RectF drawableRect = new RectF(cut, 0, min - cut, min);

    RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
    m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.START);
    setImageMatrix(m);
    super.setImageBitmap(bm);
}

}