【问题标题】:Android Animation with onDraw or ImageView?带有 onDraw 或 ImageView 的 Android 动画?
【发布时间】:2013-02-15 16:29:41
【问题描述】:

我想创建一个安卓游戏。每次有人触摸显示器时,播放器应该上升,如果他松开,播放器应该下降。

在许多教程的帮助下,我让它工作了,但现在我想为它制作动画,但我被卡住了。这意味着玩家的图像应该每半秒改变一次。此外,当玩家上升时,应该创建一个动画旋转。

但是(经过数小时的谷歌搜索)我找不到任何有用的答案来解决我的问题。 Android 开发者网站讨论了创建 ImageView 和 XML 文件。但这就是我卡住的地方:我没有 ImageView,我的播放器(我使用 PNG 文件)只是由 onDraw() 方法创建的:

public void onDraw(Canvas canvas) {
       for (Sprite s : sprites) {   
            canvas.drawBitmap(s.getGraphic(), s.getLocation().x,
                    s.getLocation().y, null);

        }
}

现在我想问我应该如何做动画和动画旋转。我应该从 ImageView 开始还是可以以某种方式将 onDraw 方法“转换”为 ImageView?或者有没有其他方法可以在没有 ImageView 的情况下进行动画和动画旋转?

其次,如果我必须创建 ImageView,我不明白如何使播放器“动态”,即:当有人触摸显示屏时改变位置。

提前致谢:)

编辑:

好的,我在drawable文件夹中创建了我的animation.xml文件:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" android:id="@+id/splashAnimation">
<item android:drawable="@drawable/ship" android:duration="200" />
<item android:drawable="@drawable/ship_2" android:duration="200" />
</animation-list>

在我的主文件中我添加了:

ImageView img = (ImageView) findViewById(R.id.splashAnimation);
    img.setBackgroundResource(R.drawable.animation);
    ship_anim= (AnimationDrawable) img.getBackground();
ship_anim.start();

但是,现在我收到错误消息:NullPointerException

问题出在哪里?

【问题讨论】:

    标签: android animation rotation imageview ondraw


    【解决方案1】:

    如果你有描述动画的drawable,你总是可以像这样在你的drawable文件夹中创建xml,而不是将它作为imageView添加到布局中

        <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/img1" android:duration="100" />
    <item android:drawable="@drawable/img2" android:duration="100" />
    <item android:drawable="@drawable/img3" android:duration="100" />
    <item android:drawable="@drawable/mimg4" android:duration="100" />
    <item android:drawable="@drawable/img5" android:duration="100" />   
    </animation-list>
    

    它的代码如下所示:

    myAnimation = (AnimationDrawable) findViewById(R.id.animation);
    

    只添加事件处理程序而不是

    if (!isPlaying)
    {
    playDrawableAnimation();
        isPlaying = true;
    } else
    {
        faceAnimation.stop();
        isPlaying = false;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多