有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在安卓中使用动画翻转图像

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

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

到中间去。xml

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

从中间开始。xml

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

这是活动中心。/res/layout中的xml文件/

  <LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
   安卓:layout_width="fill_parent"
   安卓:layout_height="fill_parent"
   安卓:orientation = "vertical"
   安卓:background="#006600"
   安卓:gravity = "center">

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

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

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

这是主要的活动。java文件

   package com.example.game5;
   import 安卓.os.Bundle;
   import 安卓.view.View;
   import 安卓.view.View.OnClickListener;
   import 安卓.view.animation.Animation;
   import 安卓.view.animation.Animation.AnimationListener;
   import 安卓.view.animation.AnimationUtils;
   import 安卓.widget.ImageView;
   import 安卓.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}: 安卓.view.InflateException: Binary XML file line #16: Error inflating class 安卓.widget.ImageView
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.app.ActivityThread.access$1500(ActivityThread.java:117)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.os.Handler.dispatchMessage(Handler.java:99)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.os.Looper.loop(Looper.java:130)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at com.安卓.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: 安卓.view.InflateException: Binary XML file line #16: Error inflating class 安卓.widget.ImageView
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.view.LayoutInflater.createView(LayoutInflater.java:518)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at com.安卓.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.view.LayoutInflater.rInflate(LayoutInflater.java:623)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:408)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at com.安卓.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.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 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-20 22:23:55.550: E/AndroidRuntime(1860):     at 安卓.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 安卓.view.LayoutInflater.createView(LayoutInflater.java:505)
09-20 22:23:55.550: E/AndroidRuntime(1860):     ... 22 more

共 (1) 个答案

  1. # 1 楼答案

    当您尝试inflateview时,会发生错误。这意味着你的XML文件有问题。但是,所有的语法似乎都是正确的。确保所有的drawables都在各自的文件夹中,并正确命名

    另外,试着清理你的项目。这样就创建了一个新的R.java

    快乐编码。:)