use of android.graphics.Canvas in project platform_frameworks_base by android.
the class IconTest method testAsync.
@SmallTest
public void testAsync() throws Exception {
final Bitmap bit1 = ((BitmapDrawable) getContext().getDrawable(R.drawable.landscape)).getBitmap();
final File dir = getContext().getExternalFilesDir(null);
final File file1 = new File(dir, "async-original.png");
bit1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file1));
final Icon im1 = Icon.createWithFilePath(file1.toString());
final HandlerThread thd = new HandlerThread("testAsync");
thd.start();
final Handler h = new Handler(thd.getLooper());
L(TAG, "asyncTest: dispatching load to thread: " + thd);
im1.loadDrawableAsync(mContext, new Icon.OnDrawableLoadedListener() {
@Override
public void onDrawableLoaded(Drawable draw1) {
L(TAG, "asyncTest: thread: loading drawable");
L(TAG, "asyncTest: thread: loaded: %dx%d", draw1.getIntrinsicWidth(), draw1.getIntrinsicHeight());
final Bitmap test1 = Bitmap.createBitmap(draw1.getIntrinsicWidth(), draw1.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
draw1.setBounds(0, 0, test1.getWidth(), test1.getHeight());
draw1.draw(new Canvas(test1));
try {
test1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(dir, "async-test.png")));
} catch (java.io.FileNotFoundException ex) {
fail("couldn't create test file: " + ex);
}
if (!equalBitmaps(bit1, test1)) {
findBitmapDifferences(bit1, test1);
fail("testAsync: file1 differs, check " + dir);
}
}
}, h);
L(TAG, "asyncTest: awaiting result");
// ;_;
Thread.sleep(500);
assertTrue("async-test.png does not exist!", new File(dir, "async-test.png").exists());
L(TAG, "asyncTest: done");
}
use of android.graphics.Canvas in project platform_frameworks_base by android.
the class RippleDrawable method updateMaskShaderIfNeeded.
/**
* @return whether we need to use a mask
*/
private void updateMaskShaderIfNeeded() {
if (mHasValidMask) {
return;
}
final int maskType = getMaskType();
if (maskType == MASK_UNKNOWN) {
return;
}
mHasValidMask = true;
final Rect bounds = getBounds();
if (maskType == MASK_NONE || bounds.isEmpty()) {
if (mMaskBuffer != null) {
mMaskBuffer.recycle();
mMaskBuffer = null;
mMaskShader = null;
mMaskCanvas = null;
}
mMaskMatrix = null;
mMaskColorFilter = null;
return;
}
// Ensure we have a correctly-sized buffer.
if (mMaskBuffer == null || mMaskBuffer.getWidth() != bounds.width() || mMaskBuffer.getHeight() != bounds.height()) {
if (mMaskBuffer != null) {
mMaskBuffer.recycle();
}
mMaskBuffer = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ALPHA_8);
mMaskShader = new BitmapShader(mMaskBuffer, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mMaskCanvas = new Canvas(mMaskBuffer);
} else {
mMaskBuffer.eraseColor(Color.TRANSPARENT);
}
if (mMaskMatrix == null) {
mMaskMatrix = new Matrix();
} else {
mMaskMatrix.reset();
}
if (mMaskColorFilter == null) {
mMaskColorFilter = new PorterDuffColorFilter(0, PorterDuff.Mode.SRC_IN);
}
// Draw the appropriate mask anchored to (0,0).
final int left = bounds.left;
final int top = bounds.top;
mMaskCanvas.translate(-left, -top);
if (maskType == MASK_EXPLICIT) {
drawMask(mMaskCanvas);
} else if (maskType == MASK_CONTENT) {
drawContent(mMaskCanvas);
}
mMaskCanvas.translate(left, top);
}
use of android.graphics.Canvas in project platform_frameworks_base by android.
the class IconTest method testWithFile.
@SmallTest
public void testWithFile() throws Exception {
final Bitmap bit1 = ((BitmapDrawable) getContext().getDrawable(R.drawable.landscape)).getBitmap();
final File dir = getContext().getExternalFilesDir(null);
final File file1 = new File(dir, "file1-original.png");
bit1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file1));
final Icon im1 = Icon.createWithFilePath(file1.toString());
final Drawable draw1 = im1.loadDrawable(mContext);
final Bitmap test1 = Bitmap.createBitmap(draw1.getIntrinsicWidth(), draw1.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
draw1.setBounds(0, 0, test1.getWidth(), test1.getHeight());
draw1.draw(new Canvas(test1));
test1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(dir, "file1-test.png")));
if (!equalBitmaps(bit1, test1)) {
findBitmapDifferences(bit1, test1);
fail("testWithFile: file1 differs, check " + dir);
}
}
use of android.graphics.Canvas in project platform_frameworks_base by android.
the class TestDocumentsProvider method openDocumentThumbnail.
@Override
public AssetFileDescriptor openDocumentThumbnail(String docId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException {
if (LAG)
lagUntilCanceled(signal);
if (THUMB_WEDGE)
wedgeUntilCanceled(signal);
if (THUMB_CRASH)
System.exit(12);
final Bitmap bitmap = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
final Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawColor(Color.RED);
canvas.drawLine(0, 0, 32, 32, paint);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 50, bos);
final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
try {
final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createReliablePipe();
new AsyncTask<Object, Object, Object>() {
@Override
protected Object doInBackground(Object... params) {
final FileOutputStream fos = new FileOutputStream(fds[1].getFileDescriptor());
try {
Streams.copy(bis, fos);
} catch (IOException e) {
throw new RuntimeException(e);
}
IoUtils.closeQuietly(fds[1]);
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return new AssetFileDescriptor(fds[0], 0, AssetFileDescriptor.UNKNOWN_LENGTH);
} catch (IOException e) {
throw new FileNotFoundException(e.getMessage());
}
}
use of android.graphics.Canvas in project platform_frameworks_base by android.
the class UserIconDrawable method rebake.
private void rebake() {
mInvalidated = false;
if (mBitmap == null || (mUserDrawable == null && mUserIcon == null)) {
return;
}
final Canvas canvas = new Canvas(mBitmap);
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
if (mUserDrawable != null) {
mUserDrawable.draw(canvas);
} else if (mUserIcon != null) {
int saveId = canvas.save();
canvas.concat(mIconMatrix);
canvas.drawCircle(mUserIcon.getWidth() * 0.5f, mUserIcon.getHeight() * 0.5f, mIntrinsicRadius, mIconPaint);
canvas.restoreToCount(saveId);
}
if (mFrameColor != null) {
mFramePaint.setColor(mFrameColor.getColorForState(getState(), Color.TRANSPARENT));
}
if ((mFrameWidth + mFramePadding) > 0.001f) {
float radius = mDisplayRadius - mPadding - mFrameWidth * 0.5f;
canvas.drawCircle(getBounds().exactCenterX(), getBounds().exactCenterY(), radius, mFramePaint);
}
if ((mBadge != null) && (mBadgeRadius > 0.001f)) {
final float badgeDiameter = mBadgeRadius * 2f;
final float badgeTop = mBitmap.getHeight() - badgeDiameter;
float badgeLeft = mBitmap.getWidth() - badgeDiameter;
mBadge.setBounds((int) badgeLeft, (int) badgeTop, (int) (badgeLeft + badgeDiameter), (int) (badgeTop + badgeDiameter));
final float borderRadius = mBadge.getBounds().width() * 0.5f + mBadgeMargin;
canvas.drawCircle(badgeLeft + mBadgeRadius, badgeTop + mBadgeRadius, borderRadius, mClearPaint);
mBadge.draw(canvas);
}
}
Aggregations