圓角可以使用棒棒糖的輪廓和前棒棒堂的路徑來完成。請參閱:
準備口罩
if (cornerRadius > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setClipToOutline(true);
setOutlineProvider(ShadowShape.viewOutlineProvider);
} else {
cornersMask = new Path();
cornersMask.addRoundRect(new RectF(0, 0, getWidth(), getHeight()), cornerRadius, cornerRadius, Path.Direction.CW);
cornersMask.setFillType(Path.FillType.INVERSE_WINDING);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
setOutlineProvider(ViewOutlineProvider.BOUNDS);
}
平局(Canvas)的方法
if (cornerRadius > 0 && getWidth() > 0 && getHeight() > 0 && Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT_WATCH) {
int saveFlags = Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, saveFlags);
super.draw(canvas);
paint.setXfermode(pdMode);
canvas.drawPath(cornersMask, paint);
canvas.restoreToCount(saveCount);
paint.setXfermode(null);
} else {
super.draw(canvas);
}
和失蹤viewOutlineProvider
viewOutlineProvider = new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
ShadowShape shadowShape = ((ShadowView) view).getShadowShape();
if (shadowShape == RECT) {
outline.setRect(0, 0, view.getWidth(), view.getHeight());
} else if (shadowShape == ROUND_RECT) {
outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), ((CornerView) view).getCornerRadius());
} else if (shadowShape == CIRCLE) {
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
}
};
而且圖像:
你可以亂用這個代碼在任何你想要的方式。您可以將圖像和背景分別切割成任意形狀,也可以一起切割。欲瞭解更多詳情,請查閱github上的代碼。
發佈您正在使用的代碼 – Blackbelt
您是否嘗試在xml文件中爲您設置'android:background =「@ android:color/transparent」'ImageView'標記? – mbelsky
@mbelsky我想要一個背景顏色,實際上。 – mrmoment