FileDocCategorySizeDatePackage
BridgeCanvas.javaAPI DocAndroid 1.5 API35418Wed May 06 22:42:02 BST 2009com.android.layoutlib.bridge

BridgeCanvas

public class BridgeCanvas extends android.graphics.Canvas
Re-implementation of the Canvas, 100% in java on top of a BufferedImage.

Fields Summary
private BufferedImage
mBufferedImage
private final Stack
mGraphicsStack
private final com.android.layoutlib.api.ILayoutLog
mLogger
Constructors Summary
public BridgeCanvas(int width, int height, com.android.layoutlib.api.ILayoutLog logger)


           
        mLogger = logger;
        mBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        mGraphicsStack.push(mBufferedImage.createGraphics());
    
public BridgeCanvas(int width, int height)

        this(width, height, null /* logger*/);
    
Methods Summary
public booleanclipPath(android.graphics.Path path, android.graphics.Region.Op op)

        // TODO Auto-generated method stub
        return super.clipPath(path, op);
    
public booleanclipPath(android.graphics.Path path)

        // TODO Auto-generated method stub
        return super.clipPath(path);
    
public booleanclipRect(float left, float top, float right, float bottom, android.graphics.Region.Op op)

        return clipRect(left, top, right, bottom);
    
public booleanclipRect(float left, float top, float right, float bottom)

        getGraphics2d().clipRect((int)left, (int)top, (int)(right-left), (int)(bottom-top));
        return true;
    
public booleanclipRect(int left, int top, int right, int bottom)

        getGraphics2d().clipRect(left, top, right-left, bottom-top);
        return true;
    
public booleanclipRect(android.graphics.Rect rect, android.graphics.Region.Op op)

        return clipRect(rect.left, rect.top, rect.right, rect.bottom);
    
public booleanclipRect(android.graphics.Rect rect)

        return clipRect(rect.left, rect.top, rect.right, rect.bottom);
    
public booleanclipRect(android.graphics.RectF rect, android.graphics.Region.Op op)

        return clipRect(rect.left, rect.top, rect.right, rect.bottom);
    
public booleanclipRect(android.graphics.RectF rect)

        return clipRect(rect.left, rect.top, rect.right, rect.bottom);
    
public booleanclipRegion(android.graphics.Region region, android.graphics.Region.Op op)

        // TODO Auto-generated method stub
        return super.clipRegion(region, op);
    
public booleanclipRegion(android.graphics.Region region)

        // TODO Auto-generated method stub
        return super.clipRegion(region);
    
public voidconcat(android.graphics.Matrix matrix)

        // TODO Auto-generated method stub
        super.concat(matrix);
    
voiddispose()

        while (mGraphicsStack.size() > 0) {
            mGraphicsStack.pop().dispose();
        }
    
private final voiddoDrawRect(int left, int top, int width, int height, android.graphics.Paint paint)

        // get current graphisc
        Graphics2D g = getGraphics2d();
        
        g = getNewGraphics(paint, g);

        Style style = paint.getStyle();
        
        // draw
        if (style == Style.FILL || style == Style.FILL_AND_STROKE) {
            g.fillRect(left, top, width, height);
        }

        if (style == Style.STROKE || style == Style.FILL_AND_STROKE) {
            g.drawRect(left, top, width, height);
        }

        // dispose Graphics2D object
        g.dispose();
    
public voiddrawARGB(int a, int r, int g, int b)

        drawColor(a << 24 | r << 16 | g << 8 | b, PorterDuff.Mode.SRC_OVER);
    
public voiddrawArc(android.graphics.RectF oval, float startAngle, float sweepAngle, boolean useCenter, android.graphics.Paint paint)

        // TODO Auto-generated method stub
        super.drawArc(oval, startAngle, sweepAngle, useCenter, paint);
    
public voiddrawBitmap(android.graphics.Bitmap bitmap, float left, float top, android.graphics.Paint paint)

        drawBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                (int)left, (int)top,
                (int)left+bitmap.getWidth(), (int)top+bitmap.getHeight(), paint);
    
public voiddrawBitmap(android.graphics.Bitmap bitmap, android.graphics.Matrix matrix, android.graphics.Paint paint)

        throw new UnsupportedOperationException();
    
public voiddrawBitmap(android.graphics.Bitmap bitmap, android.graphics.Rect src, android.graphics.Rect dst, android.graphics.Paint paint)

        if (src == null) {
            drawBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                    dst.left, dst.top, dst.right, dst.bottom, paint);
        } else {
            drawBitmap(bitmap, src.left, src.top, src.width(), src.height(),
                    dst.left, dst.top, dst.right, dst.bottom, paint);
        }
    
