how to change canvas with Drawable? - android

I need to convert the rotated canvas image to drawable.
Conversely, there are many ways to convert from drawable to canvas, but I could not find what I wanted.
Is it not common to convert from Canvas to Drawable?
Should I use another method?
Please let me know, Thank you.

View v = new View(this);
v.setDrawingCacheEnabled(true);
v.draw(canvas);
Bitmap b = v.getDrawingCache();
Drawable drawable = new BitmapDrawable(getResources(), b);

You can create a bitmap from your canvas as described in this asnwer, hen you can generate your BitmapDrawable this way new BitmapDrawable(getResources(), yourBitmap)

Related

How do i convert to Color to Bitmap?

I have a color in form of integer, and i want that color to be in a Bitmap form.
Is there any way to do so?
I tried
Drawable d = new ColorDrawable(Color.parseColor("#ffffff"));
Bitmap b = ((BitmapDrawable)d).getbitmap();
But the above code give error Cannot cast ColorDrawable to BitmapDrawable
Is there any other way?
Actual code is
Palette.generateAsync(BitmapFactory.decodeFile(songArt),
new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(final Palette palette) {
if (Build.VERSION.SDK_INT >= 16) {
Drawable colorDrawable = new ColorDrawable(palette.getDarkVibrantColor(
getResources().getColor(R.color.noti_background)));
notificationCompat.bigContentView.setImageViewResource(R.id.noti_color_bg,
((BitmapDrawable) colorDrawable).getBitmap());
notificationManager.notify(NOTIFICATION_ID, notificationCompat);
}
}
}
);
Yes there is.
You can just do it like this:
Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawColor(colorInt)
Inside drawColor() you can also set the color by using the methods of the Color class like Color.argb(...) or Color.rgb(...)
That way you will have a bitmap with dimensions width/height and filled with the specified color.
Further explanation: You create a Bitmap object with your specified dimensions. Then you create a Canvas object and attach the Bitmap object to it. This way everything that is drawn using the Canvas object methods gets drawn on the Bitmap object.
In the end you have a Bitmap object with your drawings in it.

How I use the image of a screenshot?

I want to use the code of this link: How to take a screenshot and share it programmatically to take screenshots of my application. But I want the picture which is produced to use it to change my background, like this Layout.setBackgroundResource(resid).
How I can do this?
Where I will find the image's path to use it to change Background?
First you need the view you want a screen shot of:
View anyView = findViewById(R.id.anyView);
Then use this method to take a screenshot:
public Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
like this:
Bitmap screenshot = screenShot(anyView);
Bitmaps can't be set as a background image so lets convert it to a drawable:
Drawable drawableScreenshot = new BitmapDrawable(getResources(), screenshot);
Now set it to be the View background:
anyView.setBackgroundDrawable(drawableScreenshot);

How to get the whole bitmap attached to an ImageView?

I tried to get Bitmap attached to an ImageView, using ImageView.getDrawingCache(); But I found that the returned Bitmap was not the same as I'd like to get from the ImageView. It was always smaller than the real image.
I had known that, the method getDrawingCache() should not have the view if it is bigger than the screen as the visible portion of the view is only drawn and the cache holds only what is drawn.
Could I get the whole bitmap attached to a ImageView?
If you just want the Bitmap from a ImageView the following code may work for you:-
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
I think that's what you wanted.
If your drawble is not always an instanceof BitmapDrawable
Note: ImageView should be set before you do this.
Bitmap bitmap;
if (mImageView.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
Drawable d = mImageView.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
Your bitmap is stored in bitmap.
Voila!
Easiest way is to set tag in ImageView.
imageView.setImageBitmap(bitmap);
imageView.setTag(bitmap);
To get Tag from it
imageView.getTag();

Drawing a bitmap in a canvas after calling setBitmap doesn't work

I'm drawing a bitmap in a canvas and I want to have the result in a new bitmap, but I still have a black screen as result.
This is my code, part of the onDraw(Canvas canvas) method:
if (bitmapTemplate == null) {
canvasBis = new Canvas();
bitmapTemplate = Bitmap.createBitmap(canvas.getWidth()+30,canvas.getHeight(),Bitmap.Config.ARGB_8888);
drawZones(canvasBis,bitmapTemplate);
}
bitmapRes = Bitmap.createBitmap(canvas.getWidth()+30,canvas.getHeight(),Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmapRes);
canvas.drawBitmap(bitmapTemplate, matrix, null);
My goal is to have a new bitmap (bitmapRes) by applying a matrix on an existing bitmap (bitmapTemplate). With this code I always have a black screen, but when I remove the line canvas.setBitmap(bitmapRes), I have a result but not in a new bitmap. Any ideas please? Maybe transparency? Thanks in advance.
drawZones draws some zones in bitmapTemplate.
You should try using the Canvas(Bitmap) constructor instead of the empty constructor. This sets the bitmap to the canvas for you.

How to convert a Bitmap to Drawable in android?

How can I convert a Bitmap image to Drawable ?
Try this it converts a Bitmap type image to Drawable
Drawable d = new BitmapDrawable(getResources(), bitmap);
Sounds like you want to use BitmapDrawable
From the documentation:
A Drawable that wraps a bitmap and can
be tiled, stretched, or aligned. You
can create a BitmapDrawable from a
file path, an input stream, through
XML inflation, or from a Bitmap
object.
Having seen a large amount of issues with bitmaps incorrectly scaling when converted to a BitmapDrawable, the general way to convert should be:
Drawable d = new BitmapDrawable(getResources(), bitmap);
Without the Resources reference, the bitmap may not render properly, even when scaled correctly. There are numerous questions on here which would be solved simply by using this method rather than a straight call with only the bitmap argument.
Offical Bitmapdrawable documentation
This is sample on how to convert bitmap to drawable
Bitmap bitmap;
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
I used with context
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
1) bitmap to Drawable :
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
// mImageView.setDrawable(mDrawable);
2) drawable to Bitmap :
Bitmap mIcon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);
// mImageView.setImageBitmap(mIcon);
If you have a bitmap image and you want to use it in drawable, like
Bitmap contact_pic; //a picture to show in drawable
drawable = new BitmapDrawable(contact_pic);
Just do this:
private void setImg(ImageView mImageView, Bitmap bitmap) {
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
mImageView.setDrawable(mDrawable);
}
here's another one:
Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
For Kotlin users:
Kotlin has a function for converting Bitmap to BitmapDrawable:
Bitmap.toDrawable(resources)
covert bit map to drawable in sketchware app using code
android.graphics.drawable.BitmapDrawable d = new android.graphics.drawable.BitmapDrawable(getResources(), bitmap);

Categories

Resources