Set Picture into RemoteViews - android

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.

Related

How to draw an image on top a bitmap and combine as one bitmap in Android Java

I am developing an Android application using Java. I am doing some image manipulating in my application. First of all, I like to save I have no knowledge about image processing. But, I am trying to get into it. What I would like to do now is draw a simple image on a bitmap and save it as one bitmap.
I am loading an image from asset folder as a bitmap like this.
Bitmap rectBitmap = BitmapFactory.decodeStream(istr);
Let's say the photo is a just simple rectangle like this.
Then I would like to draw a bitmap (triangle shape) using coordinates point. The image would be some this.
My imagination of code would be like this.
rectBitmap.drawOnTop(coorPointOneValues, coorPointTwoValues, coorPointThreeValues);
Coordinate point values would be x and y value since I am working on the 2D coordinate system.
Then I would like to save the image something like this after drawing traingle
rectBitmap = rectBitmap.saveBitmap();
How can I do it? The scenario mentioned is a possible way that I can think of. If it is not possible, what would be the other way around?
You can do it like this.
1, read or create your bitmap:
Bitmap rectBitmap = BitmapFactory.decodeStream(istr);
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2, create a canvas on the bitmap:
Canvas canvas = new Canvas(bitmap);
3, draw something:
canvas.drawColor(Color.RED)
canvas.drawRect / canvas.drawLine / canvas.drawArc ...
//for triangle shape you can use drawPath
4, save the bitmap:
bitmap.compress(CompressFormat format, int quality, OutputStream stream)

Using NinePatch (.9.png) for making scaled Bitmaps

I need to draw a Bitmap for my new Android Application. Since my content view is set to a Game-Panel class I've created. I want to display images through Bitmap. Just because its more convenient, and easier to do then anything else.
From what I've gathered from researching, Nine Patch images (.9.png) are used in android to scale more properly then regular Drawable images. It also says "This can be used for scaling properly in backgrounds. For example a regular button background...". If Nine Patch images are made to scale better. Can You use a Nine Patch for making a Bitmap in a specific width and height because it would scale more properly? For example
Bitmap b = createBitmap(BitmapFactory.decodeResource(NinePatchImage), x, y, width, height);
Is this possible? Should I do it a different way? Should I create a bitmap first and then just scale the bitmap? Is this even necessary? Please help.
Bitmap bitmap = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
NinePatchDrawable drawable = (NinePatchDrawable) getResources().getDrawable(R.drawable.myDrawable);
drawable.setBounds(new Rect(x, y, width, height));
drawable.draw(canvas);

create a bitmap with a bitmap inside

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.

Android gradient graphics quality (scaled bitmap)

I want to display a splash screen with an gradient in my app. But the quality of the gradient in the background is pretty bad. So I create a simple radiant gradient to have a closer look.
Edit: Maybe I should have mentioned that I am using BitmapFactory.createScaledBitmap(); and BitmapFactory.decodeStream() (The graphics are in the assets folder.)
Thats the result:
Original *.jpg (Quality 100%)
Screenshot Xperia X10 (I took the screenshot with Eclipse)
You can do it without any jpg file, but using a shape drawable. Create rectangle and provide necessary parmas for gradient tag.
In order to scale a bitmap and keep ARGB_8888 I created a new bitmap in the desired size. Then I use canvas to draw the intrinsic bitmap on the new bitmap with drawBitmap. This method allows me to scale the bitmap before I draw it on the new bitmap.
inputStream = assetManager.open(path);
originalBitmap = BitmapFactory.decodeStream(inputStream, null, opts);
Bitmap resizedBitmap = Bitmap.createBitmap(desiredX, desiredY, Config.ARGB_8888);
Canvas canvas = new Canvas(resizedBitmap);
canvas.drawBitmap(originalBitmap, null, new Rect(0, 0, desiredX, desiredY), null);
inputStream.close();
return resizedBitmap;

Android: how to simply draw a bitmap on another bitmap

I'm new to Android dev and I'm having a hard time trying to do something which seems obvious to me: drawing little images on top of a bigger image.
Let's say that I have a 500x500 image and I want to draw icons at different locations. Icons are png files that I load with:
Bitmap img =
BitmapFactory.decodeResource(getResources(),
R.drawable.idIcon1)
My "background image" is a LayerDrawable.
Then, I am totally lost... Do I have to create a canvas ? How to draw on my "background image" my icons at different positions?
int positionLeft=0;
int positionTop=0;
Bitmap newBitmap =Bitmap.createBitmap(backgroundBitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(backgroundBitmap, positionLeft, positionTop,null);
positionLeft=100;
positionTop=100;
canvas.drawBitmap(iconBitmap,positionLeft,positionTop,null);
imageView.setImageBitmap(newBitmap);
You're making simple things difficult. Just use a layout with android:background attribute, and then add ImageViews dynamically with the necessary bitmaps inside.

Categories

Resources