【问题标题】:Top crop with ImageLoader - Android使用 ImageLoader 进行顶部裁剪 - Android
【发布时间】:2016-02-08 11:31:04
【问题描述】:

我正在将图像从 URL 加载到我的 ImageView 中,就像这样 -

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

现在我想将图像裁剪到中心顶部。我尝试使用https://gist.github.com/arriolac/3843346,但我认为它需要通过可绘制的图像,我想使用 ImageLoader 来设置图像。

谁能帮助我了解我是否可以将https://code.google.com/archive/p/android-query/(Android Query)与 ImageLoader 结合使用?

有没有办法在 Android 中原生处理裁剪(除了 centerCrop)?

【问题讨论】:

    标签: android universal-image-loader


    【解决方案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);
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      相关资源
      最近更新 更多