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.
Related
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.
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.
How can I fill the screen with texture? I can get screen size and density, also a bitmap size. But what next? should I transform texture with matrix or use special paint? Anything else? Thanks.
How about
Bitmap background = Bitmap.createScaledBitmap
(YourBitmap, ScreenWidth, ScreenHeight, false);
You've got a blank canvas, there's all kinds of things you can do with it! For example, to draw a bitmap, you could add the bitmap to res/drawable and do something like
bitmap = BitmapFactory.decodeResource(caller.getResources(), r_bitmap);
canvas.drawBitmap(bitmap, draw_x - (bitmap.getWidth() / 2), draw_y - (bitmap.getHeight() / 2), null);
This is a typical operation to draw a background or a sprite. You can also use Canvas.drawText(), Canvas.drawRectangle(), Canvas.drawColor(). Lots of things.
I'm working on a game that uses Canvas as the main drawing surface, check out onDraw() in my MainGamePanel class and draw(Canvas) in my GameItem class for some examples of drawing various bitmaps/shapes/colors to a Canvas. Just remember every time you draw, you will overwrite what's below it, so the order of operations definitely matters.
Try this:
Use a SurfaceView.
Create a Bitmap from the texture (.jpeg file) with the values you found eg. screen size.
Draw that Bitmap to the canvas.
I hope this helps.
I want to know how to cut an image in jigsaw form in android at run time. What i actually want is user can enter any image and then made puzzle of it.
Any API or method for doing this in android ?
Thanks in Advance.
You can use this method in Bitmap to cut it into a new BitMap of specified width and height pixels
starting from specified pixel location of x and y
public static Bitmap createBitmap (Bitmap source,
int x,
int y, int width, int height)
Assuming your puzzle piece is square, you can use createBitmap() function.
Returns an immutable bitmap from the specified subset of the source
bitmap. The new bitmap may be the same object as source, or a copy may
have been made. It is initialized with the same density as the
original bitmap.
I want to use coverflow view in my app. To get the reflection image part i have used the following code - http://www.androidsnippets.com/create-image-with-reflection
I have seen lot of forums/discussions about dithering and tileMode, I have tried all that discussed but nothing works for me.
FYI - I am creating a bitmap dynamically not using any bitmap in layouts. And I have attached the image to show how bad it is:
I have listed below what I have tried to solve this issue.
1. getWindow().setFlags(WindowManager.LayoutParams.FLAG_DITHER, WindowManager.LayoutParams.FLAG_DITHER);
2. getWindow().setFormat(PixelFormat.RGBA_8888);
3. BitmapDrawable baseImageDawable = new BitmapDrawable(getResources().openRawResource(imageId));
baseImageDawable.setDither(true);
baseImageDawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
Bitmap originalImage = baseImageDawable.getBitmap();
int width = originalImage.getWidth();
int height = originalImage.getHeight();
Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, 0, width, height, matrix, true);
But still the reflection image is very ugly.
How can I solve this?
Hmmm. I "think" your problem is that your bitmap is not being created as ARGB_8888 (despite your setFormat call). I would "suggest," instead of creating the Bitmap drawable using openRawResource, use BitmapFactory and make sure you specify Bitmap.Config.ARGB_8888. Then make your drawable from the bitmap. Just a thought--hard to guess at this distance without being able to step with eclipse. You might find this article interesting: http://www.curious-creature.org/2010/12/08/bitmap-quality-banding-and-dithering/
Edit: So, the problem, apparently, was in the call to createBitmap being used to allocate the reflectionImage. Moral of the story (IMHO): never use createBitmap methods that do not allow you to explicitly specify Bitmap.Config. Otherwise, as Forrest Gump might say, you never know what you're gonna get.
I found the location where should I set the config. While creating the reflection bitmap set the config as ARGB_8888. Its working now.