android imageview universal-image-loader scaletype

CenterCrop no funciona en el cargador de imágenes universal: Android



imageview universal-image-loader (3)

Sí, se menciona que siempre mantiene la relación de aspecto, donde cambiar la propiedad scaletype en xml no funcionará ... usar un cultivo codificado en su lugar

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

Actualmente estoy usando Universal Image Loader 1.9.3 y lo inicializo como,

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);

Aquí he usado RoundedBitmapDisplayer porque quiero la imagen como forma redonda y he establecido la propiedad de la vista de imagen en el archivo xml como android: scaleType = "centerCrop" , por lo que debe tener el resultado como imagen de recorte central pero no dio el centro de recorte imagen ... las imágenes se estiran incluso dieron cultivo central ....


Esto parece ser un problema abierto en Universal Image Loader. El trabajo que puedo sugerir para esto es cargar el mapa de bits de la imagen y luego centrar y rodear el mapa de bits según sea necesario. Aquí está la muestra del código.

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) { } });

Para obtener un mapa de bits de esquina redondeada, puede usar este método:

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; }

Asegúrate de establecer

adjustViewBounds = "true"

en tu imagen


puede cambiar el RoundedDrawable en el RoundedBitmapDisplayer con:

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); } }

luego puedes encontrar tu muestra de imágenes en CenterCrop y la esquina redondeada.

También puedes consultar mi github para obtener más información: https://github.com/417704684/RoundCornerDrawable