有一篇很棒的教程,分为四部分,介绍了如何实现不规则形状,可以在Styling Android博客上找到。
编辑:
我已经修改了教程第二部分的代码并实现了该效果:
private Bitmap processImage(Bitmap bitmap) {
Bitmap bmp;
bmp = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bitmap,
BitmapShader.TileMode.CLAMP,
BitmapShader.TileMode.CLAMP);
float radius = bitmap.getWidth() / 2f;
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
RectF rect = new RectF(-bitmap.getWidth() / 2f, 0,
bitmap.getWidth() / 2f, bitmap.getHeight());
canvas.drawOval(rect, paint);
return bmp;
}
我刚刚将代码末尾的drawRoundRect
替换为drawOval
,它基本上绘制了一个圆形,其中一半超出了画布。
canvas.drawOval(rectRedFrame, paintRedFrame);
canvas.drawOval(rect, paint);
- Ashkan Sarlak