Android:无法翻转

时间:2015-11-04 02:21:46

标签: android imageview flip

我编写了一个应用程序,当用户点击ImageView时,它会翻转到另一侧。一开始,将显示imgFront。用户点击后,imgBack应显示在与imgFront相同的轴上。但是,它不会发生,但我的应用程序能够顺利运行。关于什么是问题的任何想法?

爪哇:

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 imageView3;
ImageView imageView4;

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);
    imgFront.setOnClickListener(this);

    imgBack = (ImageView) findViewById(R.id.imgBack);
    imgBack.setOnClickListener(this);

    imageView3 = (ImageView) findViewById(R.id.imageView3);
    imageView3.setOnClickListener(this);

    imageView4 = (ImageView) findViewById(R.id.imageView4);
    imageView4.setOnClickListener(this);





}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.imgFront:
            if (!isBackVisible) {
                setRightOut.setTarget(this.imgFront);
                setLeftIn.setTarget(this.imgBack);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(this.imgBack);
                setLeftIn.setTarget(this.imgFront);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = false;
            }
            break;
        case R.id.imageView3:
            if (!isBackVisible) {
                setRightOut.setTarget(this.imageView3);
                setLeftIn.setTarget(this.imageView4);
                setRightOut.start();
                setLeftIn.start();
                isBackVisible = true;
            } else {
                setRightOut.setTarget(this.imageView4);
                setLeftIn.setTarget(this.imageView3);
                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.xml:

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

    <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"
        android:contentDescription="@string/imgFront"
        android:clickable="false" />

    <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"
        android:clickable="false" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_marginTop="80dp"
    android:layout_marginLeft="130dp"
    android:clickable="false">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/front"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/imgFront3"
        android:clickable="false" />

    <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="imgBack3"
        android:clickable="false" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

该代码的精力充沛,但请查看我刚刚开发的FlipView库,其中包括基本的翻转动画并扩展ViewFlipper。我的意思是一个完全可自定义的库,您可以使用任何类型的动画和形状交换任何类型的视图和布局(包括Gmail图像翻转)。