最終你想從一個位置移動以動畫的另一個位置。
步驟1: 獲取視圖的初始位置
int fromLoc[] = new int[2];
v.getLocationOnScreen(fromLoc);
float startX = fromLoc[0];
float startY = fromLoc[1];
步驟2: 獲得目標位置
int toLoc[] = new int[2];
desti.getLocationOnScreen(toLoc);
float destX = toLoc[0];
float destY = toLoc[1];
步驟3: 創建一個類來管理動畫
public class Animations {
public Animation fromAtoB(float fromX, float fromY, float toX, float toY, AnimationListener l, int speed){
Animation fromAtoB = new TranslateAnimation(
Animation.ABSOLUTE, //from xType
fromX,
Animation.ABSOLUTE, //to xType
toX,
Animation.ABSOLUTE, //from yType
fromY,
Animation.ABSOLUTE, //to yType
toY
);
fromAtoB.setDuration(speed);
fromAtoB.setInterpolator(new AnticipateOvershootInterpolator(1.0f));
if(l != null)
fromAtoB.setAnimationListener(l);
return fromAtoB;
}
}
第4步:添加 和animationlistener所需視圖啓動動畫
AnimationListener animL = new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
//this is just a method call you can create to delete the animated view or hide it until you need it again.
clearAnimation();
}
};
//現在如下所述啓動動畫:
Animations anim = new Animations();
Animation a = anim.fromAtoB(startX, startY, destX, destY, animL,850);
v.setAnimation(a);
a.startNow();
我希望這會有所幫助! !
@ Indiandroid我從來沒有與動畫作品。我只看到一個演示,但沒有滿足要求。 –
正如你所說的你想從列表視圖項目到右上角做動畫?如果可能的話,然後添加屏幕截圖。 – TheFlash
@ Indiandroid yup bro請幫我一把。無法從動畫演示代碼片斷中找出。 –