2013-10-23 116 views
3

我正在開發Android onlineShopping應用程序。我必須應用一些動畫。將觸摸位置的圖像圖標設置爲右上角?

  1. 購物車圖像顯示在屏幕的右上角。
  2. 項目列表在屏幕上的每個項目與「添加到購物車」按鈕。
  3. 當用戶按此按鈕我必須播放動畫。
  4. 我有一個修復圖像,應該從觸摸位置動畫到放置在屏幕右上角的購物車圖像。

請幫我一把。

在此先感謝。

更新:

我試過這個將圖像從一個地方移動到另一個地方。

TranslateAnimation anim = new TranslateAnimation(0,0,200,200);    
       anim.setDuration(3000); 

       img.startAnimation(anim); 

此圖像我想從觸摸位置到右上角的動畫。 enter image description here

+0

@ Indiandroid我從來沒有與動畫作品。我只看到一個演示,但沒有滿足要求。 –

+0

正如你所說的你想從列表視圖項目到右上角做動畫?如果可能的話,然後添加屏幕截圖。 – TheFlash

+0

@ Indiandroid yup bro請幫我一把。無法從動畫演示代碼片斷中找出。 –

回答

5

最終你想從一個位置移動以動畫的另一個位置。

步驟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(); 

我希望這會有所幫助! !

+0

感謝您的好回答聽起來很有希望。將嘗試並更新你與此代碼發生的事情。 –

1

我想你究竟在尋找,你可以檢查鏈接

link here

enter image description here