search

Home  >  Q&A  >  body text

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;
    }
PHP中文网PHP中文网2772 days ago1063

reply all(5)I'll reply

  • 高洛峰

    高洛峰2017-04-17 16:51:13

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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:51:13

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

    reply
    0
  • PHPz

    PHPz2017-04-17 16:51:13

    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

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 16:51:13

    Inherit imageview directly and draw in ondraw!

    reply
    0
  • 黄舟

    黄舟2017-04-17 16:51:13

    Has the poster solved it? I also encountered it

    reply
    0
  • Cancelreply