BitmapDrawable performance - android

I would like to ask if how can I improve the performance of these codes.
Basically what it does is draw a BitmapDrawable and use it as a drawable for ImageView then place it on the TableRow of TableView.
private void drawTableData() {
TableLayout table = new TableLayout(this);
BitmapDrawable bm;
TableRow row = new TableRow(this);
String rowData = "A1;A2;A3;A4;A5;A6;A7;A8;A9;A10;A11;A12;";
String[] tmpRowData = rowData.split("\\;");
for (String str : tmpRowData) {
ImageView img = new ImageView(this);
bm = writeOnDrawable(R.drawable.seat_check_icon, str);
img.setImageDrawable(bm);
img.setLayoutParams(new TableRow.LayoutParams(20, 20));
row.addView(img);
}
table.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
public BitmapDrawable writeOnDrawable(int drawableId, String text) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setStyle(Style.FILL);
paint.setColor(txtColor);
paint.setTextSize((float) 14);
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
int height = bounds.bottom + bounds.height();
int width = bounds.left + bounds.width();
float canvasWidth = bm.getWidth();
float canvasHeight = bm.getHeight();
float startPositionX = (canvasWidth - width) / 2;
float startPositionY = (canvasHeight + height) / 2;
Canvas canvas = new Canvas(bm);
canvas.drawText(text, startPositionX, startPositionY, paint);
return new BitmapDrawable(this.getResources(), bm);
}
Any suggestions would be very much appreciated.
Thanks in advance.

Here are three (plus one) suggestions:
Create and initialize your Paint object only once, not every time you do the drawing.
Preload and keep the bitmap you draw upon, if it's always the same one.
You could directly create your custom Drawable class and in it's draw() method do your painting.
Radical (may not apply to your needs): In your layout, make one ImageView with the bitmap and put a TextView on top of it for the text.

Related

How to pass many textview on random position over image view?

now I am getting only one Text over the image I am using Canvas and Bitmap :
here is my code :
public Bitmap drawTextArrayToBitmap(Context mContext, int resourceId,String mText) {
try {
Resources resources = mContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
if(bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.rgb(110,110, 110));
paint.setTextSize((int) (12 * scale));
paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);
Rect bounds = new Rect();
paint.getTextBounds(mText, 0, mText.length(), bounds);
/* int x = (bitmap.getWidth() - bounds.width())/6;
int y = (bitmap.getHeight() + bounds.height())/5;*/
int x = 100, y = 100;
for(int i=0;i<20;i++)
{
canvas.drawText(mText, x * scale, y * scale, paint);
}
return bitmap;
} catch (Exception e) {
// TODO: handle exception
return null;
}
}
inside oncreate ;
ImageView mImageView = (ImageView)findViewById(R.id.imageView1);
// Bitmap bmp =drawTextToBitmap(this,R.drawable.man,"Hello Android");
Bitmap bmp =drawTextArrayToBitmap(this,R.drawable.man,"Smile");
mImageView.setImageBitmap(bmp);
In this above code, I want to add multiple texts on random position over Image and different style I have tried much time I could not get succeed yet. I have tried inside loop also.
Thank you in Advance I would appreciate your effort

Size of watermark differs with phones

