Android - 如何淡入图像?
好吧,我不明白我该怎么做。
我想要它做的就是让图像在我的游戏之上淡出。
此方法位于我的 MainGame 类中,该类扩展了 SurfaceView 并实现了 SurfaceHolder.Callback
这是我的 fadein.xml
<设置 xmlns:android="http://schemas.android.com/apk/res/android">
我已经尝试过:
公共无效动画(){ ImageView myImageView= (ImageView)findViewById(R.id.block1); 动画 myFadeInAnimation = AnimationUtils.loadAnimation(this.getApplicationContext(), R.anim.fadein); myImageView.startAnimation(myFadeInAnimation); }
AnimationUtils 类型中的方法 loadAnimation(Context, int) 不适用于参数 (MainGame, int)
以及
公共无效动画(){ ImageView myImageView= (ImageView)findViewById(R.id.myImageView); 动画 myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein); myImageView.startAnimation(myFadeInAnimation); }
MainGame 类型的 getApplicationContext() 方法未定义
我还尝试将 Animation 方法放入其自己的名为 fade 的类中,该类扩展了 Activity 并在我的应用程序中创建了一个名为 Fade 的动画对象 在方法中调用它
MainGame 类并通过Fade.Animation()
;我可以编译并运行它,但是一旦包含它的方法运行它就会崩溃。
也许有人可以为我提供一个基本的示例或教程?任何帮助都会很棒,谢谢。
Okay so I don't understand how I'm supposed to do this.
All I want it to do is have an image fade on top of my game.
This method is in my MainGame class which extends SurfaceView and implements SurfaceHolder.Callback
Here is my fadein.xml
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="3000"
android:repeatCount="infinite"/>
</set>
I've tried:
public void Animation() {
ImageView myImageView= (ImageView)findViewById(R.id.block1);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(this.getApplicationContext(), R.anim.fadein);
myImageView.startAnimation(myFadeInAnimation);
}The method loadAnimation(Context, int) in the type AnimationUtils is not applicable for the arguments (MainGame, int)
as well as
public void Animation() {
ImageView myImageView= (ImageView)findViewById(R.id.myImageView);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein);
myImageView.startAnimation(myFadeInAnimation);
}The method getApplicationContext() is undefined for the type MainGame
I've also tried putting the Animation method in it's own class called fade that extends Activity and creating an animation object called Fade in my MainGame class and calling it in a method by
Fade.Animation();
Which I can compile and run but it crashes as soon as the method containing that runs.
Perhaps someone could provide me with a basic example or a tutorial? Any help would be great, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用 getApplicationContext(),而是使用 Activity 中的 Context 对象。这可能会在 SurfaceView 的构造函数中传递给它。您可以在视图的构造函数中保存对此的引用,并在视图需要上下文时使用它。
Instead of using getApplicationContext(), use the Context object from your Activity. This is probably handed to your SurfaceView in its constructor. You can save a reference to this in the View's constructor and use it when the View needs a context.