create a bitmap with a bitmap inside - android

I know - the title may sounds strange. Let me explain:
I created an image to show you, what I'm talking about:
I got an image (Bitmap (1)), that as the size of 150w/200h.
Now I need to make the bitmap bigger ((2) 400w/400h), but the original image must have the same size. So that the image is embedded in white background.
I think one way to solve it is this:
* create a big bitmap
* create a canvas for it
* draw the original bitmap on the canvas
* draw the canvas
* generate a bitmap of the canvas
The problem for me is, that it must be done in a background thread without drawing a view.
I hope you understand me.

You can use the code bellow to achive it. Where smallBitmap is your original image and bigBitmap is the final image:
Bitmap bigBitmap = Bitmap.createBitmap(width, height , Bitmap.Config.ARGB_8888);
canvas = new Canvas(bigBitmap);
canvas.drawBitmap(smallBitmap, left, top, new Paint());
Regards.

This should do the trick.
Create a thread and in that thread object:
Create a new bitmap.
Create a canvas based on that bitmap.
Draw your bitmap to that canvas
and voila!
I hope this helps.

Related

create part of bitmap as a new bitmap

i have a problem and i don't know how to solve it. What i'm trying to do is, to create a Bitmap which is a part of another Bitmap.
I tried already this
Bitmap newBitmap = Bitmap.createBitmap(bitmap, (int)x, (int) y, (int)width, (int)height);
The width/height of the new bitmap is alright, but it's empty, every pixel is just transparent.
So i searched and found that i have to use canvas to draw the part again, because the return of createBitmap is a empty once. But i'm not sure since the call up here returns an immutable Bitmap, which Canvas doesn't like. I also not know which constructor i have to use with canvas.
Could someone help me?
Thanks and greetings
That one will create an INMUTABLE bitmap. Please, refer to http://developer.android.com/reference/android/graphics/Bitmap.html and read about that method.
To create a mutable one, use createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config). Pass in the widht and height of the part u wanna render in and the displayMetrics you get from yourActivity.getResources().getDisplayMetrics().
To render the area you want in the newly created bitmap, yes, use the Canvas. There are lots of methods. You can use this one: http://developer.android.com/reference/android/graphics/Canvas.html#drawBitmap(android.graphics.Bitmap, android.graphics.Rect, android.graphics.RectF, android.graphics.Paint)
You pass in a rect with the source area and the destination area.

how to crop top,left,right and bottom portion of imageview in android

