如何使用动画在android中翻转图像

时间:2013-09-20 17:48:15

标签: java android animation flip

我正在寻找代码来翻转图像或按钮来显示图像的另一面或按钮.Basicalyy有两个图像,一次显示一个。在单击一个图像时,它应该翻转显示另一个图像,使第一个图像隐藏。我尝试了一些在线示例应用程序,但evrytime我得到运行时错误。下面给出的代码是其中之一。这里有一个图像和一个按钮。点击按钮,图像应该翻转。 我尝试了这段代码但是我遇到了运行时错误。

这些是/ res / anim文件夹中的2个xml文件

to_middle.xml

   <?xml version="1.0" encoding="utf-8"?>
   <scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1.0" android:toXScale="0.0"
    android:pivotX="50%"
    android:fromYScale="1.0" android:toYScale="1.0"
    android:pivotY="50%"
    android:duration="250" />

from_middle.xml

   <?xml version="1.0" encoding="utf-8"?>
    <scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="0.0" android:toXScale="1.0"
    android:pivotX="50%"
    android:fromYScale="1.0" android:toYScale="1.0"
    android:pivotY="50%"
    android:duration="250" />

这是/ res / layout /

中的activty_main.xml文件
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation = "vertical"
   android:background="#006600"
   android:gravity = "center">

  <TextView
   android:id="@+id/textView1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Simulated Card Deal"
   android:textColor="#ffffff"
   android:textSize="26sp"
   android:layout_margin="10dip"
   android:textStyle="bold" />

   <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dip"
    android:gravity="center"
    android:src="@drawable/card_back" />

  <Button
   android:id="@+id/button1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_margin="10dip"
   android:padding="10dip"
   android:text="Hit Me!" />
  </LinearLayout>

这是MainActivity.java文件

   package com.example.game5;
   import android.os.Bundle;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.view.animation.Animation;
   import android.view.animation.Animation.AnimationListener;
   import android.view.animation.AnimationUtils;
   import android.widget.ImageView;
   import android.app.Activity;
    public class MainActivity extends Activity implements OnClickListener,
    AnimationListener {

         private Animation animation1;
         private Animation animation2;
         private boolean isBackOfCardShowing = true;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle);
         animation1.setAnimationListener(this);
         animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
         animation2.setAnimationListener(this);
         findViewById(R.id.button1).setOnClickListener(this);
        }
   @Override
   public void onClick(View v) {
          v.setEnabled(false);
          ((ImageView)findViewById(R.id.imageView1)).clearAnimation();
          ((ImageView)findViewById(R.id.imageView1)).setAnimation(animation1);
          ((ImageView)findViewById(R.id.imageView1)).startAnimation(animation1);
    }
    @Override
    public void onAnimationEnd(Animation animation) {
          if (animation==animation1) {
                 if (isBackOfCardShowing) {
    ((ImageView)findViewById(R.id.imageView1)).setImageResource(R.drawable.card_front);
                   } else {
    ((ImageView)findViewById(R.id.imageView1)).setImageResource(R.drawable.card_back);
                   }
                   ((ImageView)findViewById(R.id.imageView1)).clearAnimation();
     ((ImageView)findViewById(R.id.imageView1)).setAnimation(animation2);
     ((ImageView)findViewById(R.id.imageView1)).startAnimation(animation2);
             } else {
                    isBackOfCardShowing=!isBackOfCardShowing;
                    findViewById(R.id.button1).setEnabled(true);
             }
    }
    @Override
    public void onAnimationRepeat(Animation animation) {
           // TODO Auto-generated method stub
    }
    @Override
    public void onAnimationStart(Animation animation) {
    // TODO Auto-generated method stub
    }
   }
运行应用程序时,屏幕上会显示

检测到应用程序故障消息。 请有人帮我提供此代码或建议适当的教程。谢谢。这是logcat输出

09-20 22:23:55.550: E/AndroidRuntime(1860): FATAL EXCEPTION: main
09-20 22:23:55.550: E/AndroidRuntime(1860): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.game5/com.example.game5.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class android.widget.ImageView
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.os.Looper.loop(Looper.java:130)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at java.lang.reflect.Method.invoke(Method.java:507)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at dalvik.system.NativeStart.main(Native Method)
09-20 22:23:55.550: E/AndroidRuntime(1860): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.widget.ImageView
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.view.LayoutInflater.createView(LayoutInflater.java:518)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.app.Activity.setContentView(Activity.java:1660)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at com.example.game5.MainActivity.onCreate(MainActivity.java:30)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-20 22:23:55.550: E/AndroidRuntime(1860):     ... 11 more
09-20 22:23:55.550: E/AndroidRuntime(1860): Caused by: java.lang.reflect.InvocationTargetException
09-20 22:23:55.550: E/AndroidRuntime(1860):     at java.lang.reflect.Constructor.constructNative(Native Method)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at android.view.LayoutInflater.createView(LayoutInflater.java:505)
09-20 22:23:55.550: E/AndroidRuntime(1860):     ... 22 more

1 个答案:

答案 0 :(得分:4)

当您尝试 inflate view 时,会发生错误。这意味着您的XML文件存在问题。但是,一切看起来都是正确的语法。确保所有 drawables 都在各自的文件夹中并正确命名。

另外,尝试清理项目。这样就可以创建一个新的 R.java

快乐编码。:)