首页 > 解决方案 > 如何绘制圆角位图?

问题描述

我有从 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)

标签: androidkotlinbitmapandroid-custom-view

解决方案


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" />

推荐阅读