Android - float parameters in drawBitmap - android

I used this function in my Android program:
public void drawBitmap (Bitmap bitmap, float left, float top, Paint paint)
However, I want to draw my bitmap not in the position 0 x 0, but in the position 10 x 10 (in PIXELS). The drawBitmap function, however, only accepts float numbers...
How can I achieve this??
Thank you in advance!

Have you tried drawBitmap(bitmap, 10.f, 10.f, ... )? Considering the transformation matrix of the canvas is set to the identity matrix, that is.
The reason those parameters are float is probably that the Canvas does not operate in an integer space (pixels), but in a user specified space defined by a transformation matrix. If you where to set a custom transformation matrix to scale by 2 then using 0.5, 0.5 would end up mapping to pixel 1, 1. This means you could also set a custom transformation to translate by 10, 10 and then just simply draw the bitmap without specifying a destination.

Related

How to find center position of a bitmap that rotated and scaled with a matrix?

I want to calculate center of a bitmap that is drawn on a canvas with a matrix, it can be rotated, scaled or translated with a arbitrary value. What is the easiest way to find center of the this bitmap on canvas?
You need to apply the matrix to the coordinates of the center of bitmap.
If you use a canvas that has a transformation matrix, you can get the final matrix through Canvas.getMatrix()
If you draw the Bitmap on the Canvas with a Matrix : drawBitmap(bitmap, matrix, paint), then you you need to use that Matrix, or concatenate it to that of the Canvas (in the case it has one).
Then you can finally apply that matrix to the center of the matrix using Matrix.mapPoints.
Something like :
canvas.drawBitmap(bitmap, bitmap_transform, paint);
Matrix full_transform = new Matrix(canvas.getMatrix());
full_transform.postConcat(bitmap_transform);
float[] center = new float[] {bitmap.getHeight()/2.0, bitmap.getWidth()/2.0};
full_transform.mapPoints(center);
Alternatively, if you apply transformations to your bitmap without a matrix, you can use the full_transform.postRotate, full_transform.postScale, etc with the same values. In particular, if you draw your bitmap with drawBitmap(bitmap, left, top, paint) then you need to do a full_transform.postTranslate(left, top).
If you're looking for the "easiest" way then just sticking with the translate rotate and scale functions would solve the problem. The reason that those were developed was so developers wouldn't have to do vector calculus for simple animations. Also the only value you would have to actually calculate in that sense it is the translate value after you take into account the original coordinates.

Is Android canvas.drawBitmap documentation wrong?

Is the Android documentation for canvas.drawBitmap wrong? It says:
public void drawBitmap (Bitmap bitmap, float left, float top, Paint paint)
Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, transformed by the current matrix.
Well, x and y don’t seem to be floats, they’re ints; is that correct?
Say I want to overlay the bitmap (which is the size of the available screen, and is bound to a canvas of the same) over the whole available screen. It seems sensible I would:
canvas.drawBitmap(myBitmap, 0, 0, mPaint);
doesn’t it?
But that doesn’t work. What does seem to work is:
canvas.drawBitmap(myBitmap, 2000000, 1000000, mPaint).
Now that statement seems to me to tell the bitmap that it should draw itself a huge distance
Outside the screen! What am I missing here?
In this method x and y are floats, not ints. But like mentioned in the documentation, the x and y coordinates of the bitmaps will be affected by the matrix currently set on the Canvas. In the case of a ScrollView for instance, the matrix could very well contain a very large translation.
What this means is that the coordinates 0, 0 will draw the bitmap at the current origin of the Canvas. That origin is defined by the matrix you can query with getMatrix().

Android OpenGL ES drawArray with pixel instead of range [-1,1]

