Android - 任何用于向位图添加转贴标签的库

Android - Any library for adding repost label to bitmap

我一直在尝试制作一个照片分享应用程序,能够将您的图像和名称添加到图像中。折腾了Canvas一整天,也没能得到好的结果。我能够画出名称和位图,但它们看起来不太好。

这就是为什么我在这里询问是否有任何库或代码片段可以帮助我制作类似于 [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);

}

Here is the result

更新: @pskink 给了我 this code 效果很好

如果你想自定义它,那么使用 Drawable 而不是纯白色矩形(就像在你的原始代码中一样),结果可能是这样的:

代码:

// 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;
}