Я рисую круглую форму, используя Path.
И использовать какой-то механизм для подгонки изображения в круглой части.
Я сделал с помощью Path. Проверьте это.
public class RoundedView extends AppCompatImageView {
private Bitmap mBitmap;
Path mPath;
float width, height, offset;
float percent = 0.37f;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public RoundedView(Context context) {
super(context);
}
public RoundedView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public RoundedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.RoundedView);
percent = arr.getFloat(R.styleable.RoundedView_roundPercent, 0.37f);
arr.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
if (mPath != null) {
canvas.drawPath(mPath, mPaint);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
offset = w / 2f;
change();
}
private void change() {
float roundness = percent * height;
Drawable d = getDrawable();
if (d != null) {
mBitmap = drawableToBitmap(d);
mBitmap = Bitmap.createScaledBitmap(mBitmap, (int) width, (int) height, false);
final Shader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
mPath = new Path();
mPath.moveTo(0, 0);
mPath.lineTo(width, 0);
mPath.lineTo(width, height - roundness);
mPath.quadTo(offset, height + roundness, 0, height - roundness);
mPath.close();
}
}
@Override
public void setImageDrawable(@Nullable Drawable drawable) {
super.setImageDrawable(drawable);
change();
}
public Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if (bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}