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

标签 java android animation flip

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

这些是/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

最佳答案

当您尝试膨胀 View 时,会发生错误。这意味着您的 XML 文件有问题。但是,一切似乎都符合正确的语法。确保所有drawables都位于各自的文件夹中并且命名正确。

此外,请尝试清理您的项目。这样就创建了一个新的 R.java

快乐编码。:)

关于java - 如何在android中使用动画翻转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18922869/

相关文章:

javascript - 如何使用脚本将页面上已有的选定信息获取到页面的另一部分

java - 无法在 gradle 中添加依赖项(Android Studio)

java - 如何正确处理来自Web服务的anyType()?

智能手机和/或平板电脑的安卓开发

javascript - jquery 禁用点击直到动画完全完成

java - hibernate-spatial jpa 距离查询

java - 在 Java 中按值对数组元素进行分组(2 × 2、3 × 3 等)

android - 如何使用 DownloadManager 列

iphone - 在 iPhone 中为 imageview 制作动画效果,就像在天空中飞翔一样

java - 尝试在 JavaFX 中创建一个 "Ball"类,该类由一个可通过按钮控制的圆圈组成,可在屏幕上向左、向右、向上和向下移动