【发布时间】:2017-09-02 17:10:32
【问题描述】:
您好,我正在尝试在多行文本上添加描边,但它不起作用 这是代码 首先绘制填充文本它的工作正常 但是当尝试绘制笔画时它不起作用并给我一些与笔画无关的东西
我需要这样的笔划,但需要多行文字 stroke canvas
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextSize(20);
textPaint.setColor(Color.White);
textPaint1.setStyle(Paint.Style.STROKE);
textPaint1.setStrokeWidth(20);
textPaint1.setColor(Color.GREEN);
StaticLayout sl = new StaticLayout(
textLayer.getText(), // - text which will be drawn
textPaint,
boundsWidth, // - width of the layout
Layout.Alignment.ALIGN_CENTER, // - layout alignment
1, // 1 - text spacing multiply
1, // 1 - text spacing add
true); // true - include padding
int boundsHeight = sl.getHeight();
int bmpHeight = (int) (canvasHeight * Math.max(TextLayer.Limits.MIN_BITMAP_HEIGHT,
1.0F * boundsHeight / canvasHeight));
// create bitmap where text will be drawn
Bitmap bmp;
if (reuseBmp != null && reuseBmp.getWidth() == boundsWidth
&& reuseBmp.getHeight() == bmpHeight) {
// if previous bitmap exists, and it's width/height is the same - reuse it
bmp = reuseBmp;
bmp.eraseColor(Color.TRANSPARENT); // erase color when reusing
} else {
bmp = Bitmap.createBitmap(boundsWidth, bmpHeight, Bitmap.Config.ARGB_8888);
}
StaticLayout sl1 = new StaticLayout(
textLayer.getText(), // - text which will be drawn
textPaint1,
boundsWidth, // - width of the layout
Layout.Alignment.ALIGN_CENTER, // - layout alignment
1, // 1 - text spacing multiply
1, // 1 - text spacing add
true); // true - include padding
// calculate height for the entity, min - Limits.MIN_BITMAP_HEIGHT
int boundsHeight1 = sl1.getHeight();
int bmpHeight1 = (int) (canvasHeight * Math.max(TextLayer.Limits.MIN_BITMAP_HEIGHT,
1.0F * boundsHeight1 / canvasHeight));
Bitmap bmp1;
if (reuseBmp != null && reuseBmp.getWidth() == boundsWidth
&& reuseBmp.getHeight() == bmpHeight1) {
// if previous bitmap exists, and it's width/height is the same - reuse it
bmp1 = reuseBmp;
bmp1.eraseColor(Color.TRANSPARENT); // erase color when reusing
} else {
bmp1 = Bitmap.createBitmap(boundsWidth, bmpHeight, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bmp);
canvas.save();
Canvas canvas1 = new Canvas(bmp1);
canvas1.save();
// move text to center if bitmap is bigger that text
if (boundsHeight < bmpHeight) {
//calculate Y coordinate - In this case we want to draw the text in the
//center of the canvas so we move Y coordinate to center.
float textYCoordinate = (bmpHeight - boundsHeight) / 2;
canvas.translate(0, textYCoordinate);
}
// move text to center if bitmap is bigger that text
if (boundsHeight1 < bmpHeight1) {
//calculate Y coordinate - In this case we want to draw the text in the
//center of the canvas so we move Y coordinate to center.
float textYCoordinate = (bmpHeight1 - boundsHeight1) / 2;
canvas1.translate(0, textYCoordinate);
}
//draws static layout on canvas
sl.draw(canvas);
sl1.draw(canvas1);
canvas.restore();
canvas1.restore();
这可以组合位图
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas2 = new Canvas(bmOverlay);
canvas2.drawBitmap(bmp, new Matrix(), null);
canvas2.drawBitmap(bmp1, 0, 0, null);
return bmOverlay;
return bmp;
【问题讨论】:
-
您找到解决方案了吗..? @Rft.hko
标签: android canvas bitmap android-canvas draw