Hello everyone I am new to android and I just have a quick question. I am making an animation using Bitmaps and drawing them to the canvas using the onDraw(). I am trying to draw a background to the canvas that I made in photoshop. I already uploaded it as a drawable and then decoded it to a Bitmap then added it to the onDraw method. It draws, but I doesn't fill the entire canvas like I want it to. Any suggestions and or fixes? Thank you so much, I looked around and saw something about converting the bitmap to an image view but didn't really understand all to well.
Use drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) where the rect for source is the entire bitmap (from 0,0 to width, height) and dest is the entire canvas. This will stretch the bitmap to fill the canvas. Be warned this can cause the aspect ratio to be off and/or issues with fuzziness or blockiness.
Related
How can I save the image on a canvas to bitmap?
There are several related posts, but I didn't find the one works for me. To make it clear, here is my problem
What's on my canvas:
An bitmap image which was drawn to the canvas using canvas.drawBitmap(oldBitmap, x, y, null);
Some paths drawn overlay to this image on the same canvas using canvas.drawPath.
Both the image and the paths are draw on a customized SurfaceView. And both they are draw to the canvas in the onDraw function. Or more specific, the implementation is in the following link:
Android: custom draw on an given image (with SurfaceView)
Now I need to save the image on canvas (the oldBitmap and paths I drew) to a newBitmap. Could someone tell me how to do that?
Suppose, we've got a display sized 100x100, an image 100x50, and I want to enlarge that image so that it has the same height as the screen does. So the image will be 200x100. After that I want to make the animation of image movement to the left and to the right. I've found 2 ways of the realization of my task:
To create a new image using Bitmap.createScaledBitmap(). But the
image created this way will occupy twice more RAM.
To use matrix.setScale() in every onDraw(). But this works very
slow. Is there any other better solution?
The second method is slow because you calculate your "matrix" on every frame! Since your matrix is always the same thing and it does not change, you can pre-calculate your matrix and save it as a "field" in you class, then just use this function to draw it on screen:
canvas.drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)
It should be fast enough. The other option is to use:
canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
Again, try to pre-calculate stuff as much as possible. In this case, pre-calc src and dst rectangles.
It seems to be a very trivial thing, but I am not able to find a sample code for it.
I am trying to do a very simple thing. On a canvas I plan to draw a round rect and save that in a drawable which I can then set as a background of a button.
Now I am looking for 2 things, how to draw and create a round rect so that the transparency effect can be created (like in a png drawable)
Second how to save the canvas to an image.
Please help
Please try to use this code.
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.color.transparant);
canvas.drawBitmap(bitmap, matrix, paint);
I would like to create a 'graph paper' look to the Bitmap I am drawing via a Canvas, and trying to figure out the best way to do this.
I can't pass a source Bitmap containing the graph paper background to the Canvas constructor, as I am getting the Canvas in a SurfaceView via the .lockCanvas() call.
Some solutions I've tried:
I've tried implementing this solution in my SurfaceView's Thread.run(), but the issue I believe is when the BitmapDrawable is converted to a Bitmap... it loses the tiling properties.
canvas = mSurfaceHolder.lockCanvas(null);
BitmapDrawable TileMe = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.editor_graph));
TileMe.setTileModeX(Shader.TileMode.REPEAT);
TileMe.setTileModeY(Shader.TileMode.REPEAT);
Bitmap b = TileMe.getBitmap();
canvas.drawBitmap(b, 0, 0, null);
If I use the Canvas.drawBitmap method that takes a destination RectF as a parameter, it looks like the bitmap will be tiled to fill the RectF... but how do I declare a RectF reliably that fills the entire view area?
Setting the Activities background to the desired graph paper look also doesn't work, as the bitmap/canvas layout is opaque and blocks that from being seen.
Any ideas how to achieve this?
You have two easy solutions:
Either use a BitmapDrawable, but instead of extracting the Bitmap, just call BitmapDrawable.draw(Canvas). Don't forget to set the drawable's bounds to fill your drawing area.
Create a Paint with a BitmapShader and draw a rectangle with it (this is basically what BitmapDrawable does).
I'm sure there is a way to get a tiled effect using a SurfaceView. Unfortunately, it looks like you can't use the BitmapDrawable with a canvas. So you would probably have to implement you own custom tiling method by creating your own series of Rect's on the Canvas and drawing a scaled bitmap to each one.
It honestly wouldn't be that hard. Just get the width/height of the view, and create an array of Rect's based on this data that you will draw the Bitmap to.
Alternatively, if you don't need to make modifications to the actual tiled background on the fly, just draw it as a background and draw the SurfaceView on top of it. That post you linked provided multiple solutions to tiling a BitmapDrawable that you could implement.
I have a widget which looks like this:
Every cone is a "touchable element". Now I want to put a bitmap texture above each cone. However, bitmap images are all rectangular, so a bitmap texture above one cone would interfere with the bitmap texture above another cone.
I'm wondering what is the best solution to this approach. Should I just create an image which fits (as a rectangle) exactly above the cone and make the non used areas transparent?
A second question is, how do bitmap textures work with stretching? Because this whole circle draws itself to fit the whole screen size, while bitmap textures are pretty much one size.
Offhand I can't think of a better way to draw bitmaps over those cones than your own suggestion of using transparent zones.
However, I can help with your second question as stretching bitmaps is not hard. You've got a few options in the Canvas class. For example:
canvas.save();
canvas.scale(xRatio, yRatio);
canvas.drawBitmap(....);
canvas.restore();
You can also use a Matrix, using matrix.postScale(xRatio, yRatio), to then generate either a larger bitmap and draw it normally, or pass the matrix in to your canvas.drawBitmap(....) command to make it scale while it draws.
All of these methods assume you have access to the drawing canvas itself. If you are using a view, you can subclass it and override the onDraw(Canvas canvas) method to grab the canvas before it starts drawing it. If you're using a SurfaceHolder, then you should already know how to get the canvas.
Edit: I forgot the third method I was going to describe. You can use the canvas.drawBitmap(bitmap, srcRect, dstRect, paint) to also make the canvas scale the bitmap to fit the destination rectangle. In short, there are lots of methods to do this - pick the one that's easiest based on your application!