CenterCrop无法在通用图像加载器中运行:Android

时间:2015-03-10 05:51:28

标签: android imageview universal-image-loader scaletype

我目前正在使用Universal Image Loader 1.9.3并将其初始化为

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(100)).cacheOnDisc().build();
    ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(Register.this).defaultDisplayImageOptions(defaultOptions).memoryCache(new WeakMemoryCache());

    ImageLoaderConfiguration config = builder.build();
    imageLoader2 = ImageLoader.getInstance();
    imageLoader2.init(config);

这里我使用了 RoundedBitmapDisplayer ,因为我希望图像为圆形,并且我已将xml文件中的图像视图的属性设置为 android:scaleType =" centerCrop" ,所以它必须有结果作为中心裁剪图像,但它没有给出中心裁剪图像..图像被拉伸甚至给中心裁剪....

3 个答案:

答案 0 :(得分:1)

是的,提到它始终保持宽高比,在xml上更改scaletype属性不会工作...使用编码裁剪

public static Bitmap toCropcenterfitoriginal(Bitmap srcBmp) {
    Bitmap dstBmp = ThumbnailUtils.extractThumbnail(srcBmp,
            srcBmp.getWidth() / 2, srcBmp.getWidth() / 3);
    ;

    return dstBmp;
}

答案 1 :(得分:1)

您可以使用以下内容更改RoundedDrawable中的RoundedBitmapDisplayer

public static class RoundedDrawable extends Drawable {

        protected final float cornerRadius;
        protected final int margin;

        protected  RectF mRect = new RectF(),
                mBitmapRect;
        protected final BitmapShader bitmapShader;
        protected final Paint paint;
        protected Bitmap mBitmap;

        public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin) {
            this.cornerRadius = cornerRadius;
            this.margin = margin;
            mBitmap = bitmap;

            bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            mBitmapRect = new RectF(margin, margin, bitmap.getWidth() - margin, bitmap.getHeight() - margin);

            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setShader(bitmapShader);
        }

        @Override
        protected void onBoundsChange(Rect bounds) {
            super.onBoundsChange(bounds);
            mRect.set(margin, margin, bounds.width() - margin, bounds.height() - margin);

            // Resize the original bitmap to fit the new bound
            Matrix shaderMatrix = new Matrix();
//            shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL);
            int width = bounds.right - bounds.left;
            int height = bounds.bottom - bounds.top;

            float scale = width * 1.0f / mBitmap.getWidth();
            // 如果根据宽度缩放后,高度小于targetHeight
            if (scale * mBitmap.getHeight() < height) {
                scale = height * 1.0f / mBitmap.getHeight();
            }
            int outWidth = Math.round(scale * mBitmap.getWidth());
            int outHeight = Math.round(scale * mBitmap.getHeight());

            shaderMatrix.postScale(scale, scale);

            int left = 0;
            int top = 0;
            if (outWidth == width) {
                top = (outHeight - height) * -1 / 2;
            }
            else {
                left = (outWidth - width) * -1 / 2;
            }

            shaderMatrix.postTranslate(left, top);
            bitmapShader.setLocalMatrix(shaderMatrix);
        }

        @Override
        public void draw(Canvas canvas) {
            canvas.drawRoundRect(mRect, cornerRadius, cornerRadius, paint);
        }

        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }

        @Override
        public void setAlpha(int alpha) {
            paint.setAlpha(alpha);
        }

        @Override
        public void setColorFilter(ColorFilter cf) {
            paint.setColorFilter(cf);
        }
    }

然后你可以在CenterCrop和圆角找到你的图片。

您还可以查看我的github了解详细信息:https://github.com/417704684/RoundCornerDrawable

答案 2 :(得分:0)

这似乎是Universal Image Loader中的一个悬而未决的问题。我可以为此建议的工作是,加载图像位图,然后根据需要对位图进行居中和转角。这是代码示例。

 BaseActivity.imageLoader.loadImage(mUrl, mOptions, new ImageLoadingListener() 
 {
            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason)
            {
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
            {
                if (loadedImage != null)
                {
                    Bitmap croppedBitmap = ThumbnailUtils.extractThumbnail(loadedImage, HIQUtil.dpToPixel(getActivity(), 295), HIQUtil.dpToPixel(getActivity(), 211));
                    Bitmap roundedCropped = getRoundedCornerBitmap(croppedBitmap, 5);
                    imageView.setImageBitmap(roundedCropped);
                }
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }
        });

要获得圆角位图,您可以使用以下方法:

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
            .getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

确保设置

  

adjustViewBounds =&#34; true&#34;

在您的imageview中