Skip to content Skip to sidebar Skip to footer

Android - Any Library For Adding Repost Label To Bitmap

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 r

Solution 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:

enter image description here

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 = newPoint();

    Bitmapout= in.copy(Bitmap.Config.ARGB_8888, true);
    Canvascanvas=newCanvas(out);

    PainttextPaint=newPaint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setColor(Color.BLACK);
    textPaint.setTextAlign(Paint.Align.LEFT);
//    textPaint.setTextSize(128);RectinBounds=newRect();
    textPaint.getTextBounds(text, 0, text.length(), inBounds);
    floatscale= out.getWidth() * 0.35f / inBounds.width();

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

    Drawabledr= ctx.getResources().getDrawable(resId);
    Rectpadding=newRect();
    dr.getPadding(padding);
    dr.setBounds(outBounds.left - padding.left, outBounds.top - padding.top, outBounds.right + padding.right, outBounds.bottom + padding.bottom);
    dr.draw(canvas);
    Matrixmatrix=newMatrix();
    RectFsrc=newRectF(inBounds);
    RectFdst=newRectF(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;
}

Post a Comment for "Android - Any Library For Adding Repost Label To Bitmap"