2014-09-29 117 views
0

我想在使用動畫時獲得視圖的位置。檢查我使用view.getBottom()的位置;而是顯示不同的位置,它顯示一個恆定的值。我想知道如何在使用動畫時改變視圖的位置。我在android中從上到下移動視圖。如何更改動畫時更改視圖位置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); 

回答

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(); 
     } 
    }); 
0

您可以通過使用這樣的

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

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

這裏景色的getLocation方法可以是按鈕或imageview的anything..based在此你可以改變的視圖位置得到任何視圖的位置..

編輯1: 你可以讓你自定義的翻譯動畫class..and使用回調偵聽器來獲取座標...叫聽者從應用自定義的轉換方法的方法翻譯動畫class..just使用相同的代碼作爲TranslateAnimation.java ..

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

移動一步一步查看..

+0

感謝您的回覆。我必須檢查兩個視圖是否相互碰撞。因此,如果我使用動畫視圖,那麼它會向用戶產生幻覺,它正在移動,但在動畫中,實際視圖不會不動。所以我必須將視圖從上到下移動,以便我們檢查它是否與其他視圖相沖突?你能告訴我怎麼可能?感謝 – Ajay 2014-09-29 09:24:10

+0

使CustomTranslateAnimation類......並從其applytransformation方法得到一個回電話給你的活動..你會得到你的翻譯視圖的新的x,y位置。 – Meenal 2014-09-29 10:09:59

+0

我怎樣才能移動我的看法,從頂部到底部沒有使用動畫有什麼辦法呢? – Ajay 2014-09-30 06:09:24