使用图像视图转换动画无法正确定位

时间:2013-04-25 09:25:03

标签: android imageview position translate-animation

我的代码中几乎没有问题:

1.我使用getX(),getY()来获取相对布局中imageview的位置。这是获得职位的正确方法吗?

2.Translate Animate不会以精确位置停止。(即)它穿过图像视图(目的地)

3.如果动画结束到特定位置,我想将背景图像设置到此位置 我怎么能实现那个目标呢?

  1. 连续动画中的逻辑是错误的吗?我用在动画结束方法.. 请提供您的建议。 我只是一个初学者。这是一个我正在开发的小游戏。
  2. 这是我的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>
    

    My.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();
    
        }
        }
    

0 个答案:

没有答案