Android canvas flipping with canvas.scale - android

I have a little question. :)
In my new live wallpaper I am flipping the canvas horizontally
with:
c.scale(-1f, 1f, screenWidth* 0.5f, screenHeight* 0.5f);
drawFlipped();
c.scale(-1f, 1f, screenWidth* 0.5f, screenHeight* 0.5f);
This all works great but I also happen to have an ontouch event
where the user can click the different moving object defined as a Rect.
But when the rect is drawn on the flipped canvas the x,y coordinates
are not correct. Its still in mirror and I understand that because I flip
the canvas back after drawing the sprites.
My question is: How can I calculate the on touch event from the right side of
the screen?
I tried: Screenwidth - object.x - object.width but it did not help much :(

This works for me:
int screenWidth = getScreenWidth();
Rect newCoordinates = new Rect(object.getRect()); //copies old coordinates
newCoordinates.right = screenWidth - object.getRect().left; //switch right and left since the Rect is being mirrored
newCoordinates.left = screenWidth - object.getRect().right;
object.setRect(newCoordinates);

Related

How to only focus on the positive Y-axis portion of graph?

I am new to OpenGL, and have recently successfully drawn my first shapes with the guide on the Android Developers website. Right now I am trying to only focus on the upper half of the graph on my OpenGL 2D rendering. So as you guys know, right smack in the middle of the screen is (0,0). However now I want the origin to be at the middle of the bottom of the screen, while maintaining the y axis 0.5f value to be at the middle of the screen and 1f at the top of the screen. In essence, the negative y axis portion is not in view.
These are the coordinates of my square:
float squareCoords[] = {
// x , y , z
-0.5f, 0.5f , 0.0f //top left
-0.5f, 0f , 0.0f //bottom left
0.5f, 0f , 0.0f //bottom right
0.5f, 0.5f , 0.0f //top right
};
This is how I want the square to look on the screen
Ive tried using the camera to focus view but it makes the view bigger(the max y-value and x-value increases, and the object becomes smaller) in the renderer class:
Matrix.setLookAtM(viewMatrix,0,0,0.5f,3,0f,0.5f,0f,0f,1.0f,0.0f)
Does it have something to with GLES20.glViewport? The best I can come up with from online research is a function ortho2d() but it seems like Android Studio does not support it. Any help appreciated.Thanks!
You can use Matrix.orthoM
Matrix.orthoM(
projectionMatrix, 0,
left, right,
bottom, top,
near, far
);
And multiply it with viewMatrix set with Matrix.setLookAtM to obtain View-Projection matrix:
Matrix.multiplyMM(
vpMatrix, 0,
projectionMatrix, 0, viewMatrix, 0
);
Then use vpMatrix in your shader
void main() {
gl_Position = u_VpMatrix * a_Position;
}
...
int uVpMatrix = glGetUniformLocation(program, "u_VpMatrix");
...
glUniformMatrix4fv(uVpMatrix, 1, false, vpMatrix, 0);
Found the soln after further trial and error
In the renderer class at onSurfaceChanged(GL10 unused, int width ,int height){
//edit the Matrix.frustumM method, value for parameter bottom should be set to 0
Matrix.frustumM(projectionMatrix,0,left,right,bottom,top,near,far)
}
Should help with people who want to change the position of their origins to any other location as well, simply change the left,right,top,bottom values.
or refer to this stackoverflow: Set origin to top-left corner of screen in OpenGL ES 2
for other possible solutions.

Find the center of a shape (path) on a canvas (Android Studio)

Is it possible to find the center of a shape on a canvas in Android studio? I've logged the touch points and can't seem to figure it out. It does however appear that the canvas (0,0) point is the top left, but the paths/shapes on the canvas see the center point as (0,0).
For an example of what I'm talking about check out the attached image, I'm trying to find where the green dot is.
Thanks in advance for any help.
IMG
To find the center of a Path use the method computeBounds(bounds,exact) which will set the first argument RectF bounds to the extent of the Path. Then it is just a matter of getting the mean of the left right and top bottom coordinates to get the geometric center of the path.
// mPath is your path. Must contain more than 1 path point
RectF bounds = new RectF();
mPath.computeBounds(bounds, false); // fills rect with bounds
PointF center = new PointF((bounds.left + bounds.right) / 2,
(bounds.top + bounds.bottom) / 2);
No Need for Mathematical Calculations
RectF bounds = new RectF();
mPath.computeBounds(bounds, false);
float centerX = rectF.centerX();
float centerY = rectF.centerY();

In Android, What kind of transform does mapRect api of Matrix class performs?

I would like to know about the function of mapRect api available under Matrix class in Android. If I have a sample Matrix A and Rectangle R, then for
RectF R = new RectF(t1,t2,t3,t4);
A.mapRect(R);
what kind of transformation is likely to happen to R. It would be more helpful if someone can illustrate the mapRect() api with some suitable examples.
Here's a very simple example:
Let's take a matrix:
Matrix matrix = new Matrix();
Set that matrix to scale everything twice as large:
matrix.setScale(2.0F, 2.0F);
Create a rectangle that is 10x10 with origin in upper left corner:
RectF rect = new RectF(0F, 0F, 10F, 10F);
So when we call
matrix.mapRect(rect);
the input rectangle we created is replaced with the output rectangle, which is the result of transforming the input:
rect.left = 0F;
rect.top = 0F;
rect.right = 20F;
rect.bottom = 20F;
There is another version of the method
matrix.mapRect(RectF dst, RectF src);
that does the same transform without affecting the input rectangle.
What is a matrix?
Consider a mirror. The mirror takes your image and creates a horizontally flipped version of your image.
Consider a microphone and an amplifier. They take your voice and create a louder version of your voice.
That's what a matrix is. It's a transformer. It takes an input and creates an output that is based on the input. So a matrix can transform a point, a rectangle, a circle, a polygon...
For more info, see my answer How does Matrix.postScale( sx, sy, px, py) work?
Also check out Affine transformations | Wikipedia. There is an awesome graphic that shows the different affine transforms and their effects.

How can i get the image matrix co ordinates in android?

I am developing an application where is included crop functionality and in this I'm blocked at below scenario.
The scenario:
Crop overlay is not circulating the whole image when we apply straightening to the image if we change the crop overlay, ratio also is not working.
I got the code from google but it is related to matrix, I have tried using matrix, but here I need to find the coordinates(edges) of matrix to move the overlay.
How to find it? If anyone has any idea please help me...
I added the image to explain the problem
Thanks in advance..
I am using the below code but I didn't get the exact coordinates of the matrix
RectF r = new RectF();
mymatrix.mapRect(r);
Rect rect =
new Rect((int)r.left,int)r.top,int)r.right+width,int)r.bottom+height);
You should not use mapRect if you are applying rotation on your matrix. My advice is to figure out the 4 initial points representing each rectangle edge (the image itself) and use mapPoints instead.
Lets say you have an image 200px wide and 200px tall with its top left corner positioned at origin (0,0).
If you rotate this image from its center (100,100) 45 degrees and then scale it 200% from its center we will have the following scenario:
//original image coords
float[] points = {
0f, 0f, //left, top
200f, 0f, //right, top
200f, 200f, //right, bottom
0f, 200f//left, bottom
};
Matrix matrix = new Matrix();
//rotate 45 degrees from image center
matrix.postRotate(45f, 100f, 100f);
//scale 200% from center
matrix.postScale(2f, 2f, 100f, 100f);
//get the new edges coords
matrix.mapPoints(points);
After calling matrix.mapPoints(points) the points array will have the updated coords. This means points[0], points[1] will have the new left top image coord and so on.
The even indices represents the abscissas and the odd indices represents the ordinates on the points array.

Getting the size of the OpenGL window in Android

I'm trying to draw a square in OpenGL ES (Android), 2D and covering the whole screen.
At the moment I'm just using trial and error but am sure there has got to be a better way to get the size of the screen. Below is how I'm currently initializing square:
float[] square = new float[] { -0.1f, -0.1f, 0.0f,
0.1f, -0.1f, 0.0f,
-0.1f, 0.1f, 0.0f,
0.1f, 0.1f, 0.0f };
Ideally the 0.1f in the x axis would be be the width and 0.1 in y the height of the window. Any help would be greatly appreciated.
Cheers
I think the size of the screen depends on your projection. For 2D graphics, most people use glOrtho to define the parallel projection. It's up to you to specify the size here.
Also, you can specify a larger size and have multiple 2D textures positioned within the clipping bounds, or you can have a single texture with the vertices mapped to the corners of your specified projection. This single texture would contain your entire display contents.
The following site explains this a little better.
http://www.scottlu.com/2008/04/fast-2d-graphics-wopengl-es.html
. . .
WindowManager w = getWindowManager();
Display d = w.getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
. . .
see http://groups.google.com/group/android-developers/browse_thread/thread/229c677ef0c5ae97

Categories

Resources