错误或遗失某些内容?

时间:2015-11-12 03:44:55

标签: android animation flip playing-cards

我编写了一个应用程序,可以翻转卡片以显示卡片的正面和背面。在这个应用程序中,我有12张卡。我不明白的是,当我点击任何一张卡,然后点击另一张卡后,下面的卡会显示前卡两次,然后在点击时显示卡的背面。例如我点击imgFront,然后我继续点击imageView1(前面),右边它应该显示imageView2(后面),但它显示imageView1(前面)。如果我在应用程序中只有一张卡,它将按预期工作。例如。我点击imgFront,它会显示imgBack,反之亦然。有什么想法吗?

爪哇:

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

ImageView imgFront;
ImageView imgBack;
ImageView imageView;
ImageView imageView2;
ImageView imageView3;
ImageView imageView4;
ImageView imageView5;
ImageView imageView6;
ImageView imageView7;
ImageView imageView8;
ImageView imageView9;
ImageView imageView10;
ImageView imageView11;
ImageView imageView12;
ImageView imageView13;
ImageView imageView14;
ImageView imageView15;
ImageView imageView16;
ImageView imageView17;
ImageView imageView18;
ImageView imageView19;
ImageView imageView20;
ImageView imageView21;
ImageView imageView22;







boolean isBackVisible = false; // Boolean variable to check if the back image is visible currently

AnimatorSet setRightOut;
AnimatorSet setLeftIn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setRightOut = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.flight_right_out);
    setLeftIn = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.flight_left_in);

    imgFront = (ImageView) findViewById(R.id.imgFront);
    imgBack = (ImageView) findViewById(R.id.imgBack);
    imageView = (ImageView) findViewById(R.id.imageView);
    imageView2 = (ImageView) findViewById(R.id.imageView2);
    imageView3 = (ImageView) findViewById(R.id.imageView3);
    imageView4 = (ImageView) findViewById(R.id.imageView4);
    imageView5 = (ImageView) findViewById(R.id.imageView5);
    imageView6 = (ImageView) findViewById(R.id.imageView6);
    imageView7 = (ImageView) findViewById(R.id.imageView7);
    imageView8 = (ImageView) findViewById(R.id.imageView8);
    imageView9 = (ImageView) findViewById(R.id.imageView9);
    imageView10 = (ImageView) findViewById(R.id.imageView10);
    imageView11 = (ImageView) findViewById(R.id.imageView11);
    imageView12 = (ImageView) findViewById(R.id.imageView12);
    imageView13 = (ImageView) findViewById(R.id.imageView13);
    imageView14 = (ImageView) findViewById(R.id.imageView14);
    imageView15 = (ImageView) findViewById(R.id.imageView15);
    imageView16 = (ImageView) findViewById(R.id.imageView16);
    imageView17 = (ImageView) findViewById(R.id.imageView17);
    imageView18 = (ImageView) findViewById(R.id.imageView18);
    imageView19 = (ImageView) findViewById(R.id.imageView19);
    imageView20 = (ImageView) findViewById(R.id.imageView20);
    imageView21 = (ImageView) findViewById(R.id.imageView21);
    imageView22 = (ImageView) findViewById(R.id.imageView22);




    imgFront.setOnClickListener(this);
    imgBack.setOnClickListener(this);
    imageView.setOnClickListener(this);
    imageView2.setOnClickListener(this);
    imageView3.setOnClickListener(this);
    imageView4.setOnClickListener(this);
    imageView5.setOnClickListener(this);
    imageView6.setOnClickListener(this);
    imageView7.setOnClickListener(this);
    imageView8.setOnClickListener(this);
    imageView9.setOnClickListener(this);
    imageView10.setOnClickListener(this);
    imageView11.setOnClickListener(this);
    imageView12.setOnClickListener(this);
    imageView13.setOnClickListener(this);
    imageView14.setOnClickListener(this);
    imageView15.setOnClickListener(this);
    imageView16.setOnClickListener(this);
    imageView17.setOnClickListener(this);
    imageView18.setOnClickListener(this);
    imageView19.setOnClickListener(this);
    imageView20.setOnClickListener(this);
    imageView21.setOnClickListener(this);
    imageView22.setOnClickListener(this);



}

