Translating a view from one coordinate to another in android - android

I am working on an application where i need to translate a view from one cordinate to another.
x1 = 191, y1 = 300
x2 = 50 , y2 = 150
I used the following code, but its behaving very strangly, I mean it's nor translating between those two points.
Code:
Animation transAnim = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, x,
Animation.RELATIVE_TO_PARENT, pawnInitLocation[0],
Animation.RELATIVE_TO_PARENT, y,
Animation.RELATIVE_TO_PARENT, pawnInitLocation[1]);
transAnim.setDuration(1000);
transAnim.setFillEnabled(true);
transAnim.setFillAfter(true);
v.startAnimation(transAnim);
I am new to animations in android and havent really achieved anything like this. Any help would be great.

Animation.RELATIVE_TO_PARENT specifies that the value you are providing is a float value to be multiplied by the width or height of the parent. 1.0 corresponds to 100% of the parent's corresponding dimension.
An example of this would be animating a view completely off the screen by specifying 1.0 for 100% creating an exit-right animation or -1.0 for -100% creating an exit-left animation.
TranslateAnimation

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.

TranslateAnimation from middle to Top Left without cutting off view

I have a parent container called mContainer and a child view called mChildImageView. The ImageView is positioned in the middle of the container and I am trying to translate the ImageView to the top left without cutting off the ImageView.
What I would like is to translate the top most point of the ImageView to the top left part of mContainer, this way it would make the ImageView cut off.
This is the following code I am using, as you can see I am using Absolute coordinates, I would like to make it more dynamic how can I accomplish this?
TranslateAnimation a = new TranslateAnimation(
Animation.ABSOLUTE, // Specifies X-Type interpretation, ABSOLUTE, RELATIVE_TO_SELF, RELATIVE_TO_PARENT
0.0f, // Change in x coordinate at start of animation, can be absolute or percentage if RELATIVE
Animation.ABSOLUTE,
-30, // Change in x coordinate at end of animation, can be absolute or percentage if RELATIVE
Animation.ABSOLUTE, // Specifies Y-Type interpretation, ABSOLUTE, RELATIVE_TO_SELF, RELATIVE_TO_PARENT
0.0f, // Change in y coordinate at start of animation, can be absolute or percentage if RELATIVE
Animation.ABSOLUTE,
30 // Change in y coordinate at end of animation, can be absolute or percentage if RELATIVE
);
mChildImageView.setAnimation(a);
mChildImageView.animate();
If you want to change TranslationX or/and TranslationY parameters of a view with relation to its parent, I highly recommend you to use Animators instead of Animations as they have some drawbacks (for example: Android - Animation Issue).
Possible variant:
mChildImageView.animate().translationX(newTranslationX).translationY(newTranslationY).setDuration(duration).start();
I figured it out so to get it to move to the top left we can do the following
TranslateAnimation a = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
-20.0f, // Translate to left - This is arbitrary enter your own value
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
-15.0f // Translate up - This is arbitrary enter your own value
);
mChildImageView.setAnimation(a);
mChildImageView.animate();
RELATIVE_TO_PARENT uses percentages so 0.0f to 1.0f

How to combine scale and transform animation in android?

I need to translate and scale image view like on this picture from state 1 to state 2,
State 1 is center of screen, state 2 is any location on screen;
I have combined to animations
a= new TranslateAnimation(Animation.ABSOLUTE,startx,Animation.ABSOLUTE,destX*5,Animation.ABSOLUTE,starty,Animation.ABSOLUTE,(destY-32)*5);
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
a = new ScaleAnimation(1f, 0.2f, 1f, 0.2f);
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
Where "as" is AnimationSet. When I start the animation.
Everything works well, start and end position ok, but translation between them not going along "straight" line. It goes like an arc, on picture 2;
What kind of scale-animation and translate-animation need I perform and in which order to get straight transformation?
picture 1 pic1
picture 2 pic2
After changing order of animations and removing mult by 5 in translateanimation all works ok
a = new ScaleAnimation(1f, 0.2f, 1f, 0.2f);
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
a= new TranslateAnimation(Animation.ABSOLUTE,startx,Animation.ABSOLUTE,destX,Animation.ABSOLUTE,starty,Animation.ABSOLUTE,(destY-32));
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);

Is there a way to eliminate Interpolation on an Animation?

I'm sure I'm missing something obvious but I'm unable to get my ViewFlipper to animate smoothly from one layout to the next.
My desire is to have the animation move at a constant speed. Not to accel or decel. The overall desired effect is to have the flipper animate in perpetuity with no visible stutter or hiccup, so that the two (or more) layouts just keep moving along, as though on a looped reel.
However, when I don't set an Interpolation it defaults to *some default built-in interpolation that slows down to 0 towards the end.
And when I *do set the Interpolation there's no number I seem to be able to pass in to give me the smooth animation result. 0 stops the animation outright and 1 does (seemingly) what the default does.
What am I overlooking?
here's my animation method that I am passing (as part of my custom Animation to the ViewFlipper:
public Animation inFromRightAnimation(int duration) {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f
);
inFromRight.setDuration(duration);
AccelerateInterpolator ai = new AccelerateInterpolator();//tried 0 and 1.0f as params without success
inFromRight.setInterpolator(ai); //
return inFromRight;
}
Try using LinearInterpolator instead of AccelerateInterpolator. LinearInterpolator ensures that your views will move with a constant velocity without any acceleration or deceleration.

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