public voiddrawBitmap(android.graphics.Bitmap bitmap, android.graphics.Rect src, android.graphics.RectF dst, android.graphics.Paint paint)

        if (src == null) {
            drawBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                    (int)dst.left, (int)dst.top, (int)dst.right, (int)dst.bottom, paint);
        } else {
            drawBitmap(bitmap, src.left, src.top, src.width(), src.height(),
                    (int)dst.left, (int)dst.top, (int)dst.right, (int)dst.bottom, paint);
        }
    
public voiddrawBitmap(int[] colors, int offset, int stride, int x, int y, int width, int height, boolean hasAlpha, android.graphics.Paint paint)

        throw new UnsupportedOperationException();
    
private voiddrawBitmap(android.graphics.Bitmap bitmap, int sleft, int stop, int sright, int sbottom, int dleft, int dtop, int dright, int dbottom, android.graphics.Paint paint)

        BufferedImage image = bitmap.getImage();
        
        Graphics2D g = getGraphics2d();
        
        Composite c = null;
        
        if (paint.isFilterBitmap()) {
            g = (Graphics2D)g.create();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        }
        
        if (paint.getAlpha() != 0xFF) {
            c = g.getComposite();
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                    paint.getAlpha()/255.f));
        }
        
        g.drawImage(image, dleft, dtop, dright, dbottom,
                sleft, stop, sright, sbottom, null);

        if (paint.isFilterBitmap()) {
            g.dispose();
        }
        
        if (c != null) {
            g.setComposite(c);
        }
    
public voiddrawBitmapMesh(android.graphics.Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, int vertOffset, int[] colors, int colorOffset, android.graphics.Paint paint)

        // TODO Auto-generated method stub
        super.drawBitmapMesh(bitmap, meshWidth, meshHeight, verts, vertOffset, colors, colorOffset, paint);
    
public voiddrawCircle(float cx, float cy, float radius, android.graphics.Paint paint)

        // get current graphisc
        Graphics2D g = getGraphics2d();
        
        g = getNewGraphics(paint, g);

        Style style = paint.getStyle();
        
        int size = (int)(radius * 2);

        // draw
        if (style == Style.FILL || style == Style.FILL_AND_STROKE) {
            g.fillOval((int)(cx - radius), (int)(cy - radius), size, size);
        }

        if (style == Style.STROKE || style == Style.FILL_AND_STROKE) {
            g.drawOval((int)(cx - radius), (int)(cy - radius), size, size);
        }

        // dispose Graphics2D object
        g.dispose();
    
public voiddrawColor(int color, PorterDuff.Mode mode)

        Graphics2D g = getGraphics2d();
        
        // save old color
        Color c = g.getColor();
        
        Composite composite = g.getComposite();
        
        // get the alpha from the color
        int alpha = color >>> 24;
        float falpha = alpha / 255.f;
        
        setModeInGraphics(mode, g, falpha);
        
        g.setColor(new Color(color));
        
        getGraphics2d().fillRect(0, 0, getWidth(), getHeight());
        
        g.setComposite(composite);
        
        // restore color
        g.setColor(c);
    
public voiddrawColor(int color)

        drawColor(color, PorterDuff.Mode.SRC_OVER);
    
public voiddrawLine(float startX, float startY, float stopX, float stopY, android.graphics.Paint paint)

        // get current graphisc
        Graphics2D g = getGraphics2d();
        
        g = getNewGraphics(paint, g);

        g.drawLine((int)startX, (int)startY, (int)stopX, (int)stopY);

        // dispose Graphics2D object
        g.dispose();
    
public voiddrawLines(float[] pts, int offset, int count, android.graphics.Paint paint)

        // get current graphisc
        Graphics2D g = getGraphics2d();
        
        g = getNewGraphics(paint, g);

        for (int i = 0 ; i < count ; i += 4) {
            g.drawLine((int)pts[i + offset], (int)pts[i + offset + 1],
                    (int)pts[i + offset + 2], (int)pts[i + offset + 3]);
        }

        // dispose Graphics2D object
        g.dispose();
    
public voiddrawLines(float[] pts, android.graphics.Paint paint)

        drawLines(pts, 0, pts.length, paint);
    
