我只想使用动画在Ludo board中从下到上、从左到右翻译图像( token )。为此,我获取了每个 ImageView 的坐标,每个标记(红色、蓝色、黄色、绿色)都会移动,但现在我被卡住了;接下来我应该做什么?我是 Android 动画新手。我怎样才能做到这一点?
这是我的数组索引代码:
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.start );
double aPowersOfTwo[] = new double[60];
aPowersOfTwo[0]=Math.pow(81,263);
aPowersOfTwo[1]=Math.pow(50,262);
aPowersOfTwo[2]=Math.pow(50,231);
aPowersOfTwo[3]=Math.pow(80,227);
}
我的黄色 token 的java代码是
switch(rand.nextInt(6) + 1)
{
case 1:
diceImage.setImageResource(R.drawable.dice_one);
yellowToken.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.yellow_token9:
Animation animation = new TranslateAnimation(0,80,0,48);
animation.setDuration(1500);
animation.setFillAfter(true);
yellowToken.startAnimation(animation);
yellowToken.setVisibility(0);
break;
}
}});
case 2:
diceImage.setImageResource(R.drawable.dice_two);
Log.d("dice", "2");
break;
case 3:
diceImage.setImageResource(R.drawable.dice_three);
Log.d("dice", "3");
break;
case 4:
diceImage.setImageResource(R.drawable.dice_four);
Log.d("dice", "4");
break;
case 5:
diceImage.setImageResource(R.drawable.dice_five);
Log.d("dice", "5");
break;
case 6:
diceImage.setImageResource(R.drawable.dice_six);
yellowToken.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.yellow_token9:
Animation animation = new TranslateAnimation(0,80,0,48);
animation.setDuration(1500);
animation.setFillAfter(true);
yellowToken.startAnimation(animation);
yellowToken.setVisibility(0);
break;
}
}});
我的 xml 代码是
<ImageView
android:id="@+id/yellow_token9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="222dp"
android:clickable="true"
android:background="@drawable/yellow"/>
最佳答案
当您将所有坐标存储在数组中但您使用了错误的方法Math.pow(x,y)时,它用于x的y次方。因此您应该使用二维数组来存储所有坐标。完成后这个你可以用这个概念来开发这个游戏-
示例-
如果骰子显示“1”,您应该将索引增加“1”,并将坐标传递到您的
TranslateAnimation() 方法。
然后根据索引号将它们移动到正确的位置。
关于java - 如何在 Ludo 棋盘中使用数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16407281/