Drawable to Bitmap, Drawable unknown size / dimension - java

I have a drawable object that is a vector. Since you can't crop (or clip because of ICS forced hardware acceleration) a drawable it make's sprite animation useless.
I'm trying to convert the drawable to a bitmap with a specific size (percentage of the lowest screen dimension for best results).
The version needs to be super memory efficient of course.
What I need help with is creating the bitmap with the specific size, here's what I got so far:
public Bitmap svgTObitmap(String name, int percentage, int screen_width, int screen_height)
{
_resID = _context.getResources().getIdentifier( name , "raw" , _context.getPackageName());
SVG svg = SVGParser.getSVGFromResource(_context.getResources(), _resID);
Drawable drawable = svg.createPictureDrawable();
Bitmap bmp = ((BitmapDrawable)drawable).getBitmap();
return bmp;
}

Being from a vector, clearly your Drawable is not in fact a BitmapDrawable. You need to instead draw the Drawable on a Bitmap. See this answer I gave to another question:
Bitmap bmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
Obviously in your case you would specify the size you want explicitly instead of trying to get the intrinsic height/width of the drawable.

Related

How to get single color image from Bitmap?

I upload a png-image with an alpha channel to the drawable folder. From this picture I get a Bitmap, which has transparency in certain places.
int resID = context.getResources().getIdentifier(bitmapName, "drawable", context.getPackageName());
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID);
I need to get a new Bitmap in which all colors are replaced by one color (for example, red), and the alpha channel should remain unchanged. There was a colored Bitmap with transparent areas, but I need a Bitmap with the same transparent areas, but the same color. Tell me how to achieve this?

How to Draw a Pattern on Bitmap using canvas

I just want to know is there anyway to draw a pattern over a bitmap , something like this. For example, I want to draw a pattern of this over my bitmap.
You can definitely set a bitmap.And creating a canvas over it.Once canvas gets created over the bitmap, you can definitely draw custom patterns over it.
You can see the complete guide how to draw text here:
https://www.skoumal.net/en/android-how-draw-text-bitmap/
Long story short:
Copy your bitmap to make it mutable and create Canvas based on it.
Option 1: Transparent Foreground
If you follow the process described here to merge the two images, you can draw one image onto another. Simply change the opacity of the top-most image as described here. Consider the following example:
Bitmap bitmap = Bitmap.createBitmap(/* width */, /* height */, Config.ARGB_8888);
Paint alpha = new Paint();
alpha.setAlpha(/* alpha */);
Canvas canvas = new Canvas(bitmap);
Bitmap background = BitmapFactory.decodeResource(
getResources(), R.drawable.background);
Bitmap foreground = BitmapFactory.decodeResource(
getResources(), R.drawable.pattern);
canvas.drawBitmap(background, /* xPos */, /* yPos */, null);
canvas.drawBitmap(foreground, /* xPos */, /* yPos */, alpha);
Option 2: Background Masked with Foreground
Alternatively, if you are trying to mask the image, you can consider implementing the solution found here.

Drawing drawable in canvas doesn't work

I want to draw a drawable on canvas with this code but it doesn't work and i don't know why
getResources().getDrawable(R.drawable.allergist).draw(canvas);
I set my custom views heigh and width to match parent but whole screen is white and there is no drawable on screen
You need to load your image as bitmap:
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.allergist);
Then make the bitmap mutable and create a canvas over it:
Canvas canvas = new Canvas(bitmap.copy(Bitmap.Config.ARGB_8888, true));
After that you can draw it on canvas.
EDIT 1
Set bounds To Drawable.
Drawable d = getResources().getDrawable(R.drawable.allergist);
d.setBounds(left, top, right, bottom);
d.draw(canvas);
We had a similar problem, and we solved it by calling drawable.invalidateSelf(). After that, it rendered successfully onto our Canvas.

How do i display a custom background image from my drawables instead of drawing color

I have the following code which I got from a youtube tutorial for drawing canvas:
(https://www.youtube.com/watch?v=6NBt36fO0iw):
#Override
protected void onDraw(Canvas canvasporch) {
canvasporch.drawColor(Color.BLACK);
for (int i = temps.size() - 1; i >= 0; i--){
temps.get(i).onDraw(canvasporch);
for (Sprite sprite : sprites){
sprite.onDraw(canvasporch);
This works just fine. The only problem is instead of having the color Black as background, I want to display an image from my drawable folder as the background for my sprites. I've tried using drawBitmap() but I cannot seem to make it work. Thanks.
1) Create a member variable to store the Bitmap:
Bitmap spriteBackground;
2) In onCreate() (or some other initialization function) load the bitmap from the drawable:
spriteBackground = BitmapFactory.decodeResource(getResources(), R.drawable.sprite_background);
3) Draw the bitmap using drawBitmap() inside onDraw():
canvasporch.drawBitmap(spriteBackground, 0, 0, null);
If you want to scale the bitmap to cover the canvas then either scale it at load time using createScaledBitmap (uses more memory) or apply a matrix transform to the canvas (possibly slower).
Another way is to pass the canvas bounds to drawBitmap(), like this:
canvasporch.drawBitmap(spriteBackground, null, canvasporch.getClipBounds(), null);

Android, Java: Resized Bitmap ignores bitmap options

I am creating this Android game with Java. However, I load the bitmaps and then resize them to fit screens and such (dpi isn't really exact). BUT my thought is also to load the bitmaps in 16b (mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_4444) for devices with a small amount of ram. But when I resize the bitmaps they seem to go back to 32b (Bitmap.Config.ARGB_8888).
This is how I declare the options:
mBitmapOptions = new BitmapFactory.Options();
mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_4444;
This is how I load the bitmaps:
mBitmaps.add(getResizedBitmap(BitmapFactory.decodeResource(mResources, imagePath, mBitmapOptions)));
And this is the getResizeBitmap method:
public Bitmap getResizedBitmap(Bitmap bm)
{
//Original size
int width = bm.getWidth();
int height = bm.getHeight();
//New size (percent)
float newWidth = 1 * mScaleWidth;
float newHeight = 1 * mScaleHeight;
//Create the matrix
Matrix matrix = new Matrix();
matrix.postScale(newWidth, newHeight);
//Recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
//Recycle the old Bitmap
bm.recycle();
return resizedBitmap;
}
Any ideas why the new Bitmap ignores the options?
Why don't you use the createScaledBitmap method? It should preserve the options. In your case, you are creating a completely new Bitmap and it probably applies a default config.
EDIT: Another option would be to use your code and add a call to the copy method like this:
Bitmap smallerBitmap = resizedBitmap.copy (Bitmap.Config.ARGB_4444, false);
resizedBitmap.recycle ();
However, I don't think this will have a nice performance...

Categories