简体   繁体   English

带有onDraw或ImageView的Android动画?

[英]Android Animation with onDraw or ImageView?

I want to create an android game. 我想创建一个Android游戏。 Everytime someone touches the display the player should go up, and if he releases the player should fall. 每当有人触摸显示屏时,播放器都应该上升,如果释放,播放器应该跌倒。

With the help of many tutorials I made it work but now I want to animate it and I'm stuck. 在许多教程的帮助下,我使它工作了,但是现在我想对其进行动画处理,这很困难。 That means the image of the player should be changed every half a second. 这意味着播放器的图像应每半秒钟更改一次。 Additionally an animated rotation should be created when the player goes up. 此外,播放器上移时应创建动画旋转。

However (after hours of googling) I couldnt find any helpful answer to my problem. 但是(经过数小时的谷歌搜索),我找不到解决我问题的任何有用答案。 The Android Developers site talks about creating an ImageView and a XML file. Android开发人员网站讨论有关创建ImageView和XML文件的问题。 But thats where I'm stuck: I dont have an ImageView, my player (for which I used a PNG file) is simply created by the onDraw() method: 但这就是我遇到的问题:我没有ImageView,我的播放器(我使用了PNG文件)只是通过onDraw()方法创建的:

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

        }
}

Now I wanted to ask how I should do the animation and the animated rotation. 现在,我想问一下如何制作动画和动画旋转。 Should I start off with an ImageView or can I somehow "convert" the onDraw method to an ImageView? 我应该从ImageView开始还是可以将onDraw方法“转换”为ImageView? Or is there another way to do the animation and animated rotation without an ImageView? 还是有另一种方法可以在没有ImageView的情况下进行动画和动画旋转?

Secondly, if I had to create the ImageView I don't understand how I can make the player "dynamic", ie: changing the position when someone touches the display. 其次,如果我必须创建ImageView,我不了解如何使播放器“具有动态性”,即:当有人触摸显示器时改变位置。

Thanks in advance :) 提前致谢 :)

EDIT: 编辑:

Ok, I created my animation.xml file in the drawable folder: 好的,我在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>

and in my main file I added: 在我的主文件中添加:

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

However, now I get the error message: NullPointerException 但是,现在我收到错误消息:NullPointerException

Where is the Problem? 问题出在哪儿?

if you have drawables that describe animation, you can always create xml in your drawable folder like this than add this as imageView to the layout 如果您有描述动画的可绘制对象,则始终可以像这样在可绘制文件夹中创建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>

The code for it would look like this: 它的代码如下所示:

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

Add just event handler than 仅添加事件处理程序

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM