Android-任何用于向位图添加重新发布标签的库

Android-任何用于向位图添加重新发布标签的库,android,bitmap,android-canvas,Android,Bitmap,Android Canvas,我一直在尝试制作一个照片共享应用程序,能够将你的图片和名字添加到图片中。我一整天都在摆弄画布,但没能得到好结果。我能够画出名称和位图,但它们看起来不太好 这就是为什么我在这里询问是否有任何库或代码可以帮助我制作类似于[this][1]的东西。我找不到合适的东西 编辑:很抱歉没有添加我自己的代码 这是我最近一次尝试的代码 public void AddText(Position2D pos){ //Position2D is an enum having the 4 corners of the

我一直在尝试制作一个照片共享应用程序,能够将你的图片和名字添加到图片中。我一整天都在摆弄画布,但没能得到好结果。我能够画出名称和位图,但它们看起来不太好

这就是为什么我在这里询问是否有任何库或代码可以帮助我制作类似于[this][1]的东西。我找不到合适的东西

编辑:很抱歉没有添加我自己的代码

这是我最近一次尝试的代码

public void AddText(Position2D pos){
//Position2D is an enum having the 4 corners of the image
    bmWorking= bmOriginal.copy(Bitmap.Config.ARGB_8888,true);
    Canvas canvas = new Canvas(bmWorking);


    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setStyle(Paint.Style.FILL);
    Paint textPaint = new Paint();
    textPaint.setColor(Color.BLACK);
    float width = (35f/100f) * bmWorking.getWidth();
    float height = (width/16f) * 3;
    textPaint.setTextSize(height - 4);  //I wanted to have some space (margin) above and below the text
    textPaint.setTextAlign(Paint.Align.LEFT);
    float [] coords = getPositionCoords(pos, width, height);  //getPositionCoords returns a float array with the Left,Top,Right,Bottom position calculated based on the width and height
    canvas.drawRect(coords[0],coords[1], coords[2], coords[3],paint);
    username = "Haider Ali Punjabi";
    canvas.drawText(username, coords[0] ,coords[3], textPaint);
    bitmapView.setImageBitmap(bmWorking);

}
更新: @普金斯给了我
如果您想自定义它,那么使用可绘制的矩形代替原始代码中的纯白矩形,结果可能如下所示:

守则:

// for int gravity: see android.view.Gravity, like Gravity.LEFT, Gravity.BOTTOM, etc
// for example:
// Bitmap out = addText(this, in, "Haider Ali Punjabi", android.R.drawable.alert_light_frame, Gravity.BOTTOM, new Point(10, 10));
public Bitmap addText(Context ctx, Bitmap in, String text, int resId, int gravity, Point pad) {
    if (pad == null) pad = new Point();

    Bitmap out = in.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(out);

    Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setColor(Color.BLACK);
    textPaint.setTextAlign(Paint.Align.LEFT);
//    textPaint.setTextSize(128);

    Rect inBounds = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), inBounds);
    float scale = out.getWidth() * 0.35f / inBounds.width();

    Rect container = new Rect(0, 0, out.getWidth(), out.getHeight());
    Rect outBounds = new Rect();
    int w = (int) (inBounds.width() * scale);
    int h = (int) (inBounds.height() * scale);
    Gravity.apply(gravity, 2 * pad.x + w, 2 * pad.y + h, container, outBounds);

    Drawable dr = ctx.getResources().getDrawable(resId);
    Rect padding = new Rect();
    dr.getPadding(padding);
    dr.setBounds(outBounds.left - padding.left, outBounds.top - padding.top, outBounds.right + padding.right, outBounds.bottom + padding.bottom);
    dr.draw(canvas);
    Matrix matrix = new Matrix();
    RectF src = new RectF(inBounds);
    RectF dst = new RectF(outBounds);
    dst.inset(pad.x, pad.y);
    matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);
    canvas.concat(matrix);
    canvas.drawText(text, 0, 0, textPaint);
    return out;
}

我去问问有争议的人你试过什么?问题在这里。@Shark我还没有试过任何图书馆。但是我试过用画布在照片上画位图。我通过在相对布局上使用.getDrawingCache获得位图。它工作正常,但过去在低分辨率照片中占用更多空间。我给它添加了缩放,它过去占用相同的空间,但缩放导致它的质量下降。我还尝试绘制文本、背景矩形和位图,但无法将位图和文本放在矩形内。但无法将位图和文本放在矩形内,你指的是什么矩形?@pskink一个由drawRect创建的矩形,我知道我可以通过测量文本所占的空间来设置宽度,但我希望矩形占照片宽度的35%,因此我只能减小文本的大小,这看起来不太好,所以较长的文本需要分成几行?