use of android.graphics.Canvas in project AndroidPicker by gzu-liyujiang.
the class ConvertUtils method toBitmap.
/**
* 将Drawable转换为Bitmap
* 参考:http://kylines.iteye.com/blog/1660184
*/
public static Bitmap toBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof ColorDrawable) {
//color
Bitmap bitmap = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(((ColorDrawable) drawable).getColor());
return bitmap;
} else if (drawable instanceof NinePatchDrawable) {
//.9.png
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
return null;
}
use of android.graphics.Canvas in project grafika by google.
the class MultiSurfaceActivity method drawBouncingCircle.
/**
* Clears the surface, then draws a filled circle with a shadow.
* <p>
* Similar to drawCircleSurface(), but the position changes based on the value of "i".
*/
private void drawBouncingCircle(Surface surface, int i) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
Canvas canvas = surface.lockCanvas(null);
try {
Trace.beginSection("drawBouncingCircle");
Trace.beginSection("drawColor");
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
// drawColor
Trace.endSection();
int width = canvas.getWidth();
int height = canvas.getHeight();
int radius, x, y;
if (width < height) {
// portrait
radius = width / 4;
x = width / 4 + ((width / 2 * i) / BOUNCE_STEPS);
y = height * 3 / 4;
} else {
// landscape
radius = height / 4;
x = width * 3 / 4;
y = height / 4 + ((height / 2 * i) / BOUNCE_STEPS);
}
paint.setShadowLayer(radius / 4 + 1, 0, 0, Color.RED);
canvas.drawCircle(x, y, radius, paint);
// drawBouncingCircle
Trace.endSection();
} finally {
surface.unlockCanvasAndPost(canvas);
}
}
use of android.graphics.Canvas in project material-components-android by material-components.
the class CollapsingTextHelper method ensureExpandedTexture.
private void ensureExpandedTexture() {
if (mExpandedTitleTexture != null || mExpandedBounds.isEmpty() || TextUtils.isEmpty(mTextToDraw)) {
return;
}
calculateOffsets(0f);
mTextureAscent = mTextPaint.ascent();
mTextureDescent = mTextPaint.descent();
final int w = Math.round(mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length()));
final int h = Math.round(mTextureDescent - mTextureAscent);
if (w <= 0 || h <= 0) {
// If the width or height are 0, return
return;
}
mExpandedTitleTexture = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(mExpandedTitleTexture);
c.drawText(mTextToDraw, 0, mTextToDraw.length(), 0, h - mTextPaint.descent(), mTextPaint);
if (mTexturePaint == null) {
// Make sure we have a paint
mTexturePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
}
}
use of android.graphics.Canvas in project material-components-android by material-components.
the class TestUtils method assertAllPixelsOfColor.
public static void assertAllPixelsOfColor(String failMessagePrefix, @NonNull Drawable drawable, int drawableWidth, int drawableHeight, boolean callSetBounds, @ColorInt int color, Rect checkArea, int allowedComponentVariance, boolean throwExceptionIfFails) {
// Create a bitmap
Bitmap bitmap = Bitmap.createBitmap(drawableWidth, drawableHeight, Bitmap.Config.ARGB_8888);
// Create a canvas that wraps the bitmap
Canvas canvas = new Canvas(bitmap);
if (callSetBounds) {
// Configure the drawable to have bounds that match the passed size
drawable.setBounds(0, 0, drawableWidth, drawableHeight);
}
// And ask the drawable to draw itself to the canvas / bitmap
drawable.draw(canvas);
try {
int[] rowPixels = new int[drawableWidth];
final int firstRow = checkArea != null ? checkArea.top : 0;
final int lastRow = checkArea != null ? checkArea.bottom : drawableHeight - 1;
final int firstCol = checkArea != null ? checkArea.left : 0;
final int lastCol = checkArea != null ? checkArea.right : drawableWidth - 1;
final int expectedAlpha = Color.alpha(color);
final int expectedRed = Color.red(color);
final int expectedGreen = Color.green(color);
final int expectedBlue = Color.blue(color);
for (int row = firstRow; row <= lastRow; row++) {
bitmap.getPixels(rowPixels, 0, drawableWidth, 0, row, drawableWidth, 1);
for (int column = firstCol; column <= lastCol; column++) {
int sourceAlpha = Color.alpha(rowPixels[column]);
int sourceRed = Color.red(rowPixels[column]);
int sourceGreen = Color.green(rowPixels[column]);
int sourceBlue = Color.blue(rowPixels[column]);
int varianceAlpha = Math.abs(sourceAlpha - expectedAlpha);
int varianceRed = Math.abs(sourceRed - expectedRed);
int varianceGreen = Math.abs(sourceGreen - expectedGreen);
int varianceBlue = Math.abs(sourceBlue - expectedBlue);
boolean isColorMatch = (varianceAlpha <= allowedComponentVariance) && (varianceRed <= allowedComponentVariance) && (varianceGreen <= allowedComponentVariance) && (varianceBlue <= allowedComponentVariance);
if (!isColorMatch) {
String mismatchDescription = failMessagePrefix + ": expected all drawable colors to be [" + expectedAlpha + "," + expectedRed + "," + expectedGreen + "," + expectedBlue + "] but at position (" + row + "," + column + ") found [" + sourceAlpha + "," + sourceRed + "," + sourceGreen + "," + sourceBlue + "]";
if (throwExceptionIfFails) {
throw new RuntimeException(mismatchDescription);
} else {
Assert.fail(mismatchDescription);
}
}
}
}
} finally {
bitmap.recycle();
}
}
use of android.graphics.Canvas in project scissors by lyft.
the class Utils method asBitmap.
public static Bitmap asBitmap(Drawable drawable, int minWidth, int minHeight) {
final Rect tmpRect = new Rect();
drawable.copyBounds(tmpRect);
if (tmpRect.isEmpty()) {
tmpRect.set(0, 0, Math.max(minWidth, drawable.getIntrinsicWidth()), Math.max(minHeight, drawable.getIntrinsicHeight()));
drawable.setBounds(tmpRect);
}
Bitmap bitmap = Bitmap.createBitmap(tmpRect.width(), tmpRect.height(), Bitmap.Config.ARGB_8888);
drawable.draw(new Canvas(bitmap));
return bitmap;
}
Aggregations