android 自定义圆角ImageView,后设置scaleType="centerCrop"无效?

如下就是关键部分代码:如何才能实现圆角ImageView以centerCrop模式显示?

public static Bitmap getBitmap(int width, int height) {
        //int shadow = (int)(UIUtils.dip2px(2));
        Bitmap output = Bitmap.createBitmap(width,
                height, 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, width, height);
        final RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);  
        canvas.drawARGB(0, 0, 0, 0);  
        paint.setColor(color); 
        canvas.drawRoundRect(rectF, getRoundPx(), getRoundPx(), paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(output, rect, rect, paint);
        
        return output;
    }
阅读 11.8k
6 个回答
新手上路,请多包涵

你这是怎么用的?先调用这个方法,再setBitmap到ImageView?如果是这样,是有可能不能实现的!因为centerCrop是先给bitmap宽高放大到大于等于ImageView的宽高,然后显示中间的部分!这样你放大以后再裁减就有可能原来的圆角部分超过控件的范围而得不到显示

新手上路,请多包涵

直接继承imageview在ondraw里画吧!

设置DisplayImageOptions属性的时候用这个BitmapDisplayer即可实现CenterCrop效果。

public class CenterCropRoundedBitmapDisplayer implements BitmapDisplayer {
    protected final int cornerRadius;
    protected final int margin;

    public CenterCropRoundedBitmapDisplayer(int cornerRadiusPixels) {
        this(cornerRadiusPixels, 0);
    }

    public CenterCropRoundedBitmapDisplayer(int cornerRadiusPixels, int marginPixels) {
        this.cornerRadius = cornerRadiusPixels;
        this.margin = marginPixels;
    }

    public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
        if(!(imageAware instanceof ImageViewAware)) {
            throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
        } else {
            imageAware.setImageDrawable(new CenterCropRoundedBitmapDisplayer.RoundedDrawable(bitmap, this.cornerRadius, this.margin,imageAware.getWidth(),imageAware.getHeight()));
        }
    }

    public static class RoundedDrawable extends Drawable {
        protected final float cornerRadius;
        protected final int margin;
        protected final RectF mRect = new RectF();
        protected final RectF mBitmapRect;
        protected final BitmapShader bitmapShader;
        protected final Paint paint;

        float scale;
        float dx = 0, dy = 0;

        public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin,int vwidth,int vheight) {
            this.cornerRadius = (float)cornerRadius;
            this.margin = margin;
            this.bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            this.mBitmapRect = new RectF((float)margin, (float)margin, (float)(bitmap.getWidth() - margin), (float)(bitmap.getHeight() - margin));
            this.paint = new Paint();
            this.paint.setAntiAlias(true);
            this.paint.setShader(this.bitmapShader);
            this.paint.setFilterBitmap(true);
            this.paint.setDither(true);

            /*设置centercrop属性*/
            if (bitmap.getWidth() * vheight > vwidth * bitmap.getHeight()) {
                scale = (float) vheight / (float) bitmap.getHeight();
                dx = (vwidth - bitmap.getWidth() * scale) * 0.5f;
            } else {
                scale = (float) vwidth / (float) bitmap.getWidth();
                dy = (vheight - bitmap.getHeight() * scale) * 0.5f;
            }
        }

        protected void onBoundsChange(Rect bounds) {
            super.onBoundsChange(bounds);
            this.mRect.set((float)this.margin, (float)this.margin, (float)(bounds.width() - this.margin), (float)(bounds.height() - this.margin));
            Matrix shaderMatrix = new Matrix();
            shaderMatrix.setRectToRect(this.mBitmapRect, this.mRect, Matrix.ScaleToFit.FILL);
            shaderMatrix.setScale(scale, scale);
            shaderMatrix.postTranslate(Math.round(dx), Math.round(dy));
            this.bitmapShader.setLocalMatrix(shaderMatrix);
        }

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

        public int getOpacity() {
            return -3;
        }

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

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

}
推荐问题
宣传栏