我正在为Android编写一款游戏,并希望使用内置布局系统基于屏幕的大小和形状来布局内容(这也意味着它将动态改变方向),所以我编写了一个由视图派生类(称为"RenderableView")包装的界面(称为"Renderable")。
由于我使用的是最新版本的Eclipse,自定义&布局编辑器工具面板的库视图部分显示了我的类,但是显示了每个视图派生类的几个副本。
这是一个bug在Eclipse或与我的代码?
下面是我的Renderable界面:
package games.DigSite;
import android.graphics.*;
public interface Renderable
{
/**
* Sets the new position and size for this Renderable.
* @param bounds THe new bounds for this Renderable.
*/
public void setBounds(RectF bounds);
/**
* Returns the current position and size of this Renderable.
* @return The bounds this Renderable has.
*/
public RectF getBounds();
/**
* Tells the Renderable to draw itself.
* @param canvas The Canvas to draw to.
*/
public void render(Canvas canvas);
}
下面是我的RenderableView类的代码:
package games.DigSite;
import games.DigSite.play.*;
import android.content.*;
import android.graphics.*;
import android.util.*;
import android.view.*;
import android.view.SurfaceHolder.Callback;
public class RenderableView extends SurfaceView implements Callback, PlayConstants
{
private RectF bounds;
private Renderable renderable;
public RenderableView(Context context)
{
super(context);
getHolder().addCallback(this);
}
public RenderableView(Context context, AttributeSet attrs)
{
super(context, attrs);
getHolder().addCallback(this);
}
public RenderableView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
getHolder().addCallback(this);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
bounds = new RectF(0, 0, width, height);
if (renderable != null)
renderable.setBounds(bounds);
}
public void setRenderable(Renderable render)
{
renderable = render;
}
public Renderable getRenderable()
{
return renderable;
}
public void surfaceCreated(SurfaceHolder holder)
{
// Dunno if we need this one just yet
}
public void surfaceDestroyed(SurfaceHolder holder)
{
// Dunno if we need this one just yet
}
public void onDraw(Canvas canvas)
{
if (renderable != null)
renderable.render(canvas);
else
{
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(BACKGROUND_COLOUR);
canvas.drawRect(bounds, paint);
}
}
}
我不确定这是否适合问这个问题,所以请在必要时审查一下。
这是ADT中的一个bug;Eclipse代码搜索引擎两次返回同一个类。我已经更改了数据结构,其中我们从使用List到使用Set来累积自定义视图。这个修复将进入ADT 15.