android - 如何绘制圆角位图?
问题描述
我有从 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)
解决方案
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" />
推荐阅读
- ios - iOS 中曲线视图的点击问题
- sahi - Sahi 在单击打印按钮时无法打开 pdf 文件
- git - 为什么`git log --reverse --left-only --cherry-pick`没有按预期工作?
- parsing - ANTLR4 解析包含关键字的变量名
- javascript - 当我在同一个组件中同时使用 Router 和 Context 时,React 抛出错误
- c# - C# GeoLocation Watcher 获取坐标的时间太长
- laravel - 在没有本地服务的情况下提供 Laravel 外部 OAuth 服务是一种不好的做法吗?
- javascript - Framework7:超过 2 页的路由问题
- java - 根据客户端请求将数据流式传输到 Web 套接字
- javascript - React 状态在其分配的变量发生变化时发生变化