I want to add watermark to my app. The screen is divided into 4 rectangle,size of each are ScreenWidth * 1/4ScreenHeight.And each rectangle contains two watermarks.The code is posted below:
final ViewGroup rootView = (ViewGroup) getActivity().findViewById(android.R.id.content);
ScreenInitControl screenInitControl = new ScreenInitControl(getActivity());
int screenWidth = screenInitControl.getScreenWidth();
int screenHeight = screenInitControl.getScreenHeight();
int bitmapWidth = screenWidth;
int bitmapHeight = screenHeight / 4;
Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
bitmap.setHasAlpha(true);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
// init paint
Paint paint = new Paint();
paint.setColor(Color.GRAY);
paint.setAlpha(100);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.LEFT);
paint.setTextSize(50);
// the water mark text
String watermarkText = Config.getIns().getName();
// draw the first watermark in a row
int x = 30;
int y = bitmapHeight / 2;
Path path = new Path();
path.moveTo(x, y);
path.lineTo(bitmapWidth / 2, 30);
canvas.drawTextOnPath(watermarkText, path, 0, 30, paint);
// draw the second watermark in a row
Path path1 = new Path();
x = bitmapWidth / 2 + 30;
y = bitmapHeight / 2;
path1.moveTo(x, y);
path1.lineTo(bitmapWidth, 30);
canvas.drawTextOnPath(watermarkText, path1, 0, 30, paint);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
// add the watermark onto background
final View watermarkView = new View(getActivity());
watermarkView.setLayoutParams(new LinearLayout.LayoutParams(screenWidth, screenHeight));
if (Build.VERSION.SDK_INT < 16) {
watermarkView.setBackgroundDrawable(bitmapDrawable);
} else {
watermarkView.setBackground(bitmapDrawable);
}
rootView.postDelayed(new Runnable() {
#Override
public void run() {
rootView.addView(watermarkView);
}
}, 50);
The code work fine in Samsung galaxy A5. But when I install the app on OPPO R11S, the wartermark comes to be 1/3 size of that in Samsung.Do I ignore something?
Finally,I find it that BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap) is deprecated. I use BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap) instead and the problem is solved.

Are there any libraries that support Android images to add watermarks?

I tried for a long time and couldn't get any better,On different mobile phones:
1. I would like to add pictures and words to the sourceBitmap.
2. want to be able to adjust bitmap and word positions.
Without using library simply we can watermark image using canvas and paint concept
Point point=new Point();
point.set(180, 1000);
Bitmap b=waterMark(BitmapFactory.decodeResource(getResources(), R.drawable.image),"your Text",point,Color.WHITE,90,30,true);
imageView.setImageBitmap(b);
the method code
public Bitmap waterMark(Bitmap src, String watermark, Point location, int color, int alpha, int size, boolean underline) {
//get source image width and height
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
//create canvas object
Canvas canvas = new Canvas(result);
//draw bitmap on canvas
canvas.drawBitmap(src, 0, 0, null);
//create paint object
Paint paint = new Paint();
//apply color
paint.setColor(color);
//set transparency
paint.setAlpha(alpha);
//set text size
paint.setTextSize(size);
paint.setAntiAlias(true);
//set should be underlined or not
paint.setUnderlineText(underline);
//draw text on given location
canvas.drawText(watermark, location.x, location.y, paint);
return result;
}
This method will automatically adjust the size of the watermark text according to the size of bitmap. Additionally it will also add the watermark diagonally if the image is portrait and horizontal if image is landscape
public static Bitmap waterMark(Bitmap src, String watermark, int color, int alpha, int size) {
//get source image width and height
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
//create canvas object
Canvas canvas = new Canvas(result);
//draw bitmap on canvas
canvas.drawBitmap(src, 0, 0, null);
//create paint object
Paint paint = new Paint();
//apply color
paint.setColor(color);
//set transparency
paint.setAlpha(alpha);
//set text size
paint.setTextSize(size);
paint.setAntiAlias(true);
Paint stkPaint = new Paint();
stkPaint.setStyle(Paint.Style.STROKE);
stkPaint.setStrokeWidth(1);
stkPaint.setTextSize(size);
stkPaint.setAlpha(alpha);
stkPaint.setColor(Color.BLACK);
stkPaint.setAntiAlias(true);
//set should be underlined or not
//draw text on given location
canvas.save();
float width = paint.measureText(watermark);
Logger.e("width of bitmap", "" + w);
Logger.e("width of text", "" + width);
if (width > w) {
int i = 2;
while (width > w) {
paint.setTextSize(size - i);
stkPaint.setTextSize(size - i);
width = paint.measureText(watermark);
Logger.e("width of text", "" + width);
i++;
}
} else {
int i = 2;
while (width < w) {
paint.setTextSize(size + i);
stkPaint.setTextSize(size + i);
width = paint.measureText(watermark);
Logger.e("width of text", "" + width);
i++;
}
}
if (w < h) {
canvas.rotate(-60, src.getWidth() / 2, src.getHeight() / 2);
}
float final_x = (src.getWidth() - width) / 2;
Point p = new Point();
p.set((int) final_x, src.getHeight() / 2);
canvas.drawText(watermark, p.x, p.y, paint);
canvas.drawText(watermark, p.x, p.y, stkPaint);
canvas.restore();
return result;
}

