android 中图像上的中心文本?
在我的 Android 应用程序中,我需要在图像上显示文本。文本由用户在警报对话框中输入。我需要将这段文字放在图像底部的中心。我用这个在图像上绘制文本:
private Canvas drawTextImage(Bitmap b) {
Canvas c = new Canvas(b);
Paint paint = new Paint();
paint.setColor(getResources().getColor(R.color.orange));
paint.setStrokeWidth(30);
paint.setAntiAlias(true);
paint.setTextSize(40);
c.drawText(text, 350, 900, paint);
c.translate(300, 50);
return c;
}
我的alertDialg是这样的:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter a text ");
final EditText input = new EditText(this);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(25);
input.setFilters(FilterArray);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
text = input.getText().toString().trim();
Canvas c = new Canvas(bitmapResult);
drawTextImage(bitmapResult);
saveimage();
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
saveimage();
}
});
alert.show();
文本必须根据其长度居中。我该怎么做?
提前致谢..
In my android app I need to display a text on an image. The text is entered by user in an alertDialog. This text I need to center it on the bottom of the image. I draw the text on image with this :
private Canvas drawTextImage(Bitmap b) {
Canvas c = new Canvas(b);
Paint paint = new Paint();
paint.setColor(getResources().getColor(R.color.orange));
paint.setStrokeWidth(30);
paint.setAntiAlias(true);
paint.setTextSize(40);
c.drawText(text, 350, 900, paint);
c.translate(300, 50);
return c;
}
My alertDiallg is this :
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter a text ");
final EditText input = new EditText(this);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(25);
input.setFilters(FilterArray);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
text = input.getText().toString().trim();
Canvas c = new Canvas(bitmapResult);
drawTextImage(bitmapResult);
saveimage();
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
saveimage();
}
});
alert.show();
Tehe text has to be center according to it's length. How can I do this?
Thanks in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用于
绘制文字的颜料。
来源
编辑:在绘制文本中,您必须将图像的一半指定为x坐标(中心),y坐标应保持不变(位于底部的某个位置,具体取决于您想要的高度) 。
Use
on the paint thats used to draw your text.
Source
Edit: In draw text you have to specify half the with of your image as the x-coordinate (center), the y-coordinate should stay the same (somewhere along the bottom, depending how high you want it).