您可以通過使用這樣的
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
移動一步一步查看..
感謝您的回覆。我必須檢查兩個視圖是否相互碰撞。因此,如果我使用動畫視圖,那麼它會向用戶產生幻覺,它正在移動,但在動畫中,實際視圖不會不動。所以我必須將視圖從上到下移動,以便我們檢查它是否與其他視圖相沖突?你能告訴我怎麼可能?感謝 – Ajay 2014-09-29 09:24:10
使CustomTranslateAnimation類......並從其applytransformation方法得到一個回電話給你的活動..你會得到你的翻譯視圖的新的x,y位置。 – Meenal 2014-09-29 10:09:59
我怎樣才能移動我的看法,從頂部到底部沒有使用動畫有什麼辦法呢? – Ajay 2014-09-30 06:09:24