Android 安卓:椭圆形可拉拔,边缘剪裁
我正在尝试创建一个带有边框的Android 安卓:椭圆形可拉拔,边缘剪裁,android,android-shapedrawable,Android,Android Shapedrawable,我正在尝试创建一个带有边框的OvalShapeDrawable。为了给它一个提升的效果,我在边界周围添加了阴影 相关代码: class Badge extends ShapeDrawable{ void Badge(){ borderPaint = new Paint(); borderPaint.setColor(borderColor); borderPaint.setStyle(Paint.Style.STROKE); borderPaint.setStr
OvalShape
Drawable
。为了给它一个提升的效果,我在边界周围添加了阴影
相关代码:
class Badge extends ShapeDrawable{
void Badge(){
borderPaint = new Paint();
borderPaint.setColor(borderColor);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderThickness);
borderPaint.setAntiAlias(true);
borderPaint.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Rect r = getBounds();
// draw border if needed
if (borderThickness > 0) {
drawBorder(canvas);
}
int count = canvas.save();
canvas.translate(r.left, r.top);
// draw text inside badge
int width = this.width < 0 ? r.width() : this.width;
int height = this.height < 0 ? r.height() : this.height;
int fontSize = this.fontSize < 0 ? (Math.min(width, height) / 2) : this.fontSize;
textPaint.setTextSize(fontSize);
Rect textBounds = new Rect();
textPaint.getTextBounds(text, 0, text.length(), textBounds);
canvas.drawText(text, width / 2, height / 2 - textBounds.exactCenterY(), textPaint);
canvas.restoreToCount(count);
}
private void drawBorder(Canvas canvas) {
RectF rect = new RectF(getBounds());
rect.inset(borderThickness / 2, borderThickness / 2);
canvas.drawOval(rect, borderPaint);
}
}
class徽章扩展了ShapeDrawable{
无效徽章(){
borderPaint=新油漆();
borderPaint.setColor(borderColor);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.设置行程宽度(边界厚度);
borderPaint.setAntiAlias(true);
borderPaint.setShadowLayer(4.0f、0.0f、2.0f,彩色。黑色);
}
@凌驾
公共空白绘制(画布){
超级绘画(画布);
Rect r=getBounds();
//如有需要,画边界
如果(边界厚度>0){
拉边(帆布);
}
int count=canvas.save();
canvas.translate(右左,右上);
//在徽章内绘制文本
int width=this.width<0?r.width():this.width;
int height=this.height<0?r.height():this.height;
int-fontSize=this.fontSize<0?(数学最小值(宽度、高度)/2):this.fontSize;
textPaint.setTextSize(fontSize);
Rect textBounds=new Rect();
textPaint.getTextBounds(text,0,text.length(),textBounds);
drawText(text,width/2,height/2-textBounds.exactCenterY(),textPaint);
canvas.restoreToCount(count);
}
专用void绘图边框(画布){
rectfrect=newrectf(getBounds());
矩形插图(borderThickness/2,borderThickness/2);
画布。drawOval(矩形、边框绘制);
}
}
但是,正如您在图像中看到的那样,绘制的边界在边缘处被剪裁。此外,随着可拉伸的尺寸减小,边缘会被修剪得更多
我应该在代码中做什么修改来实现一个完美的圆?它似乎不适合你的画布,你是否尝试过缩放你的椭圆小图标?@OrkunKoçyiğIt:我尝试过摆弄rect.inset()。如何进行缩放?我现在无法尝试,但您能否将clipChildren=“false”和android:clipToPadding=“false”添加到包含您的形状的布局以及包含您的布局且具有相同大小的任何其他布局。@OrkunKoçyiğit:我尝试了您的建议。不幸的是,它没有任何效果。它似乎不适合你的画布,你试过缩放你的椭圆形小地图吗?@OrkunKoçyiğit:我试过摆弄rect.inset()。如何进行缩放?我现在无法尝试,但您能否将clipChildren=“false”和android:clipToPadding=“false”添加到包含您的形状的布局以及包含您的布局且具有相同大小的任何其他布局。@OrkunKoçyiğit:我尝试了您的建议。不幸的是,它没有效果。