int x = 10;
int y = 10;
int r = 4;
Paint mPaint = new Paint();
mPaint.setColor(0xFF0000);
Canvas mCanvas = new Canvas();
mCanvas.drawCircle(x,y,r,mPaint);
Is there any way to convert mCanvas to a Drawable? My goal is to generate drawables with a certain shape and color.
Thanks
For simple shapes like your circle, I'd think a Shape Drawable would be easier. For more complicated things, just create a Bitmap for your Canvas to use, then create the Canvas and draw into it, then create a Drawable from your Bitmap. Something like:
int x = 10;
int y = 10;
int r = 4;
Paint mPaint = new Paint();
mPaint.setColor(0xFF0000);
Bitmap bitmap = Bitmap.createBitmap(/* read the docs*/);
Canvas mCanvas = new Canvas(bitmap);
mCanvas.drawCircle(x,y,r,mPaint);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
To be perhaps somewhat pedantic (and hopefully increase your understanding), a Canvas just hosts the "draw" calls and draws into a Bitmap that you specify. This means:
Your example code doesn't do much, since you didn't construct your Canvas with a Bitmap or call setBitmap() on it.
You're not converting your Canvas to a Drawable, you're constructing a Drawable from the Bitmap your Canvas calls draw into.
Taken from another post, here is the psuedo code to do this.
Image on canvas to JPEG file
ByteArrayOutputStream baos = new ByteArrayOutputStream()
Bitmap bitmap = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
// This converts the bitmap to a drawable
BitmapDrawable mDrawable = new BitmapDrawable(getResources(),bitmap);
Alternately, you could use getDrawingCache() as outlined in another answer of that thread.
Related
How can I add a caption (watermark) to photo (not bitmap) when capturing through camera on Android and save to file?
Yes, it is possible. U can create a function like this. Here we are drawing a watermark over your original bitmap.
public static Bitmap mark(Bitmap src, String watermark) {
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.setColor(Color.RED);
paint.setTextSize(18);
paint.setAntiAlias(true);
paint.setUnderlineText(true);
canvas.drawText(watermark, 20, 25, paint);
return result;
}
A simple solution would be to draw a watermark view over the surface view of the camera output and take the bitmap of the parent view. And finally convert and save it as JPG file
Bitmap bitmap = Bitmap.createBitmap(canvasLL.getawidth(), canvasLL.getHeight(), Bitmap.Config.RGB_5);
Canvas canvas = new Canvas(bitmap)
View.draw(canvas);
This how i had create a bitmap using the things draw on canvas. But i want to check that if something draw or not on the canvas. Any help will be really appreciated.
Bitmap.getGenerationId() can be used to check if a bitmap has been modified.
Bitmap bitmap = Bitmap.createBitmap(canvasLL.getWidth(), canvasLL.getHeight(), Bitmap.Config.RGB_565);
final int oldBitmapId = bitmap.getGenerationId();
Canvas canvas = new Canvas(bitmap);
View.draw(canvas);
final int newBitmapId = bitmap.getGenerationId();
if (oldBitmapId != newBitmapId) {
// a Bitmap has been changed
}
How to mask image such as we can scale/zoom/rotate the background image but not the mask?
I should be able to zoom the image in this mask but it is scaling whole image.
Suggest me a way to achieve this, I'm creating a photo collage app.
Blue color is background of Layout.
The white color is for mask
I'm able to achieve this type of layout with masking, but when I apply MultiTouchListener to scale and zoom, it will scale the whole image with mask and not the image inside it.
private void getMaskedBitmap() {
Bitmap bgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background_drawable);
ImageView bg = (ImageView) findViewById(R.id.bg);
bg.setImageBitmap(bgBitmap);
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap emptyBitmap = Bitmap.createBitmap(bgBitmap.getWidth(), bgBitmap.getHeight(), conf);
Canvas canvasBmp = new Canvas(bgBitmap);
ImageView mImageView = (ImageView) findViewById(R.id.troll_face);
Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.random_drawable);
Bitmap mask = BitmapFactory.decodeResource(getResources(), R.drawable.mask_drawable);
original = Bitmap.createScaledBitmap(original, mask.getWidth(), mask.getHeight(), true);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(original, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
mImageView.setImageBitmap(result);
//mImageView.setScaleType(ScaleType.FIT_XY);
mImageView.setBackgroundResource(R.drawable.background_drawable);
bg.setOnTouchListener(new MultiTouchListener());
mImageView.invalidate();
}
Exception on line
Canvas canvasBmp = new Canvas(bgBitmap);
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
I cannot provide you code but here is what you should do:
Get background image as bitmap and draw it on view's canvas.
Create an new (empty) bitmap of same size (use Bitmap.Config.ARGB_8888) and get its canvas, here you will draw mask.
Canvas canvasBmp = new Canvas(bmp);
Now draw bmp on view's canvas. So, that you can get mask effect on image.
Zoom (re-size etc whatever you want) background bitmap image.
Invalidate your view, and follow all steps again
I have to create new bitmap from an original image after user do somethings with it like zooming, rotating, dragging. And i have a frame border, and only part of original image lay on frame is picked. Issue is that remain region of frame is black, and i want it could be transparent or white. How to do that? Thanks in advance.
public Bitmap createBitmap(final Matrix pMatrix, final Bitmap pSourceBitmap){
Bitmap bmp = Bitmap.createBitmap(width, height, pSourceBitmap.getConfig());
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(pSourceBitmap, pMatrix, new Paint());
return bmp;
}
To make the remaining portion transparent
use the following code
public Bitmap createBitmap(final Matrix pMatrix, final Bitmap pSourceBitmap)
{
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp= Bitmap.createBitmap(width, height, conf);
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(pSourceBitmap, pMatrix, new Paint());
return bmp;
}
I'm trying to rotate an overlay drawable to represent an overlay item in Android.
I use this:
Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.map_pin);
Bitmap targetBitmap = Bitmap.createBitmap((bmpOriginal.getWidth()),
(bmpOriginal.getHeight()),
Bitmap.Config.ARGB_8888);
Matrix matrix = new Matrix();
matrix.setRotate((float) lock.getDirection(),(float) (bmpOriginal.getWidth()/2),
(float)(bmpOriginal.getHeight()/2));
Canvas tempCanvas = new Canvas(targetBitmap);
tempCanvas.drawBitmap(bmpOriginal, matrix, null);
Drawable d = new BitmapDrawable(getResources(),targetBitmap);
//overlayitem.setMarker(drawable);
mapItemizedOverlay =
new MyItemizedOverlay<MyItemizedOverlayItem>(d, mapView);
The problem is that the image quality worsens. Pixelization happens. Any solution?
This code worked perfectly with me
Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.map_pin);
Bitmap targetBitmap = Bitmap.createBitmap((bmpOriginal.getWidth()),
(bmpOriginal.getHeight()),
Bitmap.Config.ARGB_8888);
Paint p = new Paint();
p.setAntiAlias(true);
p.setDither(true);
p.setFilterBitmap(true);
Matrix matrix = new Matrix();
matrix.setRotate((float) lock.getDirection(),(float) (bmpOriginal.getWidth()/2),
(float)(bmpOriginal.getHeight()/2));
RectF rectF = new RectF(0, 0, bmpOriginal.getWidth(), bmpOriginal.getHeight());
matrix.mapRect(rectF);
targetBitmap = Bitmap.createBitmap((int)rectF.width(), (int)rectF.height(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(targetBitmap);
tempCanvas.drawBitmap(bmpOriginal, matrix, p);
You could try to apply a paint with antialiasing but if its about pixels within the bitmap the only chance is filtering and android has its limits. Depending on the bitmap you might never be able to get it really smooth, i did try a lot but never found anything that would give it a real nice finish
public static Bitmap createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter)
the filter boolean should be true, thats all that you can do for filtering afaik
void android.graphics.Canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
here you can apply a paint, you can experiment with settings on this paint, but anti-alias will work on the edges of the bitmap. Shader settings might work on the bitmap as well but there are no shaders that can fix the rotation inaccuracy
Add the following:
Paint p = new Paint();
p.setAntiAlias(true);
p.setDither(true);
p.setFilterBitmap(true);
change the following:
tempCanvas.drawBitmap(bmpOriginal, matrix, null);
to:
tempCanvas.drawBitmap(bmpOriginal, matrix, p);