In my application i am getting transparent pixels in my Imageview while taking screenshot.How to cut top,left,right and bottom portion of Imageview.I tried below code but not working
Bitmap bitmap = Bitmap.createBitmap(imgView.getWidth(),imgView.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
imgView.draw(canvas);
Bitmap result =Bitmap.createBitmap(bitmap,imgView.getLeft()+10, imgView.getTop()+50, imgView.getWidth()-20, imgView.getHeight()-100);
bitmap.recycle();
please help me to solve this issue.
I am not very sure what are you referring with "not working", but from first sight I would suggest you to change from
Bitmap result =Bitmap.createBitmap(bitmap,imgView.getLeft()+10, imgView.getTop()+50, imgView.getWidth()-20, imgView.getHeight()-100);
to
Bitmap result =Bitmap.createBitmap(bitmap, 10, 50, bitmap.getWidth()-20, bitmap.getHeight()-100);
But I would not recommend you to use this kind of approach which explicitly setting width and height value.

Set Picture into RemoteViews

I have some SVGs in my assets folder and I need to dynamically set them in my widget (on an ImageView).
I am using this library: http://code.google.com/p/svg-android/
This library returns a Picture or a PictureDrawable.
The only methods I can see to use on RemoteViews are setImageViewBitmap which obviously takes a bitmap.
I tried looking for code to convert a Drawable to a Bitmap like this:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;
But the bitmap is too small. When I create the bitmap in Illustrator I set the artboard size to 65 which is what comes through on the intrinsic width/height.
My widgets can be resized so the ImageView sizes are variable. Even if I set the width and height statically to some large number like this...
Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
then the resulting bitmap just has a bunch of whitespace below and to the right of a tiny image.
I guess I need to somehow draw the picture at a scaled up value as well as creating the Bitmap at size 300. Ideally I could figure out the size of the ImageView at runtime and set the proper sized Bitmap if I knew that. Is this the best approach and how would I do this? Perhaps there is a better approach I don't even know about?
I've not used android-svg but if it's using vanilla PictureDrawables, then it should be just a matter of not using the intrinsic bounds.
Try the following:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
pictureDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
pictureDrawable.draw(canvas); // do not access the Picture directly, that defeats the purpose
currentBitmap = bitmap;
In short, use the Drawable, not its Picture and set the Drawable's bounds to be the full canvas.
Have you tried createScaledBitmap?
createScaledBitmap()
I have tried svg-android and it has not worked for me for this very reason. Not to mention its severely limited feature set.
The point of using vector graphics is that I can generate images of any appropriate size to fit the UI View size. Which means the generation method must accept the size requirements at run-time, and not always use width,height declared in <svg> tag.
Hence I used the native implementation: libsvg-android, which exactly does that.
It directly renders to a canvas with given size:
long objId = SvgRaster.svgAndroidCreate();
SvgRaster.svgAndroidParseBuffer(objId, readString(mInputStream, "UTF-8"));
SvgRaster.svgAndroidSetAntialiasing(objId, true);
SvgRaster.svgAndroidRenderToArea(objId, mCanvas, 0, 0, mWidth, mHeight);
I ended up modifying the underlying Artboard size to be 300. None of the scaling up methods worked. So the final code I used was that which I originally posted:
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
currentBitmap = bitmap;
Once I had a 300x300 Bitmap I was able to set it into the RemoteViews using setImageViewBitmap .
This kind of defeated the purpose of using SVGs in the first place (at least as far as using them in widgets was concerned).
My problem was not the same as User117. Perhaps he did not name the outer layer 'bounds' as was required in the library. Either way, I managed to get the library working, albeit by having to modify the SVG Artboard size.
Hopefully with the increase in screen resolution Android will introduce SVGs as part of the platform soon.

refresh a bitmap or canvas in android

can anybody tell me how to refresh or
reset a bitmap or canvas to draw
another image on it?
actually i have a map binary file which contains bitmap tiles in bytes now problem is i have a buffer image with buffer image i get screen image to display. so when i move my screen image i need to refresh my buffer image and draw new tiles to display on screen.
here is code to display image.
bitmap = Bitmap.createBitmap(screenWidth * (int)mapState.getiBufferMult(), screenHeight * (int)mapState.getiBufferMult(), Config.RGB_565);
canvas = new Canvas(bitmap);
image = new ImageView(this);
this is not full source but i hope you guys can have idea.
thanks a lot
If you generate the bitmap that you want, you can say image.setImageBitmap(yourBitmap);
The Bitmap class also offers methods to modify an existing bitmap or replace just part of it.
If you are using the same bitmap and modifying it, call invalidate () on the ImageView when you have the bitmap ready.

can an image be manipulated using canvas

Is it possible to manipulate images using canvas? How do we get the image onto the canvas?
#Override
protected void onDraw(Canvas canvas) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap mBitmap = bitmap.copy(bitmap.getConfig(), true);
canvas = new Canvas(mBitmap);
Matrix matrix = new Matrix();
canvas.drawBitmap(mBitmap, matrix, mPaint);
}
I'm unable to see the image on the screen. canvas.drawBitmap() shouldn't be necessary since I'm using the constructor and passing mBitmap.
Yes you can, but you don't "get the image onto the Canvas," a Canvas is just an interface to draw onto a Bitmap. To do so, simply create a Canvas and give a reference to your mutable Bitmap:
Canvas c = new Canvas(myBitmap);
Very easy :)
You shouldn't do this in your onDraw method. Try to create an ImageView that should display the image and then set the bitmap via setImageBitmap(bitmap).
In general you are doing somethings wrong in your code.
Loading an image every time something should be drawn to the screen will slow down your application a lot. Try to load the image only once and then just edit and set it to the imageview.
You are assigning a new Canvas to the parameter you got. But this won't change anything on the screen. Think of it like somebody puts a paper in front of you and says:"Please write your message on the paper". Now you take another blank paper and write on it. But the other person only knows about his paper and will use this paper to read your message.

Categories

Resources