In android How to Translate to an absolute Point in Screen - android

I have an Imageview and an ImageButton and want the Imageview to move to the imagebutton's position.
The Imageview and its location on screen
myAnimation1.getLocationInWindow(imagePos);
The Image button and its location on screen
ImageButton.getLocationInWindow(buttonPos);
To translate or move the Imageview to the Imagebutton I tried
Animation move = new TranslateAnimation(Animation.ABSOLUTE,imagePos[0],Animation.ABSOLUTE,buttonPos[0],Animation.ABSOLUTE,imagePos[1],Animation.ABSOLUTE,buttonPos[1]);
But it is not working. The translations are not seen on screen. Any help would be appreciated a lot!! Thankyou in advance.

First, just in case you are not getting the right coordinates from getLocationInWindow, try getting the coordinates like this:
ImageButton.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
ImageButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int[] locations = new int[2];
ImageButton.getLocationOnScreen(locations);
// x is locations[0];
// y is locations[1];
}
});

You did not set the duration and not even started the animation.
move.setDuration(1000);
imageView.startAnimation(move);
And then try to play with the attributes of animation to get the desired result

Related

How to stop imagebutton at current position when clicked during animation?

I have an ImageButton which moves down from the top of the screen. The image animates while moving down to the screen. I want to make it stop WHEREVER and WHENEVER it is clicked on. Basically if the image is clicked in the middle of the animation, I want to it to stop at that current position. My progress can be seen in the code block below, the code makes the image animate and move from top to bottom, when the image is clicked, the image moves straight to the desired position (500), instead of stopping at the current position. Please be really descriptive as I am a beginner in programming. Thank you.
final ImageButton image = (ImageButton)findViewById(R.id.image);
final ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(image, View.TRANSLATION_Y, 0, 500); //where the image should move to
objectAnimator.setStartDelay(2000); //how long to wait before starting
objectAnimator.setDuration(2000); //how long animation lasts
objectAnimator.start();
image.setOnClickListener(
new ImageButton.OnClickListener() {
public void onClick(View v) {
image.setTranslationY(image.getY()); //gets current position of Y?
objectAnimator.end(); //ends the animation
}
}
);
From the documentation of ValueAnimator#cancel it seems that
objectAnimator.cancel();
should solve your problem.

Move ImageView with animation

I'm trying to move an ImageView with this code:
img.animate().translationY(110).setDuration(1500);
But if I try to move it again later with something like:
img.animate().translationY(-110).setDuration(1500);
The ImageView moves from where it used to be before I moved it in the first place.
What I would expect is for it to return to it's original position. What am I missing here?
Try this
img.animate().translationY(0).setDuration(1500);
you should use animationSet and also -
animationSet.setFillAfter(true);
Good luck.
Maybe you could just get the translation and add it?
public void ClickMoveDown{
float distance = 100;
Move (0,distance)
}
private void Move (float x, float y){
img.animate().translationX(img.getTranslationX+x).setDuration(1500);
img.animate().translationY(img.getTranslationY+y).setDuration(1500);
}
you have to move it after (in your case) 1.5 seconds is passed
float originalPosition = img.getY();
img.animate().translationY(110).setDuration(1500).start();
img.animate().translationY(originalPosition).setStartDelay(1500).setDuration(1500).start();
im not so sure about the method names..
TranslateAnimation animationDown = new TranslateAnimation(00.0f, 00.0f, 00.0f, 500);
animationDown.setDuration(800);
animationDown.setRepeatMode(android.view.animation.Animation.REVERSE);
animationDown.setRepeatCount(android.view.animation.Animation.INFINITE);
This will make the image move straight down then reverse its track back up to its original spot maintaining speed. Also take note you can make multiple of these and add them to one animation set to get the desired square pattern i believe how you would store it into a set is as such...
AnimationSet example = new AnimationSet(true);
example.addAnimation(animationDown);
"" and so on until
s.setStartTime();
or
s.startAnimationSet();

Multiple animations within a relative view

I am trying to animate a pointer around the screen in an android application.
I am using an imageview as the pointer inside a relative layout as follows:
final RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
pointer = (ImageView) findViewById(R.id.pointer);
pointer.animate().setDuration(2000);
I then want to move the pointer up 10 pixels every time I click a button:
// ONCLICK LISTENER FOR LEFT BUTTON
btnUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//get positon of pointer
leftPoint = pointer.getLeft();
topPoint = pointer.getTop();
int xValue = container.getWidth() - pointer.getWidth();
int yValue = container.getHeight() - pointer.getHeight();
pointer.animate().x(leftPoint).y(topPoint-10);
}
});
This works the first time I click the button but will not move it any subsequent times. I have tried making the int points static but this did not help.
Any help greatly appreciated.
This might sound counter-intuitive, but animating a view doesn't change its location. Your code is demonstrating it. When you do this line:
topPoint = pointer.getTop();
You get the same value every time.
You need to change pointer's position at the end of the animation.
This answer shows how someone else has done it.
Other Considerations
You could use object animator, if you are only targeting SDK 11 and above. Here is a little intro to it.
If you want to interrupt your animation, you can check its status, and find out its current y offset. You can add that to the position of the view before starting the animation. Here is an example.

Animate an ImageView by saving starting and ending position

I have an ImageView which can be moved by finger with a touchListener. I want the user to save two positions of the Imageview by pressing a button and after that, when he press to play the animation, to start from the first position and finish to the second.
How this can be possible? I try by saving in an array the two position by using getX and getY and after put them in a TranslateAnimation, but it doesn't work
public void get1Cos(){
x[1] = player.getX();
y[1] = player.getY();
}
public void get2Cos(){
x[6] = player.getX();
y[6] = player.getY();
}
anim = new TranslateAnimation(x[1], x[6], y[1],y[6]);
anim.setFillAfter(true);
anim.setDuration(2000);
player.startAnimation(anim);
Try this:
player.startAnimation(animation);

why MapView.LayoutParams MODE_VIEW does not work if you add x and y coordinates?

when i added x and y coordinates to MapView.LayoutParams constructor. its MODE_VIEW behaviour disappears. I want this behaviour along with x and y coordinates. I am not getting the problem why this is happening.
public void addMarkerToMAp(GeoPoint geoPoint) {
mapView.removeAllViews();
final ImageView view = new ImageView(mapView.getContext());
view.setImageResource(R.drawable.map_marker_anim);
view.post(new Runnable() {
#Override
public void run() {
AnimationDrawable animationDrawable = (AnimationDrawable) view.getDrawable();
animationDrawable.start();
}
});
mapView.addView(view);
MapView.LayoutParams layoutParams = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT,
MapView.LayoutParams.WRAP_CONTENT,
geoPoint,
MapView.LayoutParams.MODE_VIEW);
view.setLayoutParams(layoutParams);
}
So, I got the geoPoint as
geoPoint = mapView.getProjection().fromPixels((int)event.getX(), (int)event.getY());
where event is click event.
Also I want to show marker pin at (event.getX(), event.getY()), but it shows below the click.
If you code in eclipse, press ctrl+shift+f. That will make your code more readable.
That being said, When you show a marker, which points do you use?
If im not wrong then the point you will insert, will be the bottom left if its an overlay drawable.
topleft if its a view(i think thats what you are doing) and therefor it will appear under your click.
You can add an offsetx and offsety to it being -view.getWidth()/2 and -view.getHeight/2. which should then make your view directly centered under your pressed point.
You might have trouble getting view.getWidth/Height though, so make sure it doesnt return 0.

Categories

Resources