2015-01-01 32 views
2

我已經寫了一個類似國際象棋的益智遊戲,大部分完成。我現在想要做的是增加一些圖形效果,以改善我的遊戲的感覺和外觀,從改善棋子的移動開始。在Android遊戲中滑動棋子

我已經構建我的主要遊戲棋盤,棋盤,爲TextViews一個GridLayout,使一個棋子被表示爲一個國際象棋字體字母(和正方形是由呼叫setBackgroundResource黑色或白色TextView)。每個TextView都有一個onClickListener,因此當用戶點擊適當的方塊時,所討論的TextViews上的字母會根據國際象棋規則進行更改。這意味着圖形感覺有些不自然:被移動的棋子從其原始方塊中消失,並立即再次出現在移動到的方塊上。我想通過讓棋子從原來的方塊滑到它的新廣場來改變這一點。有什麼辦法可以在保持GridLayout-of-TextViews邏輯的同時做到這一點 - 還是我需要大幅改變代碼結構?無論如何,如果有人知道如何開始這個,我很樂意聽到它!

回答

-1

嘗試這樣的事情

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    txtOne = (TextView)findViewById(R.id.textone); 
    txtTwo = (TextView)findViewById(R.id.text_two); 
    relative = (RelativeLayout)findViewById(R.id.relative); 


    txtTwo.setOnClickListener(new View.OnClickListener() { 

     @SuppressLint("NewApi") 
     @Override 
     public void onClick(View v) { 



      float to_x = txtOne.getX(); 
      float to_y = txtOne.getY(); 


      float x_from = txtTwo.getX(); 
      float y_from = txtTwo.getY(); 
      //txtTwo.getLocationInWindow(fromLoc); 
      txtOne.getLocationOnScreen(toLoc); 
      Animations anim = new Animations(); 

      Animation a = anim.fromAtoB(x_from, y_from, -to_x, -to_y, animL,1000); 
      txtTwo.startAnimation(a); 

     } 
    }); 
} 



public class Animations { 
    public Animation fromAtoB(float fromX, float fromY, float toX, float toY, AnimationListener l, int speed){ 


     Animation fromAtoB = new TranslateAnimation(

       fromX, 

       toX, 

       fromY, 

       toY 
       ); 

     fromAtoB.setDuration(speed); 
     //fromAtoB.setInterpolator(new); 


     if(l != null) 
      fromAtoB.setAnimationListener(l);    
     return fromAtoB; 
    } 
} 



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 to delete the ImageView and hide the animation Layout until we need it again. 
     //clearAnimation();  
    } 
}; 
+0

舉例摘自:http://stackoverflow.com/questions/18029097/android-cant-get-one-textview-to-animate-to-the-position- of-another-textview – kinezu

+0

謝謝。現在我正試圖理解這段代碼。什麼變量是「toLoc」? – Sid

+0

這將是你的文字視圖,你正在移動你的作品。 – kinezu