How to Add background image in canvas for android game application - android

to learn the functionality of missile android game app, i thought of doing some changes to it, so started up the editing of background change, i had googled and placed the code in canvas,
Bitmap background = Bitmap.createBitmap( 0, 0, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(background);
c.drawBitmap(background, 10, 010, null);
i had tried many things but nothing worked out, and also here in the above code not given the path for drawable, where should i give the path for image?
any helps plz..

Hope this code will give you some clue about this...
//Here you can decode it from any source like SD card as i shown,Resources or from assets
Bitmap mBitmapFromSdcard = BitmapFactory.decodeFile("/mnt/sdcard/download/tux.png");
Canvas mCanvas = canvas;
if (mBitmapFromSdcard != null) {
mCanvas.drawBitmap(mBitmapFromSdcard, 0, 0, null);
}

Related

Converting Excel or csv File to bitmap

In my application I am creating Excel file and saving it to memory card so can I convert this excel sheet or csv file into bitmap If yes than how ? Please help me.
I Dont know there is any method to convert excel sheet into bitmap but You can create bitmap same as you creating Excel sheat.
for that you can use Canvas and drawText().
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmp);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
c.drawText("Hello world!", xPos, yPos, paint);
bmp.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(destFilePath);
You can also set the font and text-size via the Paint-object. See this link How to set font and
Paint.setTextSize().
Easiest way is to take a screenshot while the excel file is displayed. The usual Android way- hold down power button and home button simultaneously. The pic will get stored in a gallery.

Draw bitmap on canvas

I tried searching for a relevant example but found none, so I'll try to be as precise as I can.
Back in the gingerbread era I made a draw bitmap to canvas code that worked flawlessly and here it is.
//this one is drawing the background for my picture
mBackground = BitmapFactory.decodeResource(getResources(), R.drawable.base);
//setting a new canvas
mComboImage = new Canvas(mBackground);
//adding another bitmap to the canvas, don't worry about the size or the i variables
mBackImage = BitmapFactory.decodeResource(getResources(), R.drawable.base);
mBackImage=Bitmap.createScaledBitmap(mBackImage, size, 105, false);
mComboImage.drawBitmap(mBackImage, 100f+1f*size*i, 170f, null);
mBackImage.recycle();
//this is setting my background for an icon to the completed image
Drawable d =new BitmapDrawable(getResources(),mBackground);
aaa.setBackground(d);
anyways the code doesn't seem to fit now. One problem I have faced is converting the bitmap into mutable which you can check out here if you are stuck on it as I was for a while.
My problem is that the background is perfectly drawn but the mBackImage doesn't show up at all.
What worries me more is that this used to work perfectly before.
I really have tried searching for a newer explanation but haven't really found any on stackoverflow so
I drew bitmap as background image for my app as well. I used: setBackgroundDrawable() instead of setBackground() in your last statement.
Also, when preparing the bitmap, I set two options:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable=true;
bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.image1), options);
You can give it a try.

Converting Bitmap in memory to Bitmap with Bitmap.Config.RGB_565

I have a loaded Bitmap which I would like to convert to set the config to Bitmap.Config.RGB_565. Is there a simple way of converting a Bitmap to this configuration after the Bitmap is already loaded into memory? For example, below I have a bitmap being decoded from the application resources, however, how would I convert an already loaded Bitmap to RGB_565? I'm sure it's something simple, however, I'm fairly new to working with Bitmaps and after a few hours of looking online, unfortunately I couldn't find what I needed specifically.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig=Bitmap.Config.RGB_565
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.myphoto ,options);
You can also try this:
Bitmap converted = original.copy(Config.RGB_565, false);
From the documentation of Bitmap.copy():
Tries to make a new bitmap based on the dimensions of this bitmap, setting the new bitmap's config to the one specified, and then copying this bitmap's pixels into the new bitmap. If the conversion is not supported, or the allocator fails, then this returns NULL.
Looking through the native source code, you should be fine converting between any values of Bitmap.Config.
I haven't tested this but it should work:
private Bitmap convert(Bitmap bitmap, Bitmap.Config config) {
Bitmap convertedBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), config);
Canvas canvas = new Canvas(convertedBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawBitmap(bitmap, 0, 0, paint);
return convertedBitmap;
}
call the methods like this:
Bitmap convertedBitmap = convert(bitmap, Bitmap.Config.RGB_565);
You can do all kinds of additional transformations like rotating, stretching etc. if you use the drawBitmap with a Matrix.
Found the answer here https://stackoverflow.com/a/12148450/1364673, thanks to
siliconeagle.
The solution is to create a new bitmap with the required encoding as per link above example.

