如何在更改动画android时更改视图位置

时间:2014-09-29 02:54:16

标签: android animation view position

我希望在使用动画时获得视图的位置。检查我使用view.getBottom()的位置;而是显示不同的位置,它显示一个恒定的值。我想知道如何在使用动画时更改视图的位置。我在android中从上到下移动视图。

move.xml    
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
   <!--  android:repeatCount="infinite" --> 
<translate
    android:fromYDelta="0%p"
    android:toYDelta="90%p"
    android:duration="800"
  />
</set>



onCreate(){


anim = AnimationUtils.loadAnimation(getApplicationContext(), R.drawable.move);                   

anim.setAnimationListener(new AnimationListener() { 
        int t=0,b=0,k=0; `

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub
            bullet[0].layout(0, 0, bullet[0].getWidth(), bullet[0].getBottom());
            bullet[0].startAnimation(animation);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub
            t=t+k;
            b=b+k;
            bullet[0].layout(0, t+bullet[0].getHeight(), bullet[0].getWidth(), b+bullet[0].getHeight());
            bullet[0].startAnimation(animation);
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub
            k=-k;
            onAnimationRepeat(animation);
        }
    });
    bullet[0].startAnimation(anim);

2 个答案:

答案 0 :(得分:0)

您可以使用像此一样的getLocation方法获取任何视图的位置

int location[] = new int[2];
view.getLocationOnScreen(location);

System.out.println("x="+location[0] + "y=" + location[1]);

此处视图可以是按钮或imageview任何内容..基于此,您可以更改视图的位置..

编辑1: 你可以制作你的自定义翻译动画类...并使用一个回调监听器来获取坐标...从CustomTranslate动画类的应用转换方法调用监听器的方法。只需使用与TranslateAnimation.java中相同的代码.. < / p>

public class CustomTranslateAnimation extends Animation {

private int mFromXType = ABSOLUTE;
private int mToXType = ABSOLUTE;

private int mFromYType = ABSOLUTE;
private int mToYType = ABSOLUTE;

private float mFromXValue = 0.0f;
private float mToXValue = 0.0f;

private float mFromYValue = 0.0f;
private float mToYValue = 0.0f;

private float mFromXDelta;
private float mToXDelta;
private float mFromYDelta;
private float mToYDelta;
private Context mContext;

/**
 * Constructor used when a TranslateAnimation is loaded from a resource.
 * 
 * @param context
 *            Application context to use
 * @param attrs
 *            Attribute set from which to read values
 */
public CustomTranslateAnimation(Context context, AttributeSet attrs) {
    super(context, attrs);

    // TypedArray a = context.obtainStyledAttributes(attrs,
    // com.android.internal.R.styleable.TranslateAnimation);
    //
    // Description d = Description.parseValue(a.peekValue(
    // com.android.internal.R.styleable.TranslateAnimation_fromXDelta));
    // mFromXType = d.type;
    // mFromXValue = d.value;
    //
    // d = Description.parseValue(a.peekValue(
    // com.android.internal.R.styleable.TranslateAnimation_toXDelta));
    // mToXType = d.type;
    // mToXValue = d.value;
    //
    // d = Description.parseValue(a.peekValue(
    // com.android.internal.R.styleable.TranslateAnimation_fromYDelta));
    // mFromYType = d.type;
    // mFromYValue = d.value;
    //
    // d = Description.parseValue(a.peekValue(
    // com.android.internal.R.styleable.TranslateAnimation_toYDelta));
    // mToYType = d.type;
    // mToYValue = d.value;
    //
    // a.recycle();
}

/**
 * Constructor to use when building a TranslateAnimation from code
 * 
 * @param fromXDelta
 *            Change in X coordinate to apply at the start of the animation
 * @param toXDelta
 *            Change in X coordinate to apply at the end of the animation
 * @param fromYDelta
 *            Change in Y coordinate to apply at the start of the animation
 * @param toYDelta
 *            Change in Y coordinate to apply at the end of the animation
 */
public CustomTranslateAnimation(float fromXDelta, float toXDelta,
        float fromYDelta, float toYDelta, Context context) {
    mFromXValue = fromXDelta;
    mToXValue = toXDelta;
    mFromYValue = fromYDelta;
    mToYValue = toYDelta;
    mContext = context;

    mFromXType = ABSOLUTE;
    mToXType = ABSOLUTE;
    mFromYType = ABSOLUTE;
    mToYType = ABSOLUTE;
}

/**
 * Constructor to use when building a TranslateAnimation from code
 * 
 * @param fromXType
 *            Specifies how fromXValue should be interpreted. One of
 *            Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
 *            Animation.RELATIVE_TO_PARENT.
 * @param fromXValue
 *            Change in X coordinate to apply at the start of the animation.
 *            This value can either be an absolute number if fromXType is
 *            ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
 * @param toXType
 *            Specifies how toXValue should be interpreted. One of
 *            Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
 *            Animation.RELATIVE_TO_PARENT.
 * @param toXValue
 *            Change in X coordinate to apply at the end of the animation.
 *            This value can either be an absolute number if toXType is
 *            ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
 * @param fromYType
 *            Specifies how fromYValue should be interpreted. One of
 *            Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
 *            Animation.RELATIVE_TO_PARENT.
 * @param fromYValue
 *            Change in Y coordinate to apply at the start of the animation.
 *            This value can either be an absolute number if fromYType is
 *            ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
 * @param toYType
 *            Specifies how toYValue should be interpreted. One of
 *            Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
 *            Animation.RELATIVE_TO_PARENT.
 * @param toYValue
 *            Change in Y coordinate to apply at the end of the animation.
 *            This value can either be an absolute number if toYType is
 *            ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
 */
public CustomTranslateAnimation(int fromXType, float fromXValue,
        int toXType, float toXValue, int fromYType, float fromYValue,
        int toYType, float toYValue) {

    mFromXValue = fromXValue;
    mToXValue = toXValue;
    mFromYValue = fromYValue;
    mToYValue = toYValue;

    mFromXType = fromXType;
    mToXType = toXType;
    mFromYType = fromYType;
    mToYType = toYType;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    float dx = mFromXDelta;
    float dy = mFromYDelta;
    if (mFromXDelta != mToXDelta) {
        dx = mFromXDelta + ((mToXDelta - mFromXDelta) * interpolatedTime);
    }
    if (mFromYDelta != mToYDelta) {
        dy = mFromYDelta + ((mToYDelta - mFromYDelta) * interpolatedTime);
    }
    t.getMatrix().setTranslate(dx, dy);

    ((CheckCollosion) mContext).check(dx, dy);

}

@Override
public void initialize(int width, int height, int parentWidth,
        int parentHeight) {
    super.initialize(width, height, parentWidth, parentHeight);
    mFromXDelta = resolveSize(mFromXType, mFromXValue, width, parentWidth);
    mToXDelta = resolveSize(mToXType, mToXValue, width, parentWidth);
    mFromYDelta = resolveSize(mFromYType, mFromYValue, height, parentHeight);
    mToYDelta = resolveSize(mToYType, mToYValue, height, parentHeight);
}

}

编辑2:

此链接将指导您Moving the image according to the Button click

逐步移动视图..

答案 1 :(得分:0)

我使用ValueAnimator

解决了这个问题
    va=ValueAnimator.ofFloat(0.0f,size.y);
    va.setDuration(5000);
    va.setRepeatCount(va.INFINITE);
    va.setRepeatMode(va.REVERSE);
    va.start();
    va.addUpdateListener(new AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            // TODO Auto-generated method stub
                    bullet[0].setTranslationY((Float) va.getAnimatedValue());           
                       Rect R11=new Rect(bullet[0].getLeft(),bullet[0].getTop()+(int)bullet[0].getTranslationY(),bullet[0].getRight(),bullet[0].getBottom()+(int)bullet[0].getTranslationY());
                          Rect R21=new Rect(ball.getLeft(), ball.getTop(), ball.getRight(), ball.getBottom());
                          if(R11.intersect(R21))
                            va.cancel();
        }
    });