public voiddrawOval(android.graphics.RectF oval, android.graphics.Paint paint)

        // get current graphics
        Graphics2D g = getGraphics2d();
        
        g = getNewGraphics(paint, g);

        Style style = paint.getStyle();
        
        // draw
        if (style == Style.FILL || style == Style.FILL_AND_STROKE) {
            g.fillOval((int)oval.left, (int)oval.top, (int)oval.width(), (int)oval.height());
        }

        if (style == Style.STROKE || style == Style.FILL_AND_STROKE) {
            g.drawOval((int)oval.left, (int)oval.top, (int)oval.width(), (int)oval.height());
        }

        // dispose Graphics2D object
        g.dispose();
    
public voiddrawPaint(android.graphics.Paint paint)

        drawColor(paint.getColor());
    
public voiddrawPath(android.graphics.Path path, android.graphics.Paint paint)

        // get current graphics
        Graphics2D g = getGraphics2d();
        
        g = getNewGraphics(paint, g);

        Style style = paint.getStyle();
        
        // draw
        if (style == Style.FILL || style == Style.FILL_AND_STROKE) {
            g.fill(path.getAwtShape());
        }

        if (style == Style.STROKE || style == Style.FILL_AND_STROKE) {
            g.draw(path.getAwtShape());
        }

        // dispose Graphics2D object
        g.dispose();
    
public voiddrawPicture(android.graphics.Picture picture, android.graphics.Rect dst)

        // TODO Auto-generated method stub
        super.drawPicture(picture, dst);
    
public voiddrawPicture(android.graphics.Picture picture, android.graphics.RectF dst)

        // TODO Auto-generated method stub
        super.drawPicture(picture, dst);
    
public voiddrawPicture(android.graphics.Picture picture)

        // TODO Auto-generated method stub
        super.drawPicture(picture);
    
public voiddrawPoint(float x, float y, android.graphics.Paint paint)

        // TODO Auto-generated method stub
        super.drawPoint(x, y, paint);
    
public voiddrawPoints(float[] pts, int offset, int count, android.graphics.Paint paint)

        // TODO Auto-generated method stub
        super.drawPoints(pts, offset, count, paint);
    
public voiddrawPoints(float[] pts, android.graphics.Paint paint)

        // TODO Auto-generated method stub
        super.drawPoints(pts, paint);
    
public voiddrawPosText(char[] text, int index, int count, float[] pos, android.graphics.Paint paint)

        // TODO Auto-generated method stub
        super.drawPosText(text, index, count, pos, paint);
    
public voiddrawPosText(java.lang.String text, float[] pos, android.graphics.Paint paint)

        // TODO Auto-generated method stub
        super.drawPosText(text, pos, paint);
    
public voiddrawRGB(int r, int g, int b)

        drawColor(0xFF << 24 | r << 16 | g << 8 | b, PorterDuff.Mode.SRC_OVER);
    
public voiddrawRect(android.graphics.RectF rect, android.graphics.Paint paint)

        doDrawRect((int)rect.left, (int)rect.top, (int)rect.width(), (int)rect.height(), paint);
    
public voiddrawRect(float left, float top, float right, float bottom, android.graphics.Paint paint)

        doDrawRect((int)left, (int)top, (int)(right-left), (int)(bottom-top), paint);
    
public voiddrawRect(android.graphics.Rect r, android.graphics.Paint paint)

        doDrawRect(r.left, r.top, r.width(), r.height(), paint);
    
public voiddrawRoundRect(android.graphics.RectF rect, float rx, float ry, android.graphics.Paint paint)

        // get current graphisc
        Graphics2D g = getGraphics2d();
        
        g = getNewGraphics(paint, g);

        Style style = paint.getStyle();
        
        // draw
        
        int arcWidth = (int)(rx * 2);
        int arcHeight = (int)(ry * 2);
        
        if (style == Style.FILL || style == Style.FILL_AND_STROKE) {
            g.fillRoundRect((int)rect.left, (int)rect.top, (int)rect.width(), (int)rect.height(),
                    arcWidth, arcHeight);
        }

        if (style == Style.STROKE || style == Style.FILL_AND_STROKE) {
            g.drawRoundRect((int)rect.left, (int)rect.top, (int)rect.width(), (int)rect.height(),
                    arcWidth, arcHeight);
        }

        // dispose Graphics2D object
        g.dispose();
    
public voiddrawText(char[] text, int index, int count, float x, float y, android.graphics.Paint paint)

        Graphics2D g = getGraphics2d();
        
        g = (Graphics2D)g.create();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        g.setFont(paint.getFont());
        
        // set the color. because this only handles RGB we have to handle the alpha separately
        g.setColor(new Color(paint.getColor()));
        int alpha = paint.getAlpha();
        float falpha = alpha / 255.f;
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, falpha));

        // Paint.TextAlign indicates how the text is positioned relative to X.
        // LEFT is the default and there's nothing to do.
        if (paint.getTextAlign() != Align.LEFT) {
            float m = paint.measureText(text, index, count);
            if (paint.getTextAlign() == Align.CENTER) {
                x -= m / 2;
            } else if (paint.getTextAlign() == Align.RIGHT) {
                x -= m;
            }
        }
        
        g.drawChars(text, index, count, (int)x, (int)y);
        
        g.dispose();
    
