java - 直接在指定位图下绘制文本

标签 java android bitmap android-recyclerview java-canvas

p.setColor(Color.parseColor("#D32F2F"));
RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
c.drawRect(background, p);
icon = getBitmapFromVectorDrawable(MainActivity.this, R.drawable.ic_clear);
RectF icon_dest = new RectF((float) itemView.getRight() - 2 * width, (float) itemView.getTop() + width, (float) itemView.getRight() - width, (float) itemView.getBot
c.drawBitmap(icon, null, icon_dest, p); //[1]
p.setTextSize(50);
p.setColor(Color.WHITE);
c.drawText("Cancel",*/Insert x and y here*/,p);

到目前为止我已经做到了。

我正在尝试直接在我绘制的位图下drawText。 ([1])。但是我不确定如何获取位图的坐标并调整它以使其位于其下方的中心。

image

上图是我想要实现的目标。 该应用的内容已被审查。

总之,如何获取我想要获取的文本的坐标?

我正在将 ItemTouchHelperonChildDraw 方法用于 RecyclerView。

最佳答案

您可以使用paint.getTextBounds()来获取要在 Canvas 上绘制的文本的边界。获得文本的边界后,您可以根据图标的位置计算绘制文本的位置。

编辑:

这会将图标下方的文本对齐到与图标相同的 X 位置:

float textX = icon_dest.left;
float textY = icon_dest.bottom + some_padding;

canvas.drawText("Cancel", textX, textY, textPaint);

这会将图标的中心和文本的中心对齐在同一垂直线上:

Rect textBounds = new Rect();
textPaint.getTextBounds("Cancel", 0, length, textBounds);

float iconCenterX = icon_dest.left + (icon_dest.width() / 2);
float textHalfWidth = (textBounds.right - textBounds.left) / 2;

float textX = iconCenterX - textHalfWidth;
float textY = icon_dest.bottom + somePadding

canvas.drawText("Cancel", textX, textY, textPaint);

关于java - 直接在指定位图下绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45521458/

相关文章:

java - 如何在 Java 中使用枚举作为数组下标

java - JPA 映射模型返回带有复合键的空元素

android - SKMaps 2.5.1 的导航建议始终为英文

Android 检查视频流结束

android - : Landroid/arch/core/executor/AppToolkitTaskExecutor 解析失败

java - Android 位图 - 内存不足

使用 C 复制从 tif 文件生成的 8 位灰度 bmp 文件

java - 读取多列文本文件并将其拆分为数组

Java二叉搜索树插入方法不起作用

bitmap - OCR:如何提高准确性-现有的库,用于删除非文本'furniture',形状等,以避免混淆OCR?