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);
Related
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)
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'm not having much luck wrapping my head around this, so I'm hoping someone can help me out.
I'm getting a drawable from an SVG using svg-android, but the drawable isn't scaling to the view. Everything that I've been able to find says I should draw directly to canvas and rescale the canvas, but when I try that it only seems to change the bounds but not scale the image.
This is what I've tried so far:
ImageView testview = (ImageView)findViewById(R.id.testview);
//Get SVG and convert to drawable
SVG vector = SVGParser.getSVGFromResource(getResources(),R.drawable.testvector);
Drawable test = vector.createPictureDrawable();
testview.setBackground(test); //displays fine, but won't scale to the dimensions of
//the View
//function that clips the image but doesn't scale:
Drawable testTwo = new CustomPictureDrawable(vector.getPicture(),
(float)0.5, (float)0.5);
testView.setBackground(testTwo);
class CustomPictureDrawable extends PictureDrawable {
private float scalex, scaley;
public CustomPictureDrawable(Picture picture, float scalex, float scaley) {
super(picture);
this.scalex = scalex;
this.scaley = scaley;
}
#Override
public void draw(Canvas canvas) {
Matrix original = canvas.getMatrix();
canvas.scale(scalex, scaley);
super.draw(canvas);
canvas.setMatrix(original);
}
}
//doesn't display anything
Picture testThree = vector.getPicture();
Bitmap b = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(b);
c.drawPicture(testThree, new Rect(0,0,10,10));
testview.draw(c);
I've also found a function that will create a scaled bitmap, but the image quality is significantly reduced, so I may as well just use scaled PNGs.
Obviously I'm missing something, and my lack of experience is making it really frustrating.
What I'd like to be able to do is have svg-android completely re-scale the SVG before pulling a Picture or PictureDrawable out of it, but I can't figure out how to step through the SVGParser, and running multipliers on every coordinate pair would probably be super resource intensive anyway.
[edit] Is the only way to scale and re-draw the Picture and assign that to a view to create custom Views and override OnDraw?
i.e.
Picture testThree = vector.getPicture();
Bitmap b = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(b);
c.drawPicture(testThree, new Rect(0,0,10,10));
//CustomView extends ImageView or Button or whatever with OnDraw overridden and no
//other changes
CustomView testview = (CustomView)findViewById(R.id.testview);
testview.OnDraw(c);
Am I on the right track? Canvas c would overwrite the default canvas (which is what I want), wouldn't it?
I figured it out. Or at least figured out a method that works. The answer was staring me in the face in that scaled bitmap function that I didn't like. I fundamentally misunderstood how the Picture class and Draw calls work.
Code that seems to have pulled it off:
//Get a Picture from the SVG
SVG vector = SVGParser.getSVGfromResource(getResources(), R.raw.testvector);
Picture test = vector.getPicture();
//Redraw the picture to a new size
Bitmap bitmap = Bitmap.createBitmap(desired width, desired height, config);
Canvas canvas = new Canvas(bitmap);
Picture resizePicture = new Picture();
canvas = resizePicture.beginRecording(desiredWidth, desiredGeight);
canvas.drawPicture(test, new Rect(0,0,desiredWidth, desiredHeight);
resizePicture.endRecording();
//get a drawable from resizePicture
Drawable vectorDrawing = new PictureDrawable(resizePicture);
I can size it to whatever View I want by getting desiredWidth and desiredHeight from getWidth() and getHeight() calls, and setBackground(vectorDrawing) at the end to put it on the View.
I had a similar problem and this is what I learned and how I solved it.
First I tried to render my svg directly into a canvas
svg.renderToCanvas(canv);
which didn't have the size I wanted. Then I tried to resize the image using
svg.renderToCanvas(canv,new RectF(0,0,200,200));
and
svg.setDocumentWidth(200);
svg.renderToCanvas(canv);
but both didn't work, so I started to look at my SVG. The problem was that my SVG started like this
<svg width="66.3679625" height="100.4148375" xmlns="http://www.w3.org/2000/svg">
and it seems impossible, once width and height are set in the svg, to change this in code afterwards. There is also an answer in the AndroidSvgFAQ where they recommend to remove those attributes. So I changed this to
<svg version="1.1" viewBox="0 0 1280 696" xmlns="http://www.w3.org/2000/svg">
The viewBox attribute is the frame of my SVG picture, so if now I use the code above
svg.setDocumentWidth(200);
svg.renderToCanvas(canv);
I get the content of the box with upper left angle at (0,0) and lower right angle at (1200,696) resized such that the width is 200. You can change the behaviour for the ratio initialy it keeps ratios. Note that one can omit width, heigth and ViewBox in SVG and set the ViewBox programmatically
svg.setDocumentViewBox(0,0,width,height);
To get to know more about the ViewBox attribute see W3ViewBox.
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.
While rotating a bitmap using matrix, vertex is not stable..
Matrix matrix = new Matrix();
matrix.postRotate(mDegree,100,100);
mCompasstemp = Bitmap.createBitmap(mCompPic, 0, 0, mCompPic.getWidth(), mCompPic.getHeight(), matrix, true);
mCompassHud.setImageBitmap(mCompasstemp);
Output of my code is like
-bitmap will rotate.
-vertex of my bitmap is not stable.
-Bitmap is resizing
I need disable image resizing and make the rotation stable.Can you please suggest a solution for this?
Rather than creating your new Bitmap directly from the original, another (more straight-forward, imho) option is to create the resultant Bitmap, create a Canvas with that Bitmap, then do your rotation/translation/scaling on the Canvas and draw the original Bitmap onto the new Bitmap via the Canvas.
Basically, you're looking, then, at:
scaledImage = Bitmap.createBitmap (croppedWidth, croppedHeight, Bitmap.Config.ARGB_8888);
Canvas offscreenCanvas = new Canvas (scaledImage);
Matrix matrix = new Matrix();
matrix.setRotate (rotations, centreX, centreY);
matrix.postScale(scaleX, scaleY);
offscreenCanvas.setMatrix (matrix);
offscreenCanvas.drawBitmap (pickedImage, 0, 0, new Paint(Paint.DITHER_FLAG));
Not sure if this is what your looking for but it might help.
Android uses its built in compatibility features to scale and render a bitmap appropriately for screens with different pixel densities. There are two methods of scaling, pre-scaling and auto-scaling.
It will pre-scale bitmaps' from resources and auto-scales when the bitmap is being drawn internally (which is what your doing be using createBitmap).
Go to http://developer.android.com/guide/practices/screens_support.html and check under:
4.Use density and/or size-specific resources:
Pre-scaling and auto-scaling of bitmaps and nine-patches
I have tried this code, and the rotate is stable at the center of the bitmap
matrix.reset();
matrix.setRotate(degree, Xpos+bitmap.getWidth()/2, Ypos+bitmap.getHeight()/2);
and then in canvas doDraw()
canvas.setMatrix(matrix);
canvas.drawBitmap(bitmap, Xpos, Ypos, null);
canvas.setMatrix(null);
The Xpos and Ypos is the X and Y position of the bitmap
The setMatrix(null), set the matrix to null, so that the rotate didn't affect the after bitmap
And it didn't always create new bitmap, so it's great for performance
I hope that help
I know its an old question but, all answers with code imply a canvas, so heres a solution without a canvas that worked for me :
Matrix matrix = new Matrix();
matrix.postRotate(mDegree,100,100);
mCompasstemp = Bitmap.createBitmap(mCompPic, 0, 0, mCompPic.getWidth(),
mCompPic.getHeight(), matrix, true);
mCompasstemp = Bitmap.createScaledBitmap(mCompassTemp, mCompPic.getWidth(),
mCompic.getHeight(), false);
mCompassHud.setImageBitmap(mCompasstemp);
So basically after rotating your bitmap you rescale it to the desired size. Might not be best practice, but at least you dont have to create an extra canvas if you dont want/need to, since the involving Paint() Object is not an inexpensive operation either.