【问题标题】:Convert String text to Bitmap将字符串文本转换为位图
【发布时间】:2012-02-06 15:06:27
【问题描述】:

是否可以将EditText 框中的字符串文本转换为位图?换句话说,有没有办法将字符串文本转换为位图,这意味着文本将显示为图像?

以下是我的代码:

class TextToImage extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        //create String object to be converted to image
        String sampleText = "SAMPLE TEXT";
        String fileName = "Image";

        //create a File Object
        File newFile = new File("./" + fileName + ".jpeg");

        //create the font you wish to use
        Font font = new Font("Tahoma", Font.PLAIN, 11);

        //create the FontRenderContext object which helps us to measure the text
        FontRenderContext frc = new FontRenderContext(null, true, true);
    }
}

【问题讨论】:

    标签: android image bitmap


    【解决方案1】:

    您可以创建一个适当大小的位图,为该位图创建一个画布,然后在其中绘制您的文本。您可以使用 Paint 对象来测量文本,以便了解位图所需的大小。你可以这样做(未经测试):

    public Bitmap textAsBitmap(String text, float textSize, int textColor) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(textSize);
        paint.setColor(textColor);
        paint.setTextAlign(Paint.Align.LEFT);
        float baseline = -paint.ascent(); // ascent() is negative
        int width = (int) (paint.measureText(text) + 0.5f); // round
        int height = (int) (baseline + paint.descent() + 0.5f);
        Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(image);
        canvas.drawText(text, 0, baseline, paint);
        return image;
    }
    

    【讨论】:

    • @shyam - 这些都是 Android 类,而不是 AWT 类。它们在包装中android.graphics
    • 嘿 Ted Hopp 我使用 canvas.drawText() 在画布上绘制文本现在我想在触摸文本时移动它意味着在手指运动上移动文本..我有移动图像的代码画布表面..如果可以将文本作为图像,那么我可以将其用作图像并在屏幕上移动文本..那么如何将文本转换为位图以便在屏幕上移动?
    • 油漆油漆=新油漆(); paint.setColor(Color.RED);油漆.setTextSize(16); paint.setAntiAlias(true); paint.setTypeface(Typeface.MONOSPACE); // 用黑色填充画布 c.drawText("shyam ji ", 10, 16, paint);
    • 对代码的两个修复:1)使用文本上升绝对值(即Math.abs(paint.ascent())) 2)使文本左对齐(即paint.setTextAlign(Paint.Align.左))
    • @RuslanYanchyshyn - 感谢您抓到这些!现已修复。
    【解决方案2】:

    我已经修改了@TedHopp 的答案,以确保创建的图像始终为正方形,这取决于图像的显示位置,例如@987654322 @图标之类的。文本在图像中间水平居中。

    public static Bitmap textAsBitmap(String text, float textSize, int textColor) {
        // adapted from https://stackoverflow.com/a/8799344/1476989
        Paint paint = new Paint(ANTI_ALIAS_FLAG);
        paint.setTextSize(textSize);
        paint.setColor(textColor);
        paint.setTextAlign(Paint.Align.LEFT);
        float baseline = -paint.ascent(); // ascent() is negative
        int width = (int) (paint.measureText(text) + 0.0f); // round
        int height = (int) (baseline + paint.descent() + 0.0f);
    
        int trueWidth = width;
        if(width>height)height=width; else width=height;
        Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    
        Canvas canvas = new Canvas(image);
        canvas.drawText(text, width/2-trueWidth/2, baseline, paint);
        return image;
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

          public static Bitmap drawText(String text, int textWidth, int textSize) {
      // Get text dimensions
      TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG
      | Paint.LINEAR_TEXT_FLAG);
      textPaint.setStyle(Paint.Style.FILL);
      textPaint.setColor(Color.BLACK);
      textPaint.setTextSize(textSize);
      StaticLayout mTextLayout = new StaticLayout(text, textPaint,
      textWidth, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
      
      // Create bitmap and canvas to draw to
      Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Config.RGB_565);
      Canvas c = new Canvas(b);
      
      // Draw background
      Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG
      | Paint.LINEAR_TEXT_FLAG);
      paint.setStyle(Paint.Style.FILL);
      paint.setColor(Color.WHITE);
      c.drawPaint(paint);
      
      // Draw text
      c.save();
      c.translate(0, 0);
      mTextLayout.draw(c);
      c.restore();
      
      return b;
      }
      

      【讨论】:

        【解决方案4】:
        String text = "Hello world!";
        Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        c.drawBitmap(b, 0, 0, null);
        TextPaint textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(16.0F);
        StaticLayout sl= new StaticLayout(text, textPaint, b.getWidth()-8, Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
        c.translate(6, 40);
        sl.draw(c);
        return b
        

        【讨论】:

          【解决方案5】:

          对于我不知道的唯一字符串,

          你会得到Bitmap image of the whole EditText不仅有这个字符串,

          mEditText.setCursorVisible(false); 
          mEditText.buildDrawingCache(); 
          Bitmap bmp = Bitmap.createBitmap(mEditText.getDrawingCache()); 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-12-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-29
            • 1970-01-01
            • 2014-08-24
            相关资源
            最近更新 更多