Exporting multiple bitmaps to one picture

I've got two ImageViews within a RelativeLayout. One ImageView is draggable and scalable upon the other ImageView. This all works fine except when I try to export those ImageViews to a bitmap and add it to a canvas.
I can't figure out how to calculate the scale and dragged position of view/bitmap B.
ImageView A functions as a background.
ImageView B is draggable and scalable on top of ImageView A.
Here is a code example
public static Bitmap MergeBitmaps(View v, View ViewA, Bitmap bitmapA, View viewB, Bitmap bitmapB) {
Bitmap b = Bitmap.createBitmap(
bitmapA.getWidth(), bitmapA.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
float scaleFactor = (float) ViewA.getHeight() / (float) bitmapA.getHeight();
float scaleFactorWidth = (float) ViewA.getWidth() / (float)bitmapA.getWidth();
int test2[] = new int[2];
viewB.getLocationOnScreen(test2);
//Rect rect1 = new Rect();
//selectedImg.getDrawingRect(rect1);
//Rect rect2 = new Rect();
//logo.getDrawingRect(rect2);
//int logoHeight = logo.getHeight();
//int logoWidth = logo.getWidth();
//int logoLeft = logo.getLeft();
//int selectedImgLeft = selectedImg.getLeft();
float scaleFactorMid = (scaleFactor + scaleFactorWidth)/2;
c.drawBitmap(bitmapA, 0, 0, null);
//c.drawBitmap(logoBitmap, test2[0]*scaleFactorWidth, test2[1]*scaleFactor, null);
bitmapB = getResizedBitmap(bitmapB, (int)(bitmapB.getHeight()*scaleFactorMid), (int)(bitmapB.getWidth()*scaleFactorMid));
c.drawBitmap(bitmapB, bitmapA.getWidth()/2, bitmapA.getHeight()/2, null);
//v.layout(0, 0, v.getWidth(), v.getHeight());
//selectedImg.draw(c);
return b;
}
I tried calculating the scale factor by dividing the view's width/height and the Bitmaps width/height.
I tried a lot more as you can see. Can anyone point me in the right direction?
For your reference below function are helpful,(In this i scaled bitmap statically, you can move the code).
public void mergeTwoBitmap() {
ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
ImageView iv2 = (ImageView) findViewById(R.id.imageView2);
Bitmap bitmap1 = ((BitmapDrawable) iv1.getDrawable()).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable) iv2.getDrawable()).getBitmap();
float img2X = iv2.getX(), img2Y = iv2.getY();
float img2Width = bitmap2.getWidth(), img2Height = bitmap2.getHeight(); // Original Width And Height
bitmap2 = Bitmap.createScaledBitmap(bitmap2, 300, 300, true);
float img2NewX, img2NewY;
img2NewX = img2X + (img2Width - bitmap2.getWidth()) * 0.5f; // here bitmap2.getWidth() after scale bitmap1
img2NewY = img2Y + (img2Height - bitmap2.getHeight()) * 0.5f; // here bitmap2.getHeight() after scale bitmap2
Bitmap b = mergeBitmap(iv1.getWidth(), iv1.getHeight(), bitmap1,
bitmap2, img2NewX, img2NewY);
}
private Bitmap mergeBitmap(float w, float h, Bitmap bmp1, Bitmap bmp2,
float x, float y) {
Bitmap cs = null;
int width = (int) w, height = (int) h;
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(
Bitmap.createScaledBitmap(bmp1, width, height, true), 0f, 0f,
null);
comboImage.drawBitmap(bmp2, x, y, null);
return cs;
}

how to draw text with background color using canvas

