【问题标题】:How to draw bitmap with rounded corners?如何绘制圆角位图?
【发布时间】:2020-04-28 17:56:06
【问题描述】:

我有从 url 获取并放入位图中的图像。现在我需要用画布绘制这个位图,并且必须是圆角的。

附言

ImageView 对我来说不是这里的选项,因为应该绘制很多这样的图像。

var url = "someurl"
var mediaMetadataRetriever = MediaMetadataRetriever()
mediaMetadataRetriever.setDataSource(url, HashMap<String, String>())
var frame:Bitmap = mediaMetadataRetriever.getFrameAtTime(time)          // get image from url 
var rect = Rect(left, top, right, bottom)                               // coordinates for bitmap
canvas.drawBitmap(frame, null, rect, paint)

【问题讨论】:

    标签: android kotlin bitmap android-custom-view


    【解决方案1】:
    public class CustomImageView extends ImageView {
    public static float cornerRadius = 19.0f; 
    public CustomImageView(Context context) {
        super(context);
    }
    
    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        Path clipPath = new Path();
        RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
        clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
    }
    

    }

    <CustomImageView
                android:id="@+id/selectIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多