Normally, when you draw a bitmap to the canvas, it is rectangular. Is it possible to make it appear as a parallelogram instead?
Try canvas.skew(20, 0). That skews the drawing matrix in x, but you can obviously do y as well, depending on what you need.
Related
So on my canvas I have few rectangular areas where I draw different bitmaps.
Sometimes bitmaps get outside of their respective areas. I want these bitmaps to be cut off, so only the portion of bitmap inside of it's area is drawn.
I sure can calculate it manually (srcRect and dstRect)...
But isn't there a simpler solution?
I found clipBounds, but it doesn't seem to restrict drawing area in any way.
What you want is Canvas.clipRect(). You will need to know the bounds of the clipping rectangle, of course, but you don't need to worry about calculating a custom srcRect.
To use, save() the Canvas, do a single drawBitmap(), then restore() it to get your original clipping state back:
canvas.save();
canvas.clipRect(...);
canvas.drawBitmap(...);
canvas.restore();
I have a custom view wich contain some bitmaps and I want to set shadows for them, for that, I use this code:
shadowPaints=new Paint(Paint.ANTI_ALIAS_FLAG);
shadowPaints.setShadowLayer(10.0f, 3.0f, 2.0f, Color.BLACK);
canvas.drawBitmap(bmp, matrix, shadowPaints);
setLayerType(LAYER_TYPE_SOFTWARE, shadowPaints);
and my result is
as you can see my shadow actually is another bitmap with different x and y position but what I want is my shadow be a solid color
bitmap.
can anyone help me about this?
setShadowLayer is actually meant for putting shadows on text.
If you already know the bitmap you want to draw, you can just add a shadow in PhotoShop and draw the bitmap and shadow all at once.
If you don't want to do that, you could make a shadow by making a copy of the image, using a PorterDuff filter to make it all grey, use Renderscript to blur the image, and draw it on the canvas at an x,y offset before drawing the actual image on top of it.
Personally, I think PhotoShop is a lot easier.
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.
I would like to draw text on a canvas using the Canvas.drawText call for a game application, and later be able to scale it as if it was a bitmap object. I do not want to dynamically change its font size, but I want to scale it applying an affine transform.
Is it possible? I cannot find an adequate API call in the documentation.
Thank you!
Using Matrix you can scale it and that apply the matrix tranformation to the canvas with Canvas.concat(Matrix m) . Do this before drawing the text onto the canvas.
Once it's on the Canvas, it's not a separate object that you can do anything to. What you can do is use the Canvas as an API for drawing to a bitmap:
Create a Bitmap.
Create a Canvas backed by that Bitmap.
Draw your text into the Canvas.
Hang on to your Bitmap and do whatever you want with it.
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!