@Override
public void onClick(View view) {
    switch (view.getId()) {


        case R.id.imgBack:
            if (!isBackVisible) {
                setRightOut.setTarget(imgFront);
                setLeftIn.setTarget(imgBack);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imgBack);
                setLeftIn.setTarget(imgFront);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;

        case R.id.imageView2:
            if (!isBackVisible) {
                setRightOut.setTarget(imageView);
                setLeftIn.setTarget(imageView2);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imageView2);
                setLeftIn.setTarget(imageView);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;

        case R.id.imageView4:
            if (!isBackVisible) {
                setRightOut.setTarget(imageView3);
                setLeftIn.setTarget(imageView4);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imageView4);
                setLeftIn.setTarget(imageView3);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;

        case R.id.imageView6:
            if (!isBackVisible) {
                setRightOut.setTarget(imageView5);
                setLeftIn.setTarget(imageView6);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imageView6);
                setLeftIn.setTarget(imageView5);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;

        case R.id.imageView8:
            if (!isBackVisible) {
                setRightOut.setTarget(imageView7);
                setLeftIn.setTarget(imageView8);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imageView8);
                setLeftIn.setTarget(imageView7);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;

        case R.id.imageView10:
            if (!isBackVisible) {
                setRightOut.setTarget(imageView9);
                setLeftIn.setTarget(imageView10);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imageView10);
                setLeftIn.setTarget(imageView9);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;

        case R.id.imageView12:
            if (!isBackVisible) {
                setRightOut.setTarget(imageView11);
                setLeftIn.setTarget(imageView12);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imageView12);
                setLeftIn.setTarget(imageView11);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;

        case R.id.imageView14:
            if (!isBackVisible) {
                setRightOut.setTarget(imageView13);
                setLeftIn.setTarget(imageView14);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imageView14);
                setLeftIn.setTarget(imageView13);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;

        case R.id.imageView16:
            if (!isBackVisible) {
                setRightOut.setTarget(imageView15);
                setLeftIn.setTarget(imageView16);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imageView16);
                setLeftIn.setTarget(imageView15);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;

        case R.id.imageView18:
            if (!isBackVisible) {
                setRightOut.setTarget(imageView17);
                setLeftIn.setTarget(imageView18);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imageView18);
                setLeftIn.setTarget(imageView17);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;

        case R.id.imageView20:
            if (!isBackVisible) {
                setRightOut.setTarget(imageView19);
                setLeftIn.setTarget(imageView20);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imageView20);
                setLeftIn.setTarget(imageView19);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;

        case R.id.imageView22:
            if (!isBackVisible) {
                setRightOut.setTarget(imageView21);
                setLeftIn.setTarget(imageView22);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(imageView22);
                setLeftIn.setTarget(imageView21);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;




    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

activity_main.xml中:

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="80dp"
    android:clickable="true">

    <ImageView
    android:id="@+id/imgFront"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:src="@drawable/front"
    android:layout_alignTop="@+id/imgBack"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
        />

    <ImageView
    android:id="@+id/imgBack"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:alpha="0"
    android:src="@drawable/back"
        android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
        android:contentDescription="@string/imgBack" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="80dp"
    android:clickable="true"
    android:layout_marginLeft="135dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/front"
        android:layout_alignTop="@+id/imageView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:alpha="0"
        android:src="@drawable/back"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/imgBack" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="80dp"
    android:clickable="true"
    android:layout_marginLeft="265dp">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/front"
        android:layout_alignTop="@+id/imageView4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:alpha="0"
        android:src="@drawable/back"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/imgBack" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="180dp"
    android:clickable="true" >

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/front"
        android:layout_alignTop="@+id/imageView6"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:alpha="0"
        android:src="@drawable/back"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/imgBack" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="180dp"
    android:clickable="true"
    android:layout_marginLeft="135dp" >

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/front"
        android:layout_alignTop="@+id/imageView8"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:alpha="0"
        android:src="@drawable/back"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/imgBack" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="180dp"
    android:clickable="true"
    android:layout_marginLeft="265dp" >

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/front"
        android:layout_alignTop="@+id/imageView10"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/imageView10"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:alpha="0"
        android:src="@drawable/back"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/imgBack" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="280dp"
    android:clickable="true" >

    <ImageView
        android:id="@+id/imageView11"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/front"
        android:layout_alignTop="@+id/imageView12"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/imageView12"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:alpha="0"
        android:src="@drawable/back"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/imgBack" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="280dp"
    android:clickable="true"
    android:layout_marginLeft="135dp" >

    <ImageView
        android:id="@+id/imageView13"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/front"
        android:layout_alignTop="@+id/imageView14"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/imageView14"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:alpha="0"
        android:src="@drawable/back"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/imgBack" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="280dp"
    android:clickable="true"
    android:layout_marginLeft="265dp" >

    <ImageView
        android:id="@+id/imageView15"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/front"
        android:layout_alignTop="@+id/imageView16"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/imageView16"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:alpha="0"
        android:src="@drawable/back"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/imgBack" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="380dp"
    android:clickable="true" >

    <ImageView
        android:id="@+id/imageView17"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/front"
        android:layout_alignTop="@+id/imageView18"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/imageView18"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:alpha="0"
        android:src="@drawable/back"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/imgBack" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="380dp"
    android:clickable="true"
    android:layout_marginLeft="135dp" >

    <ImageView
        android:id="@+id/imageView19"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/front"
        android:layout_alignTop="@+id/imageView20"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/imageView20"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:alpha="0"
        android:src="@drawable/back"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/imgBack" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="380dp"
    android:clickable="true"
    android:layout_marginLeft="265dp" >

    <ImageView
        android:id="@+id/imageView21"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/front"
        android:layout_alignTop="@+id/imageView22"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/imageView22"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:alpha="0"
        android:src="@drawable/back"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/imgBack" />
</RelativeLayout>

动画:Flight_Left_In:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="-180"
        android:valueTo="0"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="500" />

    <!-- When the roration reach half of animation, show the card -->
    <objectAnimator
        android:valueFrom="0.0"
        android:valueTo="1.0"
        android:propertyName="alpha"
        android:duration="1"
        android:startOffset="250"/>

</set>

动画:Flight_Right_Out:

 <set xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Rotate. -->
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="180"
        android:propertyName="rotationY"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:duration="500" />

    <!-- Half-way through the rotation, hide the front card -->
    <objectAnimator
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:propertyName="alpha"
        android:startOffset="250"
        android:duration="1" />
</set>

0 个答案:

没有答案