0

I'm basically programming a D-Pad for a video game, and I'm trying to figure out how to make an ImageView move at a constant X or Y, then keep that position after the button is released.

Any help or questions about what I'm doing specifically would be very much appreciated.

  • ObjectAnimator animator = ObjectAnimator.ofInt(myView, "x", myView.getX()+5); animator.start(); – raj Jan 06 '16 at 09:50

3 Answers3

0

You can use an ObjectAnimator. To slide your view (myView) 5 pixels over:

ObjectAnimator animator = ObjectAnimator.ofInt(myView, "x", myView.getX()+5);
animator.start();
raj
  • 2,088
  • 14
  • 23
  • I've been trying with animation for some time now, but I just find that the position resets to the original parameters when I click another directional button, not saving any of the changes. Will this change the parameters themselves? – Ian Mazgelis Jan 06 '16 at 09:58
0

You can use ObjectAnimator.ofFloat(Object target, String property, float values...)

Set it a LinearInterpomator so that the animation is constant in its progress.

Example :

ObjectAnimator animator = ObjectAnimator.ofFloat(myImage, "X", 0f, 150f);

Set interpolator : animator.setInterpolator(new LinearInterpolator());

A duration in milliseconds : animator.setDuration(5000); //5 seconds

Then start it.

It will move your image from 0 on the X axis to 150. The String property refers to the object property on which you want to apply the new coordinates value.

I hope it helped.

Note : you may change the from and to values so that the from value correspond to the actual position of the image and so that the destination coordinate is actually the new position would want.

There are other solutions such as KitKat TransitionManaqger but should you want to target devices before KitKat you may use ObjectAnimator.

Mackovich
  • 3,319
  • 6
  • 35
  • 73
0

create slideanimation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">

<translate
    android:duration="1500"
    android:fromXDelta="-100%"
    android:fromYDelta="0%"
    android:repeatMode="reverse"
    android:toXDelta="0%"
    android:toYDelta="0%" />

</set>

In Code

image = (ImageView) findViewById(R.id.imageLady);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_right);
image.startAnimation(animation);
  • That's very similar to what I have right now, but would making shareInterlopator false somehow prevent the position from resetting? – Ian Mazgelis Jan 06 '16 at 10:04