我正在尝试使用下面的代码显示文本.问题是文本不是水平居中的.当我设置坐标时drawText,它会在此位置设置文本的底部.我希望绘制文本,以便文本也水平居中.
这是一张进一步显示我的问题的图片:
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//canvas.drawRGB(2, 2, 200);
Paint textPaint = new Paint();
textPaint.setARGB(200, 254, 0, 0);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTypeface(font);
textPaint.setTextSize(300);
canvas.drawText("Hello", canvas.getWidth()/2, canvas.getHeight()/2 , textPaint);
}
Run Code Online (Sandbox Code Playgroud) 我有一个视图,我在onDraw(Canvas画布)方法中使用Canvas对象进行绘制.我的代码是:
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawPaint(paint);
paint.setColor(android.R.color.black);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);
Run Code Online (Sandbox Code Playgroud)
问题是文字没有透过背景显示,我做错了什么?如果我删除canvas.drawPaint(paint)和paint.setColor(android.R.color.black),你可以看到屏幕上的文字......
目标:纯画布上的Android> = 1.6.
假设我想编写一个绘制(宽度,高度)大红色矩形的函数,然后在里面绘制一个黑色的Hello World文本.我希望文本在视觉上位于矩形的中心.所以让我们试试:
void drawHelloRectangle(Canvas c, int topLeftX,
int topLeftY, int width, int height) {
Paint mPaint = new Paint();
// height of 'Hello World'; height*0.7 looks good
int fontHeight = (int)(height*0.7);
mPaint.setColor(COLOR_RED);
mPaint.setStyle(Style.FILL);
c.drawRect( topLeftX, topLeftY, topLeftX+width, topLeftY+height, mPaint);
mPaint.setTextSize(fontHeight);
mPaint.setColor(COLOR_BLACK);
mPaint.setTextAlign(Align.CENTER);
c.drawText( "Hello World", topLeftX+width/2, ????, mPaint);
}
Run Code Online (Sandbox Code Playgroud)
现在我不知道在标记为drawText的参数中放什么????,即我不知道如何垂直对齐文本.
就像是
???? = topLeftY + height/2 + fontHeight/2 - fontHeight/8;
似乎或多或少有效,但必须有更好的方法.
对于我的Android游戏,我有一些电话Canvas.drawText().
为了测试,我使用标准的字体大小似乎工作正常.
但是,当我将分辨率提高到更高的密度时,会自动加载较大的图像,但文本现在非常小.
有没有一种简单的方法来计算应该绘制文本的大小,还是我必须手动执行此操作?
我正在使用Canvas创建一个带有一些背景和一些文本的Drawable.drawable用作EditText内的复合drawable.
文本是通过画布上的drawText()绘制的,但在某些情况下,我确实遇到了绘制文本的y位置问题.在这些情况下,某些字符的部分被截断(参见图像链接).
没有定位问题的字符:
http://i50.tinypic.com/zkpu1l.jpg
有定位问题的字符,文字包含'g','j','q'等:
http://i45.tinypic.com/vrqxja.jpg
您可以找到一个代码段来重现下面的问题.
有没有专家知道如何确定y位置的正确偏移量?
public void writeTestBitmap(String text, String fileName) {
// font size
float fontSize = new EditText(this.getContext()).getTextSize();
fontSize+=fontSize*0.2f;
// paint to write text with
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.DKGRAY);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.SERIF);
paint.setTextSize((int)fontSize);
// min. rect of text
Rect textBounds = new Rect();
paint.getTextBounds(text, 0, text.length(), textBounds);
// create bitmap for text
Bitmap bm = Bitmap.createBitmap(textBounds.width(), textBounds.height(), Bitmap.Config.ARGB_8888);
// canvas
Canvas canvas = new Canvas(bm);
canvas.drawARGB(255, 0, 255, 0);// for visualization
// y …Run Code Online (Sandbox Code Playgroud) import PIL
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import urllib.request
with urllib.request.urlopen('http://pastebin.ca/raw/2311595') as in_file:
hex_data = in_file.read()
print(hex_data)
img = Image.frombuffer('RGB', (320,240), hex_data) #i have tried fromstring
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf",14)
draw.text((0, 220),"This is a test11",(255,255,0),font=font)
draw = ImageDraw.Draw(img)
img.save("a_test.jpg")
Run Code Online (Sandbox Code Playgroud)
即时尝试将二进制转换为图像,然后绘制文本.但我得到错误:
img = Image.frombuffer('RGB', (320,240), hex_data)
raise ValueError("not enough image data")
ValueError: not enough image data
Run Code Online (Sandbox Code Playgroud)
我已经在这里http://pastebin.ca/raw/2311595上传了字节字符串,
图片大小我可以肯定是320x240
这里是我可以肯定的图片字节字符串是来自320x240,只需运行代码就会从字节字符串中创建一个图像
import urllib.request
import binascii
import struct
# Download the data. …Run Code Online (Sandbox Code Playgroud) 我目前正在创建一个图像编辑器,并尝试使用canvas.drawText()在图像上绘制文本.到目前为止,我已经成功地做到了这一点,但是当用户输入的文本太长时,文本只会继续在页面外的一行上,并且不会将自身包裹到屏幕的宽度.我该怎么做呢?我尝试过使用静态布局,但似乎无法让它工作,有没有人有一个教程来做到这一点?
我使用静态布局绘制画布的功能:
public Bitmap createImage(float scr_x,float scr_y,String user_text){
Canvas canvas = new Canvas(image);
scr_x = 100;
scr_y = 100;
final TextPaint tp = new TextPaint(Color.WHITE);
canvas.save();
StaticLayout sl = new StaticLayout("" + user_text, tp, originalBitmap.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
sl.draw(canvas);
return image;
}
Run Code Online (Sandbox Code Playgroud)
好的,我已经更新了我的代码,但是当我尝试在图像上绘制时根本没有任何事情发生,我不知道为什么:
public Bitmap createImage(String user_text) {
// canvas object with bitmap image as constructor
Canvas canvas = new Canvas(image);
TextPaint tp = new TextPaint();
tp.setColor(Color.RED);
tp.setTextSize(50);
tp.setTextAlign(Align.CENTER);
tp.setAntiAlias(true);
StaticLayout sl = new StaticLayout("" + user_text, tp,
canvas.getWidth(), …Run Code Online (Sandbox Code Playgroud) 我正在使用WPF创建大量文本DrawText,然后将它们添加到单个文本中Canvas.
我需要在每个MouseWheel事件中重绘屏幕并且我意识到性能有点慢,所以我测量了创建对象的时间并且它不到1毫秒!
那可能是什么问题呢?很久以前,我想我读到的地方实际上是Rendering花费时间,而不是创建和添加视觉效果.
这是我用来创建文本对象的代码,我只包含了基本部分:
public class ColumnIdsInPlan : UIElement
{
private readonly VisualCollection _visuals;
public ColumnIdsInPlan(BaseWorkspace space)
{
_visuals = new VisualCollection(this);
foreach (var column in Building.ModelColumnsInTheElevation)
{
var drawingVisual = new DrawingVisual();
using (var dc = drawingVisual.RenderOpen())
{
var text = "C" + Convert.ToString(column.GroupId);
var ft = new FormattedText(text, cultureinfo, flowdirection,
typeface, columntextsize, columntextcolor,
null, TextFormattingMode.Display)
{
TextAlignment = TextAlignment.Left
};
// Apply Transforms
var st = new ScaleTransform(1 / scale, …Run Code Online (Sandbox Code Playgroud) 我想在google maps api上创建一个包装文本.我已经能够用很多代码完成这项工作,但一直在努力改进.我最近的尝试是使用StaticLayout类和文本包装,但我不知道如何定位它...无论我尝试它总是从屏幕的左上角开始....
我正在尝试水平和垂直对齐位图中的文本,我读了几篇文章,但我找不到解决方案.位图是一个简单的圆形图像.我发布了当前的代码.它或多或少都有效,但是文本没有完全居中,它看起来有点在左边,有点在顶部,我的意思是我似乎必须添加一个偏移来将它移动到右边和底部.
public static float convertDpToPixel(float dp, Context context) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}
v = (ImageView) findViewById(R.id.imageView1);
Bitmap b = BitmapFactory.decodeResource(getResources(),
R.drawable.marker).copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(b);
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setARGB(255, 0, 0, 0);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setTextSize(convertDpToPixel(9, this));
String text = "30";
int xPos = (canvas.getWidth() / 2);
int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() +
textPaint.ascent()) / 2)) ; …Run Code Online (Sandbox Code Playgroud)