【发布时间】:2011-06-12 23:39:55
【问题描述】:
自从我不久前拿到我的 Droid X 之后,我就一直在琢磨研究 Android SDK 的想法,并且最近才采取行动。我在 Eclipse 中安装了 ADT 插件,下载并安装了 Android SDK (Rev. 11),并尝试了一些东西。我想尝试 2d 图形,因为它们与我在工作中花费的时间(嵌入式系统开发)大相径庭。
我尝试按照教程here 进行操作,但由于两个已实现的接口出现一些错误而无法构建。我正在尝试在 64 位 Windows 7 机器上构建 2.3.3。我将包含下面的代码,并重要警告我不是一个典型的 Java 程序员,一些明显的事情很容易让我逃脱。
Drawable Panel.java
package com.android.tutorial;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public abstract class DrawablePanel
extends SurfaceView
implements SurfaceHolder.Callback, ISurface {
private AnimationThread thread;
public DrawablePanel(Context context) {
super(context);
getHolder().addCallback(this);
this.thread = new AnimationThread(getHolder(), this);
}
@Override
public void onDraw(Canvas canvas) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
ISurface.java
package com.android.tutorial;
import android.graphics.Canvas;
public interface ISurface {
void onInitialize();
void onDraw(Canvas canvas);
void onUpdate(long gameTime);
}
AndroidTutorial.java
package com.android.tutorial;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
public class AndroidTutorial extends Activity {
AnimatedSprite animation = new AnimatedSprite();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new AndroidTutorialPanel(this));
}
class AndroidTutorialPanel extends DrawablePanel {
public AndroidTutorialPanel(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
AndroidTutorial.this.animation.draw(canvas);
}
@Override
public void onInitialize() {
AndroidTutorial.this.animation.Initialize(
BitmapFactory.decodeResource(
getResources(),
R.drawable.explosion),
32, 32, 14, 7);
}
@Override
public void onUpdate(long gameTime) {
AndroidTutorial.this.animation.Update(gameTime);
}
}
}
DrawablePanel.java 在surfaceChanged()、surfaceCreated() 和surfaceDestroyed() 的定义中遇到以下错误:
The method [name of function mentioned above](SurfaceHolder) of type DrawablePanel must override a superclass method.
onInitialize 和 AndroidTutorial.java 中的 onUpdate 是一样的。我迷路了,因为我可以在工作中复制/粘贴相同的东西(SDK 的第 10 版),没有错误。有什么想法吗?
【问题讨论】:
标签: android graphics surfaceholder