BridgeCanvaspublic 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 boolean | clipPath(android.graphics.Path path, android.graphics.Region.Op op)
// TODO Auto-generated method stub
return super.clipPath(path, op);
| public boolean | clipPath(android.graphics.Path path)
// TODO Auto-generated method stub
return super.clipPath(path);
| public boolean | clipRect(float left, float top, float right, float bottom, android.graphics.Region.Op op)
return clipRect(left, top, right, bottom);
| public boolean | clipRect(float left, float top, float right, float bottom)
getGraphics2d().clipRect((int)left, (int)top, (int)(right-left), (int)(bottom-top));
return true;
| public boolean | clipRect(int left, int top, int right, int bottom)
getGraphics2d().clipRect(left, top, right-left, bottom-top);
return true;
| public boolean | clipRect(android.graphics.Rect rect, android.graphics.Region.Op op)
return clipRect(rect.left, rect.top, rect.right, rect.bottom);
| public boolean | clipRect(android.graphics.Rect rect)
return clipRect(rect.left, rect.top, rect.right, rect.bottom);
| public boolean | clipRect(android.graphics.RectF rect, android.graphics.Region.Op op)
return clipRect(rect.left, rect.top, rect.right, rect.bottom);
| public boolean | clipRect(android.graphics.RectF rect)
return clipRect(rect.left, rect.top, rect.right, rect.bottom);
| public boolean | clipRegion(android.graphics.Region region, android.graphics.Region.Op op)
// TODO Auto-generated method stub
return super.clipRegion(region, op);
| public boolean | clipRegion(android.graphics.Region region)
// TODO Auto-generated method stub
return super.clipRegion(region);
| public void | concat(android.graphics.Matrix matrix)
// TODO Auto-generated method stub
super.concat(matrix);
| void | dispose()
while (mGraphicsStack.size() > 0) {
mGraphicsStack.pop().dispose();
}
| private final void | doDrawRect(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 void | drawARGB(int a, int r, int g, int b)
drawColor(a << 24 | r << 16 | g << 8 | b, PorterDuff.Mode.SRC_OVER);
| public void | drawArc(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 void | drawBitmap(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 void | drawBitmap(android.graphics.Bitmap bitmap, android.graphics.Matrix matrix, android.graphics.Paint paint)
throw new UnsupportedOperationException();
| public void | drawBitmap(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 void | drawBitmap(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 void | drawBitmap(int[] colors, int offset, int stride, int x, int y, int width, int height, boolean hasAlpha, android.graphics.Paint paint)
throw new UnsupportedOperationException();
| private void | drawBitmap(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 void | drawBitmapMesh(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 void | drawCircle(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 void | drawColor(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 void | drawColor(int color)
drawColor(color, PorterDuff.Mode.SRC_OVER);
| public void | drawLine(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 void | drawLines(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 void | drawLines(float[] pts, android.graphics.Paint paint)
drawLines(pts, 0, pts.length, paint);
| public void | drawOval(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 void | drawPaint(android.graphics.Paint paint)
drawColor(paint.getColor());
| public void | drawPath(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 void | drawPicture(android.graphics.Picture picture, android.graphics.Rect dst)
// TODO Auto-generated method stub
super.drawPicture(picture, dst);
| public void | drawPicture(android.graphics.Picture picture, android.graphics.RectF dst)
// TODO Auto-generated method stub
super.drawPicture(picture, dst);
| public void | drawPicture(android.graphics.Picture picture)
// TODO Auto-generated method stub
super.drawPicture(picture);
| public void | drawPoint(float x, float y, android.graphics.Paint paint)
// TODO Auto-generated method stub
super.drawPoint(x, y, paint);
| public void | drawPoints(float[] pts, int offset, int count, android.graphics.Paint paint)
// TODO Auto-generated method stub
super.drawPoints(pts, offset, count, paint);
| public void | drawPoints(float[] pts, android.graphics.Paint paint)
// TODO Auto-generated method stub
super.drawPoints(pts, paint);
| public void | drawPosText(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 void | drawPosText(java.lang.String text, float[] pos, android.graphics.Paint paint)
// TODO Auto-generated method stub
super.drawPosText(text, pos, paint);
| public void | drawRGB(int r, int g, int b)
drawColor(0xFF << 24 | r << 16 | g << 8 | b, PorterDuff.Mode.SRC_OVER);
| public void | drawRect(android.graphics.RectF rect, android.graphics.Paint paint)
doDrawRect((int)rect.left, (int)rect.top, (int)rect.width(), (int)rect.height(), paint);
| public void | drawRect(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 void | drawRect(android.graphics.Rect r, android.graphics.Paint paint)
doDrawRect(r.left, r.top, r.width(), r.height(), paint);
| public void | drawRoundRect(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 void | drawText(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 void | drawText(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 void | drawText(java.lang.String text, float x, float y, android.graphics.Paint paint)
drawText(text.toCharArray(), 0, text.length(), x, y, paint);
| public void | drawText(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 void | drawTextOnPath(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 void | drawTextOnPath(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 void | drawVertices(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 void | finalize()
// pass
| public boolean | getClipBounds(android.graphics.Rect bounds)Retrieve the clip bounds, returning true if they are 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.DrawFilter | getDrawFilter()
// TODO Auto-generated method stub
return super.getDrawFilter();
| public javax.microedition.khronos.opengles.GL | getGL()
// TODO Auto-generated method stub
return super.getGL();
| java.awt.Graphics2D | getGraphics2d()
return mGraphicsStack.peek();
| public int | getHeight()
return mBufferedImage.getHeight();
| public java.awt.image.BufferedImage | getImage()
return mBufferedImage;
| public android.graphics.Matrix | getMatrix()
// TODO Auto-generated method stub
return super.getMatrix();
| public void | getMatrix(android.graphics.Matrix ctm)
// TODO Auto-generated method stub
super.getMatrix(ctm);
| private java.awt.Graphics2D | getNewGraphics(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 int | getSaveCount()
return mGraphicsStack.size() - 1;
| public int | getWidth()
return mBufferedImage.getWidth();
| public boolean | isOpaque()
// TODO Auto-generated method stub
return super.isOpaque();
| public boolean | quickReject(android.graphics.RectF rect, EdgeType type)
return false;
| public boolean | quickReject(android.graphics.Path path, EdgeType type)
return false;
| public boolean | quickReject(float left, float top, float right, float bottom, EdgeType type)
return false;
| public void | restore()
mGraphicsStack.pop();
| public void | restoreToCount(int saveCount)
while (mGraphicsStack.size() > saveCount) {
mGraphicsStack.pop();
}
| public void | rotate(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 void | rotate(float degrees)
getGraphics2d().rotate(Math.toRadians(degrees));
| public int | save()
Graphics2D g = (Graphics2D)getGraphics2d().create();
mGraphicsStack.push(g);
return mGraphicsStack.size() - 1;
| public int | save(int saveFlags)
// For now we ignore saveFlags
return save();
| public int | saveLayer(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 int | saveLayer(android.graphics.RectF bounds, android.graphics.Paint paint, int saveFlags)
// TODO Auto-generated method stub
return super.saveLayer(bounds, paint, saveFlags);
| public int | saveLayerAlpha(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 int | saveLayerAlpha(android.graphics.RectF bounds, int alpha, int saveFlags)
// TODO Auto-generated method stub
return super.saveLayerAlpha(bounds, alpha, saveFlags);
| public void | scale(float sx, float sy, float px, float py)
Graphics2D g = getGraphics2d();
g.translate(px, py);
g.scale(sx, sy);
g.translate(-px, -py);
| public void | scale(float sx, float sy)
getGraphics2d().scale(sx, sy);
| public void | setBitmap(android.graphics.Bitmap bitmap)
// TODO Auto-generated method stub
super.setBitmap(bitmap);
| public void | setDrawFilter(android.graphics.DrawFilter filter)
// TODO Auto-generated method stub
super.setDrawFilter(filter);
| public void | setMatrix(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 void | setModeInGraphics(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 void | setViewport(int width, int height)
// TODO Auto-generated method stub
super.setViewport(width, height);
| public void | skew(float sx, float sy)
// TODO Auto-generated method stub
super.skew(sx, sy);
| public void | translate(float dx, float dy)
getGraphics2d().translate(dx, dy);
|
|