大佬教程收集整理的这篇文章主要介绍了android – 在图像中绘制文本时的新行,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
但有时候文字太长而且超出了图像的宽度,所以我要做的就是把它分成新的一行.我该怎么办?
我尝试使用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); }
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 – 在图像中绘制文本时的新行所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。