public voiddrawText(java.lang.CharSequence text, int start, int end, float x, float y, android.graphics.Paint paint)

        drawText(text.toString().toCharArray(), start, end - start, x, y, paint);
    
public voiddrawText(java.lang.String text, float x, float y, android.graphics.Paint paint)

        drawText(text.toCharArray(), 0, text.length(), x, y, paint);
    
public voiddrawText(java.lang.String text, int start, int end, float x, float y, android.graphics.Paint paint)

        drawText(text.toCharArray(), start, end - start, x, y, paint);
    
public voiddrawTextOnPath(char[] text, int index, int count, android.graphics.Path path, float offset, float offset2, android.graphics.Paint paint)

        // TODO Auto-generated method stub
        super.drawTextOnPath(text, index, count, path, offset, offset2, paint);
    
public voiddrawTextOnPath(java.lang.String text, android.graphics.Path path, float offset, float offset2, android.graphics.Paint paint)

        // TODO Auto-generated method stub
        super.drawTextOnPath(text, path, offset, offset2, paint);
    
public voiddrawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset, float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices, int indexOffset, int indexCount, android.graphics.Paint paint)

        // TODO Auto-generated method stub
        super.drawVertices(mode, vertexCount, verts, vertOffset, texs, texOffset, colors, colorOffset,
                indices, indexOffset, indexCount, paint);
    
public voidfinalize()

        // pass
    
public booleangetClipBounds(android.graphics.Rect bounds)
Retrieve the clip bounds, returning true if they are non-empty.

param
bounds Return the clip bounds here. If it is null, ignore it but still return true if the current clip is non-empty.
return
true if the current clip is non-empty.

        Rectangle rect = getGraphics2d().getClipBounds();
        if (rect != null) {
            bounds.left = rect.x;
            bounds.top = rect.y;
            bounds.right = rect.x + rect.width;
            bounds.bottom = rect.y + rect.height;
            return true;
        }
        return false;
    
public android.graphics.DrawFiltergetDrawFilter()

        // TODO Auto-generated method stub
        return super.getDrawFilter();
    
public javax.microedition.khronos.opengles.GLgetGL()

        // TODO Auto-generated method stub
        return super.getGL();
    
java.awt.Graphics2DgetGraphics2d()

        return mGraphicsStack.peek();
    
public intgetHeight()

        return mBufferedImage.getHeight();
    
public java.awt.image.BufferedImagegetImage()

        return mBufferedImage;
    
public android.graphics.MatrixgetMatrix()

        // TODO Auto-generated method stub
        return super.getMatrix();
    
public voidgetMatrix(android.graphics.Matrix ctm)

        // TODO Auto-generated method stub
        super.getMatrix(ctm);
    
private java.awt.Graphics2DgetNewGraphics(android.graphics.Paint paint, java.awt.Graphics2D g)
Creates a new {@link Graphics2D} based on the {@link Paint} parameters.

The object must be disposed ({@link Graphics2D#dispose()}) after being used.

        // make new one
        g = (Graphics2D)g.create();
        g.setColor(new Color(paint.getColor()));
        int alpha = paint.getAlpha();
        float falpha = alpha / 255.f;
        
        Xfermode xfermode = paint.getXfermode();
        if (xfermode instanceof PorterDuffXfermode) {
            PorterDuff.Mode mode = ((PorterDuffXfermode)xfermode).getMode();
            
            setModeInGraphics(mode, g, falpha);
        } else {
            if (mLogger != null && xfermode != null) {
                mLogger.warning(String.format(
                        "Xfermode '%1$s' is not supported in the Layout Editor.",
                        xfermode.getClass().getCanonicalName()));
            }
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, falpha));
        }
        
        Shader shader = paint.getShader();
        if (shader instanceof LinearGradient) {
            g.setPaint(((LinearGradient)shader).getPaint());
        } else {
            if (mLogger != null && shader != null) {
                mLogger.warning(String.format(
                        "Shader '%1$s' is not supported in the Layout Editor.",
                        shader.getClass().getCanonicalName()));
            }
        }
        
        return g;
    
public intgetSaveCount()

        return mGraphicsStack.size() - 1;
    
