我的代码中有几个问题:
1.我使用 getX(), getY() 来获取 imageview 在相对布局中的位置。获得职位的方法是否正确?
2.Translate Animate 不会以确切的位置停止。(即)它穿过图像视图(目的地)
3.如果动画结束到特定位置,我想将背景图像设置到该位置,我该如何实现?
- 连续动画中的逻辑错误吗?我在动画结束方法中使用..请提供您的建议。我只是一个初学者。这是我正在开发的一个小游戏。
这是我的 xml 和 java 代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<ImageView
android:id="@+id/player4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/player6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:layout_toLeftOf="@+id/player5"
android:visibility="invisible"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/player5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="14dp"
android:layout_marginRight="89dp"
android:layout_toLeftOf="@+id/player4"
android:visibility="invisible"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/player3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/player5"
android:layout_marginRight="26dp"
android:layout_toLeftOf="@+id/player2"
android:visibility="invisible"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/player2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="invisible"
android:layout_marginRight="16dp"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/player1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/player2"
android:layout_alignParentTop="true"
android:visibility="invisible"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/player7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/player1"
android:layout_alignLeft="@+id/player6"
android:visibility="invisible"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/dealer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/player4"
android:layout_alignParentTop="true"
android:visibility="invisible"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
我的.java 文件
//getting the imageviews from layout
player1=(ImageView)findViewById(R.id.player1);
player2=(ImageView)findViewById(R.id.player2);
player3=(ImageView)findViewById(R.id.player3);
player4=(ImageView)findViewById(R.id.player4);
player5=(ImageView)findViewById(R.id.player5);
player6=(ImageView)findViewById(R.id.player6);
player7=(ImageView)findViewById(R.id.player7);
dealer=(ImageView)findViewById(R.id.dealer);
private void Logic() {
// TODO Auto-generated method stub
if(players ==1)animation = new TranslateAnimation(0,addX.get(0),0,addY.get(0));
if(players ==2)animation = new TranslateAnimation(0,addX.get(1),0, addY.get(1));
if(players ==3)animation = new TranslateAnimation(0,addX.get(2),0, addY.get(2));
if(players ==4)animation = new TranslateAnimation(0,addX.get(3),0, addY.get(3));
if(players ==5)animation = new TranslateAnimation(0,addX.get(4),0, addY.get(4));
if(players ==6)animation = new TranslateAnimation(0,addX.get(5),0, addY.get(5));
if(players ==7)animation = new TranslateAnimation(0,addX.get(6),0, addY.get(6));
}
//Method for Start Animation
private void doAnimation() {
// TODO Auto-generated method stub
animation.setDuration(1000);
animation.setFillAfter( true );
animation.setAnimationListener(this);
dealer.startAnimation(animation);
}
//IN Animation END
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
dealer.clearAnimation();
dealer.setImageResource(R.drawable.singlecard);
players=players+1;
dealer.clearAnimation();
if(players<=count){
Logic();
doAnimation();
}
}