android 自定义圆角ImageView,后设置scaleType="centerCrop"无效?
PHP中文网
PHP中文网 2017-04-17 16:49:13
0
5
1062
如下就是关键部分代码:如何才能实现圆角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;
    }
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(5)
小葫芦

https://github.com/hdodenhof/CircleImageView Just push it, work requires getting on the wheel, learning, and chewing the wheel source code.

Ty80

Some thoughts: https://segmentfault.com/a/1190000004646559

PHPzhong

How do you use this? Call this method first and then setBitmap to ImageView? If so, it may not be possible! Because centerCrop first enlarges the bitmap width and height to be greater than or equal to the width and height of ImageView, and then displays the middle part! In this way, if you zoom in and then crop, the original rounded corners may exceed the range of the control and not be displayed

Peter_Zhu

Inherit imageview directly and draw in ondraw!

黄舟

Has the poster solved it? I also encountered it

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template