i´m new to openGL and want to draw lines on an GLSurfaceView.
I switched from canvas to openGL cause of the performance.
I know that OpenGL is more likely for 3D drawings, so pixel are not used because of this.
I want to draw 2D graphics like lines but need the dimensions in pixel, only x and y values without 3rd dimension.
Set properly your projection and view matrices. For first, use orthographic projection, second leave identity.
P = Diagonal(2 / W, 2 / H, 2 / (10 + 10), 1)
// Assume Znear=-10, ZFar=10, W and H are width and Height of the screen.
So coordinates you see will bew in cube -W/2 to W/2, -H/2 to H/2 and -10 to 10. You may even translate in view matrix to move origin from -W/2, -H/2 to 0, 0 translating view matrix: V = Translate(W/2, H/2, 0).

how to get pixel Color value on a zoomed bitmap?

I am using zooming and panning with sony ericssion tutorial with this link
http://blogs.sonyericsson.com/developerworld/2010/06/09/android-one-finger-zoom-tutorial-part-3/
and when I am using Bitmap.getPixel(x,y) in ontouchevent of image.Its giving different different RGB values of color.After zooming bitmap same problem occurs.I have tried x y coordinates diviing by zooming factor also.it doesnt work for me.
can plz someone help me to solve this problem.how to get the color value on a zoomed Bitmap?
getPixel is working on your unzoomed Bitmap.
I assume X and Y come from a touch event and are relative to the View.
I see in your link that the example is doing the resize using two Rect objects.
#Override
protected void onDraw(Canvas canvas) {
if (mBitmap != null && mState != null) {
canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint);
}
}
I assume that is is also how you did it.
Simply dividing your coordinates with the zoom ratio will not work because X and Y are relative to your view and not to the RectDst.
I generally use Matrix objects to do zooming because it is so easy to reproduce, invert, etc...
For exemple if your zoom had been done using Matrix you'll be able to invert it, give it your X,Y and get your original coordinates relative to your bitmap back.
But you've got a way to do it anyway, by calculating the Matrix using the 2 Rects:
//Recreate the Matrix using the 2 Rects
Matrix m = new Matrix()
m.setRectToRect(mRectDst, mRectSrc, false);
//Transform the coordinates back to the original plan
float[] src = {x,y};
float[] dst = new float[2];
m.mapPoints(src, dst);
myBitmap.getPixel(dst[0], dst[1]);
I did not test it but it should work or at least help you find your solution.
Bitmap.getPixel(x,y) get damm slow result .
prefer
int [] byteArry= new int [Bitmap.getWidth()*Bitmap.getHeight()] ;
Bitmap.getPixels(byteArry, 0, Bitmap.getWidth(), 0, 0, slotBitmap_1.getWidth(), Bitmap.getHeight());
the byteArry will returns the pixels color of image.

Android canvas.scale(-1,1)

So my aim is to flip an image horizontally then draw it on a canvas. Currently I'm using canvas.scale(-1,1) which effectively works and draws the image horizontally, however it also screws with the x axis values where before the scale the x position would be 150 and after I'd have to switch it to -150 to render in the same spot.
My question is, how can I make it so the x value is 150 in both cases without having to adjust the x position after the scale? Is there a more effective way to do this without taking a hit on performance?
I know this question is old, but I happened to bump into the same problem. In my situation, I had to flip the canvas when drawing on a class extending an ImageButton. Fortunately, the solution for this specific case was more elegant than I thought. Simply override the onDraw(Canvas) method as follows:
#Override
protected void onDraw(final Canvas canvas) {
// Scale the canvas, offset by its center.
canvas.scale(-1f, 1f,
super.getWidth() * 0.5f, super.getHeight() * 0.5f);
// Draw the button!
super.onDraw(canvas);
}
I've fixed this by applying the transformation to the bitmap prior to ever using it like this:
public void applyMatrix(Matrix matrix) {
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0,
mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
}
...
Matrix matrix = new Matrix();
matrix.preScale(-1, 1);
mSprite.applyMatrix(matrix);
Did you try repeating the canvas.scale(-1, 1)? It will effectively remove the transformation, since two negatives make a positive.

Categories

Resources