3

我知道有關於這個主題的問題,但沒有幫助我。 我正在做的紙牌遊戲(通常稱爲濃度記憶遊戲),而當兩個相同的卡都選擇我希望他們去使用TranslateAnimation頂部。問題是,在其他佈局和視圖的意見去...Android翻譯動畫根據佈局和意見

這裏的功能:

private void matchAnimation(int card1ID, int card2ID ,boolean isPlayer1) { 

    getNameLocations(); 
    int x; 
    int[] card1Loc = new int[2]; 
    int[] card2Loc = new int[2]; 
    ImageView card1 = (ImageView)findViewById(card1ID); 
    ImageView card2 = (ImageView)findViewById(card2ID); 

    if (player) 
     x = playerOneLocation[0]; 
    else 
     x = playerTwoLocation[0]; 

    card1.getLocationOnScreen(card1Loc); 
    card2.getLocationOnScreen(card2Loc); 

    Animation animationMove = 
     new TranslateAnimation(0, x-card1Loc[0], 0, 
          playerOneLocation[1]-card1Loc[1]); 
    animationMove.setZAdjustment(Animation.ZORDER_TOP); 
    animationMove.setDuration(1000); 
    animationMove.setFillAfter(true); 
    card1.startAnimation(animationMove); 
    card2.startAnimation(animationMove); 
    card1.setVisibility(View.VISIBLE); 
} 

和這裏的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#BDE1FF" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".GamePlay" > 

<TableLayout 
    android:id="@+id/tableLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="@dimen/activity_vertical_margin" 
    android:layout_centerHorizontal="true" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp" > 

     <ImageView 
      android:id="@+id/c1" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_1" /> 

     <ImageView 
      android:id="@+id/c2" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_2" 
      /> 

     <ImageView 
      android:id="@+id/c3" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_3" 
      /> 

     <ImageView 
      android:id="@+id/c4" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_4" 
      /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp"> 

     <ImageView 
      android:id="@+id/c5" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_5" 
      /> 

     <ImageView 
      android:id="@+id/c6" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_6" 
      /> 

     <ImageView 
      android:id="@+id/c7" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_7" 
      /> 

     <ImageView 
      android:id="@+id/c8" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_8" 
      /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp"> 

     <ImageView 
      android:id="@+id/c9" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_9" 
      /> 

     <ImageView 
      android:id="@+id/c10" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_10" 
      /> 

     <ImageView 
      android:id="@+id/c11" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_11" /> 

     <ImageView 
      android:id="@+id/c12" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_12" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp" > 

     <ImageView 
      android:id="@+id/c13" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_13" 
      /> 

     <ImageView 
      android:id="@+id/c14" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_14" /> 

     <ImageView 
      android:id="@+id/c15" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_15" /> 

     <ImageView 
      android:id="@+id/c16" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_16" 
      /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp" > 

     <ImageView 
      android:id="@+id/c17" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_17" /> 

     <ImageView 
      android:id="@+id/c18" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_18" /> 

     <ImageView 
      android:id="@+id/c19" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_19" /> 

     <ImageView 
      android:id="@+id/c20" 
      android:onClick="cardPicked" 
      android:src="@drawable/icon" 
      android:layout_marginLeft="4dp" 
      android:layout_marginRight="4dp" 
      android:contentDescription="@string/card_20" /> 
    </TableRow> 


</TableLayout> 


    <TextView 
     android:id="@+id/playername1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="4dp" 
     android:layout_marginRight="12dp" 
     android:text="@string/placeholder" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/playername2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="4dp" 
     android:layout_marginLeft="12dp" 
     android:text="@string/placeholder" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/playerscore2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/playerscore1" 
     android:layout_alignBottom="@+id/playerscore1" 
     android:layout_alignLeft="@+id/tableLayout1" 
     android:text="@string/initial_score" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textStyle="bold" /> 

    <ImageView 
     android:id="@+id/pointer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/playername1" 
     android:layout_centerHorizontal="true" 
     android:contentDescription="@string/smurf_pointing" 
     android:src="@drawable/smurf" /> 

    <TextView 
     android:id="@+id/playerscore1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/tableLayout1" 
     android:layout_below="@+id/playername1" 
     android:text="@string/initial_score" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textStyle="bold" /> 

希望有人能夠幫助!謝謝。

回答

2

android:clipChildren="false"設置爲所有容器佈局。

另請參閱我有的問題:Animation: Move TextView into another container

但是,我注意到這導致Android版本2.3中的佈局有問題。 所以我現在要做的是在所有動畫發生的地方創建一個自己的ViewOverlay。

取決於你的目標是支持什麼版本的,也有View.getViewOverlay()

+0

謝謝你救了我的理智。 :| – Aphex