CenterCrop在通用图像加载器:Android中不工作
我目前正在使用Universal Image Loader 1.9.3并将其初始化为CenterCrop在通用图像加载器:Android中不工作,android,imageview,universal-image-loader,scaletype,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 ImageLoaderConfigur
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”,因此它必须具有作为中心裁剪图像的结果,但它没有给出中心裁剪图像。。图像被拉伸,甚至给了中心裁剪….是的,它总是保持纵横比,在xml上更改scaletype属性是不起作用的。。。改用编码作物
public static Bitmap toCropcenterfitoriginal(Bitmap srcBmp) {
Bitmap dstBmp = ThumbnailUtils.extractThumbnail(srcBmp,
srcBmp.getWidth() / 2, srcBmp.getWidth() / 3);
;
return dstBmp;
}
这似乎是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=“true”
在您的imageview中,您可以使用以下方法更改
RoundedBitmapDisplayer中的RoundedDrawable
:
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);
}
}
公共静态类RoundedDrawable扩展了Drawable{
受保护的最终浮动半径;
受保护的最终整数边距;
受保护的RectF mRect=new RectF(),
mBitmapRect;
受保护的最终BitmapShader BitmapShader;
受保护的最终油漆;
受保护位图mBitmap;
公共圆角可绘制(位图位图、整数圆角半径、整数边距){
此参数。拐角半径=拐角半径;
这个。保证金=保证金;
mBitmap=位图;
bitmapShader=新的bitmapShader(位图,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
mBitmapRect=new RectF(margin,margin,bitmap.getWidth()-margin,bitmap.getHeight()-margin);
油漆=新油漆();
paint.setAntiAlias(真);
paint.setShader(位图着色器);
}
@凌驾
受保护的void onBoundsChange(矩形边界){
super.onBoundsChange(边界);
mRect.set(margin,margin,bounds.width()-margin,bounds.height()-margin);
//调整原始位图的大小以适应新的边界
矩阵着色器矩阵=新矩阵();
//shaderMatrix.setRectRect(mBitmapRect、mRect、Matrix.ScaleToFit.FILL);
int width=bounds.right-bounds.left;
int height=bounds.bottom-bounds.top;
浮动比例=宽度*1.0f/mBitmap.getWidth();
// 如果根据宽度缩放后,高度小于瞄准
if(scale*mBitmap.getHeight()
然后,您可以在中心裁剪和圆角中找到您的图片显示
您还可以查看my github以了解详细信息: