微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android翻译动画位于布局和视图下

我知道有人对此主题有疑问,但没有帮助我.
我正在制作纸牌游戏(通常称为集中或记忆游戏),每当选择两张相同的纸牌时,我都希望它们使用TranslateAnimation到达顶部.问题是,视图位于其他布局和视图下…

这是函数

    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" />

希望有人能帮忙!谢谢.

解决方法:

将android:clipChildren =“ false”设置为所有容器布局.

另请参阅我的问题:Animation: Move TextView into another container.

但是,我注意到这导致Android 2.3版中的布局出现问题.
所以我现在想做的是在所有动画发生的位置创建一个自己的ViewOverlay.

根据您要支持的版本,还有一个View.getViewOverlay()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