【发布时间】:2017-10-10 11:09:56
【问题描述】:
我正在尝试使用Rect 创建一个button。我已经成功创建了这个,但是我的图像和文本没有正确地居中。我想设置在精确的中心但无法实现。我需要通过Rect 执行此操作。请指导我,任何帮助将不胜感激。谢谢
这是我的代码 sn-p
RectF rightButton = new RectF(itemView.getRight() -
buttonWidthWithoutPadding, itemView.getTop(), itemView.getRight(), itemView.getBottom());
p.setColor(Color.parseColor("#F44336"));
c.drawRoundRect(rightButton, corners, corners, p);
drawView("DELETE", c, rightButton, p);
//draw view method
private void drawView(String text, Canvas c, RectF button, Paint p) {
float textSize = 20;
p.setColor(Color.WHITE);
p.setAntiAlias(true);
p.setTextSize(textSize);
float textWidth = p.measureText(text);
Bitmap bmp = drawableToBitmap(ContextCompat.getDrawable(mContext, R.drawable.delete_white_24dp));
c.drawBitmap(bmp, button.centerX() - (bmp.getWidth() / 2), button.centerY() - (bmp.getHeight()/2), null);
c.drawText(text, button.centerX() - (textWidth / 2), button.centerY() + bmp.getHeight(), p);
}
预期输出
我的输出(不完全在中心,图像和文本之间也没有空格
【问题讨论】: