Android - Any library for adding repost label to bitmap

萝らか妹 提交于 2019-12-04 07:12:04

问题


I have been trying to make a photo sharing app, with the ability to add your image and name to the image. I have been messing with Canvas for the whole day, but couldn't get good results. I was able to draw the name and bitmap, but they didn't look so good.

That's why I am here asking about is there any library or piece of code that could help me in making something similar to [this][1]. I wasn't able to find any thing for it.

EDIT: Sorry for not adding my own code

Here is my code from my latest try

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

UPDATE: @pskink gave me this code which works nicely


回答1:


if you want to customize it, then instead of solid white rectangle (like in your original code) use a Drawable and the result could be something like this:

the code:

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


来源:https://stackoverflow.com/questions/42457786/android-any-library-for-adding-repost-label-to-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!