Reputation: 4528
How to draw my drawable Image on the canvas? I am getting my image from the Resource and draw that image on the canvas. Is that Possible we can declare multiple canvas and set all in to one ? I have already image on the canvas and want to draw a drawable image on that?
private Bitmap drawtextonimage(Bitmap bitmap, String text, String caption) {
caption = caption.replace("\n", " ");
Declaring Canvas and Paint ?
Canvas cs = new Canvas(bitmap);
Paint tPaint = new Paint();
Paint captionPaint = new Paint();
if (text.equals("Good")) {
tPaint.setColor(Color.GREEN);
} else {
tPaint.setColor(Color.RED);
}
tPaint.setStyle(Style.FILL);
tPaint.setTextSize(30);
//
captionPaint.setColor(Color.CYAN);
captionPaint.setAlpha(100);
captionPaint.setTextSize(25);
captionPaint.setTextScaleX((float) 0.7);
captionPaint.setTextAlign(Align.CENTER);
captionPaint.setTypeface(Typeface.SERIF);
captionPaint.setShadowLayer(1f, 0, 1f, Color.WHITE);
/*Canvas cs1 = new Canvas(bitmap);
cs1.drawRect(0, bitmap.getHeight(), bitmap.getWidth(), 500, tPaint);
// canvas.drawRect(33, 33, 77, 60, paint );
Paint zPaint = new Paint();
cs1.drawARGB((int) 0.5,54,54,200);*/
// cs.drawText(text, 60, 60, tPaint);
This Method get my drawable image and draw on canvas but it is not working?
Resources res = getResources();
Bitmap bitmapx = BitmapFactory.decodeResource(res, R.drawable.overlay_good_full);
Bitmap bitmapxx = BitmapFactory.decodeResource(res, R.drawable.overlay_bad_full);
if(text.equals("Good"))
{
cs.drawBitmap(bitmapx, 0, 0, new Paint());
//cs.drawBitmap(bitmapx, 0, 0, tPaint);
}
else
{
// cs.drawBitmap(bitmapxx, 0, 0, tPaint);
}
cs.drawText(caption, (bitmap.getWidth() / 2), bitmap.getHeight()
, captionPaint);
// canvas.drawBitmap(image, 0, 0, null);
// Log.i("Caption", caption);
return bitmap;
}
Upvotes: 0
Views: 12564
Reputation: 25471
The following code will draw a Drawable from a resource onto a canvas created from a Bitmap (in this case a bitmap from a camera preview). This is tested and works with API 22+ (have not tested it with earlier versions):
Bitmap cameraBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length, opt);
Canvas camImgCanvas = new Canvas(cameraBitmap);
Drawable d = ContextCompat.getDrawable(getActivity(), R.drawable.myDrawable);
//Centre the drawing
int bitMapWidthCenter = cameraBitmap.getWidth()/2;
int bitMapheightCenter = cameraBitmap.getHeight()/2;
d.setBounds(bitMapWidthCenter, bitMapheightCenter, bitMapWidthCenter+d.getIntrinsicWidth(),
bitMapheightCenter+d.getIntrinsicHeight());
//And draw it...
d.draw(camImgCanvas);
Upvotes: 1