开发者

Initializing SurfaceView with a Drawable

I'd like to open up a SurfaceView with a icon placed in the center of the screen when an application is first started. I'm evoking icon creation using an implementation of SurfaceHolder.Callback to track when the 'Canvas' object is ready. My question is is there a bet开发者_如何学编程ter way? Are there less cumbersome methods of starting a SurfaceView with some Drawables loaded on creation without having to resort to placing draw logic within a callback object?

Here's my code for reference. First the object which does drawing:

public class CanvasDraw{
    protected final SurfaceHolder mHolder;
    protected final Drawable mDrawable;

    public interface DrawLogic{
        void draw(Rect _surface);
    }

    public CanvasDraw(SurfaceView _view, Drawable _drawable){
        mHolder = _view.getHolder();
        mDrawable = _drawable;
    }

    public void draw(DrawLogic _logic){
        Canvas canvas = null;
        try{
            canvas = mHolder.lockCanvas();
            if( canvas != null ){
                Log.i("DRAWABLE", "Drawing " + mDrawable.toString());
                _logic.draw( mHolder.getSurfaceFrame() );
                mDrawable.draw(canvas);
            }
            else{
                Log.i("DRAWABLE", "Canvas null valued");
            }
        }
        finally{
            if( canvas != null ){
                mHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

and then my private callback object:

private class DrawOnceCallback implements SurfaceHolder.Callback {
    private final SurfaceHolder mHolder;

    public DrawOnceCallback(SurfaceHolder _holder ){
        mHolder = _holder;
        mHolder.addCallback(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.i("SURFACEHOLDER","Surface created.  Canvas available.");
        mDrawTiles.draw( new CanvasDraw.DrawLogic(){

            @Override
            public void draw(Rect _surface) {
                mTiles.setBounds( 
                    _surface.centerX() - mDrawWidth/2,
                    _surface.centerY() - mDrawHeight/2,
                    _surface.centerX() + mDrawHeight/2,
                    _surface.centerY() + mDrawHeight/2);
            }

        });
        mHolder.removeCallback(this);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {Log.i("SURFACEHOLDER","Surface destroyed.");}
}


I did something like this simply by assigning the image I wanted to a view in the layout XML file. If you've got to wait until your canvas and all that is ready anyways why not just use a default property? My opening splash screen was just a view with

android:background="@drawable/splash"

added to its layout.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