开发者

Eclipse development for Android: Layout Editor's Custom Views dialog

I'm writing a game for Android, and would like to use the built-in layout system to lay things out based on the size and shape of the screen (which also means it'll change orientation dynamically), so I've written an interface (called "Renderable") that is wrapped by a View-derived class (called "RenderableView").

Since I am using the latest version of Eclipse, the Custom & Library Views section of the Layout Editor's tool pallet does show my class, but shows several copies of each View-derived class.

Is this a bug in Eclipse or with my code?

Here is my Renderable interface:

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);
}

And here is the code for my RenderableView class:

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(开发者_JS百科)
    {
        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);
        }
    }
}

I'm not sure if this is the place to ask this, so please censor this as necessary.


This is a bug in ADT; the Eclipse code search engine returns the same class twice. I've changed the data structure where we accumulate custom views from using a List to using a Set. This fix will go into ADT 15.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