我有一些代码,我在位图(画布)上绘制我的文字
canvas.drawTextOnPath(Text, textPath[count], gipa, -10, text);
Run Code Online (Sandbox Code Playgroud)
请告诉我,可以在路径(textPath)中使用背景颜色绘制此文本吗?
它只是绘制文本的全部功能
public void drawText(float x,float y ,String Text,Canvas canvas,Paint paint1 ,int count )
{
float xren =text.measureText(Text.trim());
canvas.drawTextOnPath(Text, textPath[count], gipa, -10, text);
}
Run Code Online (Sandbox Code Playgroud)
使用此功能我在画布上绘制文字.那么如何修改这个函数来用背景绘制这个文本?
eye*_*yus 20
这里很可能需要两个步骤.您将首先沿路径绘制一条线,背景颜色,然后按照指示绘制文本.使用绘制对象设置线条的粗细.此外,改变油漆的风格可以有助于产生效果.尝试FILL,STROKE或FILL_AND_STROKE为不同的效果.
mpaint.setStyle(Paint.Style.STROKE);
mpaint.setStrokeWidth(strokeWidth);
Run Code Online (Sandbox Code Playgroud)
添加样本以绘制红色的路径(矩形):
Paint mPaint = new Paint();
mPaint.setColor(Color.RED);
Path mPath = new Path();
RectF mRectF = new RectF(20, 20, 240, 240);
mPath.addRect(mRectF, Path.Direction.CCW);
mPaint.setStrokeWidth(20);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(mPath, mPaint);
Run Code Online (Sandbox Code Playgroud)
然后沿同一路径绘制文本(蓝色):
mPaint.setColor(Color.BLUE);
mPaint.setStrokeWidth(0);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setTextSize(20);
canvas.drawTextOnPath("Draw the text, with origin at (x,y), using the specified paint, along the specified path.", mPath, 0, 5, mPaint);
Run Code Online (Sandbox Code Playgroud)

如果你想这样做,那么实现下面的代码片段:
/**
* PUT THIS METHOD FOR IMPLEMENT WATER-MARK IN COMMON FILE
*/
public static Bitmap waterMark(Bitmap src, String watermark) {
//get source image width and height
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
Paint.FontMetrics fm = new Paint.FontMetrics();
paint.setColor(Color.WHITE);
paint.getFontMetrics(fm);
int margin = 5;
canvas.drawRect(50 - margin, 50 + fm.top - margin,
50 + paint.measureText(watermark) + margin, 50 + fm.bottom
+ margin, paint);
paint.setColor(Color.RED);
canvas.drawText(watermark, 50, 50, paint);
return result;
}
Run Code Online (Sandbox Code Playgroud)
// 从 URI 中获取位图:
private Bitmap getBitmapFromUri(String photoPath) {
Bitmap image = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
return bitmap;
}
Run Code Online (Sandbox Code Playgroud)
// 保存图片 :
private String SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/shareImage");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 20, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return file.getAbsolutePath();
}
Run Code Online (Sandbox Code Playgroud)
// 像这样调用:
Bitmap bitmap = getBitmapFromUri(attachment.get(i).getPath()); // Enter here your Image path
Bitmap bitmapp = waterMark(bitmap, "ENTER YOUR TEXT FOR WATERMARK LABEL");
String path = SaveImage(bitmapp);
Uri uri = Uri.fromFile(new File(path));
Run Code Online (Sandbox Code Playgroud)
最后从 uri 你可以得到一个新的水印图像。
希望这对你有帮助。
我相信这个解决方案比drawPath.
用它来计算文本背景的大小:
private @NonNull Rect getTextBackgroundSize(float x, float y, @NonNull String text, @NonNull TextPaint paint) {
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
float halfTextLength = paint.measureText(text) / 2 + 5;
return new Rect((int) (x - halfTextLength), (int) (y + fontMetrics.top), (int) (x + halfTextLength), (int) (y + fontMetrics.bottom));
}
Run Code Online (Sandbox Code Playgroud)
然后将背景绘制为Rect:
Rect background = getTextBackgroundSize(x, y, text, textPaint);
canvas.drawRect(background, bkgPaint);
canvas.drawText(text, x, t, textPaint);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17891 次 |
| 最近记录: |