Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 在图像中绘制文本时的新行大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_673_4@ 我有一个用户插入文本的应用程序,当他点击一个按钮时,它会在预先确定的图像中生成一个带有该文本的新图像并将其保存在手机上.

但有时文字太长而且超出了图像的宽度,所以我要做的就是把它分成新的一行.我该怎么办

我尝试使用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 – 在图像中绘制文本时的新行所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。