Android 保存前向位图添加文本

Android 保存前向位图添加文本,android,canvas,bitmap,paint,drawtext,Android,Canvas,Bitmap,Paint,Drawtext,我正在将一个可绘制的位图转换为一个可静音的位图,创建一个画布并在其上绘制文本,将画布添加到图像中,然后将位图保存到一个文件中。它正在正确保存图像,但文本不在保存的图像上。我看了其他答案并尝试了一下,但似乎没有任何效果 代码如下: public void createTitleImageFromDrawable(){ // Create file object for saving the the bitmap File file = new File(MainActivit

我正在将一个可绘制的位图转换为一个可静音的位图,创建一个画布并在其上绘制文本,将画布添加到图像中,然后将位图保存到一个文件中。它正在正确保存图像,但文本不在保存的图像上。我看了其他答案并尝试了一下,但似乎没有任何效果

代码如下:

  public void createTitleImageFromDrawable(){

    // Create file object for saving the the bitmap
    File file = new File(MainActivity.mRootDirectoryForFrontViewImages + "front_view_image_day00001.PNG");
    FileOutputStream outStream = null;
    try {
        outStream = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    // Create a bitmap from the title image drawable
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.dory);

    // Create a mutable version of the bitmap to draw on it
    Bitmap mutableBitmap = bm.copy(Bitmap.Config.ARGB_8888, true);//<--true makes copy mutable

    // Create a new canvas and set it to the new muteable bitmap
    Canvas canvas = new Canvas(mutableBitmap);

    // Create paint object and set up settings for it
    Paint paint = new Paint();
    paint.setColor(Color.BLACK); // Text Color
    paint.setTextSize(40); // Text Size


    // Using the canvas draw on the muteable bitmap
     canvas.drawText("Test", 10, 10, paint);

    // Put the muteable file into the file
    mutableBitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);

    try {
        outStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        outStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }


}

谢谢您的帮助。

在这里,我放置的代码将对您大有帮助。请理解此代码并根据您的要求应用更改。您可以将文本放置在您触摸过的图像的准确位置

public void setCBitmap(Bitmap bitmap2, MotionEvent e, String name) {
    float eventX = e.getX();
    float eventY = e.getY();
    float[] eventXY = new float[] { eventX, eventY };

    Matrix invertMatrix = new Matrix();
    ((ImageView) main_iMG).getImageMatrix().invert(invertMatrix);

    invertMatrix.mapPoints(eventXY);
    int x = Integer.valueOf((int) eventXY[0]);
    int y = Integer.valueOf((int) eventXY[1]);
    Canvas canvas = null;
    canvas = new Canvas(bitmap2);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.WHITE);
    paint.setTextSize(45);


    Paint mpaint= new Paint();
    mpaint.setColor(Color.parseColor("#f9bf3a"));
    mpaint.setStyle(Style.FILL);
    float w = paint.measureText(name)/2;
    float textSize = paint.getTextSize();
    Paint paint1= new Paint(Paint.ANTI_ALIAS_FLAG);



    Paint p= new Paint();
    p.setAntiAlias(true);
    p.setColor(Color.WHITE);
    p.setStyle(Paint.Style.FILL);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(new RectF( x-13, y -(textSize+3), x + paint.measureText(name)+13,y+23), 7, 7, p);


    canvas.drawRoundRect(new RectF( x-10, y -(textSize), x + paint.measureText(name)+10,y+20),  7,7,mpaint);
    canvas.drawText(name, x, y, paint);

    main_iMG.setImageBitmap(bitmap2)}

注意:1)使用
setTextSize()
方法,而不是
setStrokeWidth()
。2) 12的大小(恰好是默认的文本大小)非常小;你可能只是没有看到文本。3) 调用
drawBitmap()
只是将位图绘制到自身上,毫无意义。4) 传递到
drawText()
中的坐标用于文本基线的左侧点,而不是其顶部。好的,谢谢,我去掉了笔划宽度并将文本设置为40,删除了画布drawBitmap并删除了setXfermode,但遗憾的是仍然是一样的。用这些修改编辑了我的问题。好吧,根据图像的大小和你的观看方式,40仍然可以相对较小。如果您没有更改
drawText()
方法中的坐标,则大多数文本都超出了左上角图像的顶部。很好,您是对的。谢谢我踢的是大小和位置,现在我看到了。我不知道问题出在哪里