
但有时候文字太长而且超出了图像的宽度,所以我要做的就是把它分成新的一行.我该怎么办?
我尝试使用breakText,但我不确定如何使用它……我正在使用:
textPaint.breakText(text[2],true,bmp.getWIDth(),null);
但它没有用.
此外,当我手动在EditText中断行时,它只显示一行中的所有内容,并使用“[]”,其中第二行应该开始…
编辑:我的代码原始代码:
private voID SaveMyImage() { // Todo auto-generated method stub file myDir = new file(Environment.getExternalStorageDirectory().getPath()+"/App/"); myDir.mkdirs(); file file = new file (myDir,fname); if (file.exists ()) file.delete (); try { fileOutputStream out = new fileOutputStream(file); Canvas canvas = new Canvas(bmp); Paint textPaint = new Paint(Paint.ANTI_AliAS_FLAG); if (text[0].equals("Image 01")) { textPaint.setcolor(color.BLACK); } else { textPaint.setcolor(color.WHITE); } textPaint.setTextAlign(Align.CENTER); textPaint.setTextSize(tamanho); textPaint.setShadowLayer(2,2,color.BLACK); textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text OverlapPing Pattern canvas.drawBitmap(bmp,null); canvas.drawText(text[1],largura,altura2,textPaint); canvas.drawText(text[2],altura,textPaint); bmp.compress(Bitmap.CompressFormat.JPEG,90,out); out.flush(); out.close(); Toast.makeText(Saveimg.this,"Image saved on phone",Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printstacktrace(); } sendbroadcast(new Intent( Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStorageDirectory()))); uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/App/"+fname); pronto.setimageURI(uri);}解决方法 breatText返回字符串中的字符数,如果在被截断之前可以显示.我建议在一个循环中调用它.删除它可以适合的许多字符,并在每次迭代时将它们放在一个字符串中,直到源文本为空: ArrayList<String> lines = new ArrayList<String>();String test = text[2];while(!test.isEmpty()){ int newLength = textPaint.breakText(test,null); lines.add(test.substring(0,newLength)); test = test.substring(newLength);} 至于打印多行.我假设你正在使用Canvas.drawText does not seem to support line breaks. So you’ll need to draw each line separately with different Y-Values. (Code adapted from here):
Rect bounds = new Rect();int yoff = 0;for(String line:lines){ canvas.drawText(line,x,y + yoff,paint); textPaint.getTextBounds(line,line.length(),bounds); yoff += bounds.height();} 编辑我没有在你的代码中看到你按照我的描述实际拆分字符串.如果你没有真正告诉我你是如何实现它的话,我无法诊断为什么我的解决方案不起作用.
虽然我在这里工作,但我想我可以告诉你如何修复错误.如果你想多次这样做,那么为它编写一个方法是个好主意.将以下方法添加到您的类:
public voID splitAndDrawlines(Canvas canvas,String text,int x,int y,Paint textPaint,int wIDth){ ArrayList<String> lines = new ArrayList<String>(); String test = text; while(!test.isEmpty()){ int newLength = textPaint.breakText(test,canvas.getWIDth(),null); lines.add(test.substring(0,newLength)); test = test.substring(newLength); } Rect bounds = new Rect(); int yoff = 0; for(String line:lines){ canvas.drawText(line,textPaint); textPaint.getTextBounds(line,bounds); yoff += bounds.height(); }} 替换此代码:
canvas.drawText(text[1],textPaint);canvas.drawText(text[2],textPaint);
使用此代码:
this.splitAndDrawlines(canvas,text[1],textPaint);this.splitAndDrawlines(canvas,text[2],textPaint);
编辑2:
这是我用来设置代码的代码:
// Create a 100x100 bitmap bmp = Bitmap.createBitmap(100,100,Bitmap.Config.ARGB_8888); // Set the height of the text to 12. this.tamanho = 12f; // Draw the text in the mIDdle of the picture wIDth-wise. this.largura = bmp.getWIDth() / 2; // Text parameters this.text = new String[]{"MAKE THE TEXT WHITE","This text starts in the mIDdle of the mIDdle is too long and will be split","Short text at the top of the image"}; // Start one line size into the picture height-wise. this.altura = this.tamanho; // Start in the mIDdle of the picture height-wise. this.altura2 = bmp.getHeight()/2; // Output file name. this.fname = "TEST.jpg"; // Save the image SaveMyImage(); 总结 以上是内存溢出为你收集整理的android – 在图像中绘制文本时的新行全部内容,希望文章能够帮你解决android – 在图像中绘制文本时的新行所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)