I have some code where I'm drawing my text on bitmap (canvas)
canvas.drawTextOnPath(Text, textPath[count], gipa, -10, text);
Please tell me, it's possible to draw this text in path(textPath) with background color?
it's full function for drawing only text
public void drawText(float x,float y ,String Text,Canvas canvas,Paint paint1 ,int count )
{
float xren =text.measureText(Text.trim());
canvas.drawTextOnPath(Text, textPath[count], gipa, -10, text);
}
Using this function I'm drawing text on my canvas. so how to modify this function for drawing this text with background?
Most likely two steps are needed here. you would draw a line along path first with color for background and then draw the text as indicated. Set the thickness of the line with a paint object. Also, changing the style of the paint can help with the effect. try FILL, STROKE or FILL_AND_STROKE for different effects.
mpaint.setStyle(Paint.Style.STROKE);
mpaint.setStrokeWidth(strokeWidth);
Added sample to draw a path(rectangle) with red color:
Paint mPaint = new Paint();
mPaint.setColor(Color.RED);
Path mPath = new Path();
RectF mRectF = new RectF(20, 20, 240, 240);
mPath.addRect(mRectF, Path.Direction.CCW);
mPaint.setStrokeWidth(20);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(mPath, mPaint);
Then draw text along same path (blue color):
mPaint.setColor(Color.BLUE);
mPaint.setStrokeWidth(0);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setTextSize(20);
canvas.drawTextOnPath("Draw the text, with origin at (x,y), using the specified paint, along the specified path.", mPath, 0, 5, mPaint);
If you want to make like this then implement below code snippet:
/**
* PUT THIS METHOD FOR IMPLEMENT WATER-MARK IN COMMON FILE
*/
public static Bitmap waterMark(Bitmap src, String watermark) {
//get source image width and height
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
Paint.FontMetrics fm = new Paint.FontMetrics();
paint.setColor(Color.WHITE);
paint.getFontMetrics(fm);
int margin = 5;
canvas.drawRect(50 - margin, 50 + fm.top - margin,
50 + paint.measureText(watermark) + margin, 50 + fm.bottom
+ margin, paint);
paint.setColor(Color.RED);
canvas.drawText(watermark, 50, 50, paint);
return result;
}
// To Get Bitmap from URI:
private Bitmap getBitmapFromUri(String photoPath) {
Bitmap image = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
return bitmap;
}
// Save Image :
private String SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/shareImage");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 20, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return file.getAbsolutePath();
}
// Call like this :
Bitmap bitmap = getBitmapFromUri(attachment.get(i).getPath()); // Enter here your Image path
Bitmap bitmapp = waterMark(bitmap, "ENTER YOUR TEXT FOR WATERMARK LABEL");
String path = SaveImage(bitmapp);
Uri uri = Uri.fromFile(new File(path));
Here at last from uri you can get a new implemented watermark image.
Hope this helps you.
I believe this solution is better and more flexible than drawPath.
Use this to calculate the size of the text background:
private #NonNull Rect getTextBackgroundSize(float x, float y, #NonNull String text, #NonNull TextPaint paint) {
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
float halfTextLength = paint.measureText(text) / 2 + 5;
return new Rect((int) (x - halfTextLength), (int) (y + fontMetrics.top), (int) (x + halfTextLength), (int) (y + fontMetrics.bottom));
}
Then draw the background as a Rect:
Rect background = getTextBackgroundSize(x, y, text, textPaint);
canvas.drawRect(background, bkgPaint);
canvas.drawText(text, x, t, textPaint);
this method i created , it will give you better idea how easily you can do this
public static Drawable getTextToDrawable(final String sText, final float textSize, final int textColor, final int bgColor, final int imageSize) {
Shape shape = new Shape() {
#Override
public void draw(Canvas canvas, Paint paint) {
paint.setTextSize(spToPixel(textSize));
int ivImageSize = SUtils.dpToPx(imageSize);
paint.setTextAlign(Paint.Align.LEFT);
float baseline = -paint.ascent(); // ascent() is negative
int width = (int) (paint.measureText(sText)); // round
int height = (int) (baseline + paint.descent());
Bitmap image = Bitmap.createBitmap(ivImageSize, (int) (ivImageSize), Bitmap.Config.ARGB_8888);
canvas.drawBitmap(image, ivImageSize, ivImageSize, paint);
paint.setColor(bgColor);
if (sText != null) {
if (sText.length() < 3) {
canvas.drawCircle(ivImageSize / 2, ivImageSize / 2, ivImageSize / 2, paint);
paint.setColor(textColor);
canvas.drawText(sText, (ivImageSize - width) / 2, (height+baseline)/2, paint);
} else {
canvas.drawRect(0, 0, ivImageSize, height, paint);
paint.setColor(textColor);
canvas.drawText(sText, (ivImageSize - width) / 2, baseline, paint);
}
}
}
};
return new ShapeDrawable(shape);
}

Categories

Resources