public intgetWidth()

        return mBufferedImage.getWidth();
    
public booleanisOpaque()

        // TODO Auto-generated method stub
        return super.isOpaque();
    
public booleanquickReject(android.graphics.RectF rect, EdgeType type)

        return false;
    
public booleanquickReject(android.graphics.Path path, EdgeType type)

        return false;
    
public booleanquickReject(float left, float top, float right, float bottom, EdgeType type)

        return false;
    
public voidrestore()

        mGraphicsStack.pop();
    
public voidrestoreToCount(int saveCount)

        while (mGraphicsStack.size() > saveCount) {
            mGraphicsStack.pop();
        }
    
public voidrotate(float degrees, float px, float py)

        if (degrees != 0) {
            Graphics2D g = getGraphics2d();
            g.translate(px, py);
            g.rotate(Math.toRadians(degrees));
            g.translate(-px, -py);
        }
    
public voidrotate(float degrees)

        getGraphics2d().rotate(Math.toRadians(degrees));
    
public intsave()

        Graphics2D g = (Graphics2D)getGraphics2d().create();
        mGraphicsStack.push(g);
        
        return mGraphicsStack.size() - 1;
    
public intsave(int saveFlags)

        // For now we ignore saveFlags
        return save();
    
public intsaveLayer(float left, float top, float right, float bottom, android.graphics.Paint paint, int saveFlags)

        // TODO Auto-generated method stub
        return super.saveLayer(left, top, right, bottom, paint, saveFlags);
    
public intsaveLayer(android.graphics.RectF bounds, android.graphics.Paint paint, int saveFlags)

        // TODO Auto-generated method stub
        return super.saveLayer(bounds, paint, saveFlags);
    
public intsaveLayerAlpha(float left, float top, float right, float bottom, int alpha, int saveFlags)

        // TODO Auto-generated method stub
        return super.saveLayerAlpha(left, top, right, bottom, alpha, saveFlags);
    
public intsaveLayerAlpha(android.graphics.RectF bounds, int alpha, int saveFlags)

        // TODO Auto-generated method stub
        return super.saveLayerAlpha(bounds, alpha, saveFlags);
    
public voidscale(float sx, float sy, float px, float py)

        Graphics2D g = getGraphics2d();
        g.translate(px, py);
        g.scale(sx, sy);
        g.translate(-px, -py);
    
public voidscale(float sx, float sy)

        getGraphics2d().scale(sx, sy);
    
public voidsetBitmap(android.graphics.Bitmap bitmap)

        // TODO Auto-generated method stub
        super.setBitmap(bitmap);
    
public voidsetDrawFilter(android.graphics.DrawFilter filter)

        // TODO Auto-generated method stub
        super.setDrawFilter(filter);
    
public voidsetMatrix(android.graphics.Matrix matrix)

        // since SetMatrix *replaces* all the other transformation, we have to restore/save
        restore();
        save();

        // get the new current graphics
        Graphics2D g = getGraphics2d();
        
        // and apply the matrix
        g.setTransform(matrix.getTransform());
        
        if (mLogger != null && matrix.hasPerspective()) {
            mLogger.warning("android.graphics.Canvas#setMatrix(android.graphics.Matrix) only supports affine transformations in the Layout Editor.");
        }
    
private voidsetModeInGraphics(PorterDuff.Mode mode, java.awt.Graphics2D g, float falpha)

        switch (mode) {
            case CLEAR:
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, falpha));
                break;
            case DARKEN:
                break;
            case DST:
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.DST, falpha));
                break;
            case DST_ATOP:
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_ATOP, falpha));
                break;
            case DST_IN:
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN, falpha));
                break;
            case DST_OUT:
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_OUT, falpha));
                break;
            case DST_OVER:
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_OVER, falpha));
                break;
            case LIGHTEN:
                break;
            case MULTIPLY:
                break;
            case SCREEN:
                break;
            case SRC:
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, falpha));
                break;
            case SRC_ATOP:
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, falpha));
                break;
            case SRC_IN:
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, falpha));
                break;
            case SRC_OUT:
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OUT, falpha));
                break;
            case SRC_OVER:
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, falpha));
                break;
            case XOR:
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.XOR, falpha));
                break;
        }
    
public voidsetViewport(int width, int height)

        // TODO Auto-generated method stub
        super.setViewport(width, height);
    
public voidskew(float sx, float sy)

        // TODO Auto-generated method stub
        super.skew(sx, sy);
    
public voidtranslate(float dx, float dy)

        getGraphics2d().translate(dx, dy);