Android游戏中的滑动棋子
我已经用Android写了一个象棋一样的益智游戏,基本上已经完成了。我现在想做的是添加一些图形效果,以改善我的游戏的感觉和外观,从改善棋子的运动开始Android游戏中的滑动棋子,android,textview,android-gridlayout,Android,Textview,Android Gridlayout,我已经用Android写了一个象棋一样的益智游戏,基本上已经完成了。我现在想做的是添加一些图形效果,以改善我的游戏的感觉和外观,从改善棋子的运动开始 我已经将我的主游戏板chess board构建为TextView的GridLayout,这样一个棋子就可以用一个象棋字体的字母来表示(通过调用TextView上的setBackgroundResource,一个正方形被涂成黑色或白色)。每个TextView都有一个onClickListener,因此当用户单击相应的方块时,相关TextView上的字
我已经将我的主游戏板chess board构建为
TextView
的GridLayout
,这样一个棋子就可以用一个象棋字体的字母来表示(通过调用TextView
上的setBackgroundResource
,一个正方形被涂成黑色或白色)。每个TextView
都有一个onClickListener
,因此当用户单击相应的方块时,相关TextView
上的字母将根据国际象棋规则进行更改。这意味着图形感觉有些不自然:移动的棋子从其原始方块消失,并立即在其移动到的方块上重新出现。我想改变这一点,让棋子从原来的方块滑到新的方块上。在维护TextViews逻辑的GridLayout的同时,是否有任何方法可以做到这一点?或者我是否需要大幅更改代码的结构?在任何情况下,如果有人知道如何开始,我很乐意听到它 试试这样的东西
@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();
}
};
试试这样的
@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();
}
};
例如:谢谢。现在我正在努力理解这段代码。什么变量是“toLoc”?这将是你的文本视图,你要将你的作品移动到它。好的,但是我们如何使用它呢?在上述代码中调用变量的唯一位置是在
txtOne.getLocationOnScreen(toLoc)行中代码>返回一个位置,但我们似乎没有对它做任何处理。或者我误解了什么?我还想知道:如果我正确理解了代码块,我们在这里做的是将TextView
从一个位置移动到另一个位置,而不是TextView
中包含的文本。这意味着整个方格都被移动了,但方格中的棋子却没有移动。这是正确的吗?例子取自:谢谢。现在我正在努力理解这段代码。什么变量是“toLoc”?这将是你的文本视图,你要将你的作品移动到它。好的,但是我们如何使用它呢?在上述代码中调用变量的唯一位置是在txtOne.getLocationOnScreen(toLoc)行中代码>返回一个位置,但我们似乎没有对它做任何处理。或者我误解了什么?我还想知道:如果我正确理解了代码块,我们在这里做的是将TextView
从一个位置移动到另一个位置,而不是TextView
中包含的文本。这意味着整个方格都被移动了,但方格中的棋子却没有移动。这是正确的吗?