Merging image from camera with image from drawables

I´m trying to merge 2 images, one is bitmap from camera, second one is .png file stored in drawables. What I did was that I used both images as bitmaps and I tried to merge them by using canvas, something like this:
Bitmap topImage = BitmapFactory.decodeFile("gui.png");
Bitmap bottomImage = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
Canvas canvas = new Canvas(bottomImage);
canvas.drawBitmap(topImage, 0, 0, null);
But I keep getting "Bitmap size exceeds VM budget" error all the time. I tried nearly everything, but still, it keeps throwing this error. Is there another way of merging 2 images? What i need to do is simple - I need to take photo and save it merged with that .PNG image stored in drawables. For example this app is very close to what i need - https://play.google.com/store/apps/details?id=com.hl2.hud&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5obDIuaHVkIl0.
Thanks :)
See the below code for combining two images.
This method returns combined bitmap
public Bitmap combineImages(Bitmap frame, Bitmap image) {
Bitmap cs = null;
Bitmap rs = null;
rs = Bitmap.createScaledBitmap(frame, image.getWidth() + 50,
image.getHeight() + 50, true);
cs = Bitmap.createBitmap(rs.getWidth(), rs.getHeight(),
Bitmap.Config.RGB_565);
Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(image, 25, 25, null);
comboImage.drawBitmap(rs, 0, 0, null);
if (rs != null) {
rs.recycle();
rs = null;
}
Runtime.getRuntime().gc();
return cs;
}
You can change height and width as per your requirements
Hope this will help...
How large are the images? I've only encountered this problem when trying to load large images into memory.
Is the byte array your decoding actually an image?
From a quick look at the android docs you can capture an image using the default camera app which may work in this situation.
http://developer.android.com/training/camera/photobasics.html
Also see this question: Capture Image from Camera and Display in Activity
Edit: You may also need to scale the image from the camera down if it is very large. See the end of the android page I linked to for details on that.

How do I made a deep copy a BitmapDrawable?

I'm having trouble cloning a BitmapDrawable. I tried the answer in this post but it creates a "shallow" copy, and I need a "deep" copy so I can alter the pixels in the clone without affecting the original.
I also tried this:
Bitmap bitmap = bdOriginal.getBitmap();
BitmapDrawable bdClone = new BitmapDrawable(getResources(), bitmap.copy(bitmap.getConfig(), true));
But it seems to create an immutable clone even though I set the mutable parameter in Bitmap.copy() to "true". That is, color filters don't appear to change the clone. Am I doing it wrong? (EDIT: I used the debugger to confirm bitmap.mIsMutable = true)
To summarize, I need a clone of a BitmapDrawable that can be altered with color filters without affecting the original. Any suggestions?
Thanks in advance...
Create new Bitmap of the same size.
Create canvas for this new Bitmap
Draw your first Bitmap into this canvas.
Example:
Bitmap copy = Bitmap.createBitmap(original.getWidth(), original.getHeight(), original.getConfig());
Canvas copiedCanvas = new Canvas(copy);
copiedCanvas.drawBitmap(original, 0f, 0f, null);

Categories

Resources