use of android.graphics.Canvas in project MaterializeYourApp by antoniolg.
the class CircleTransform method transform.
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
return bitmap;
}
use of android.graphics.Canvas in project Launcher3 by chislon.
the class Workspace method onDragStartedWithItem.
public void onDragStartedWithItem(PendingAddItemInfo info, Bitmap b, boolean clipAlpha) {
final Canvas canvas = new Canvas();
int[] size = estimateItemSize(info.spanX, info.spanY, info, false);
// The outline is used to visualize where the item will land if dropped
mDragOutline = createDragOutline(b, canvas, DRAG_BITMAP_PADDING, size[0], size[1], clipAlpha);
}
use of android.graphics.Canvas in project Launcher3 by chislon.
the class WidgetPreviewLoader method getPreview.
public Bitmap getPreview(final Object o) {
final String name = getObjectName(o);
final String packageName = getObjectPackage(o);
// check if the package is valid
boolean packageValid = true;
synchronized (sInvalidPackages) {
packageValid = !sInvalidPackages.contains(packageName);
}
if (!packageValid) {
return null;
}
if (packageValid) {
synchronized (mLoadedPreviews) {
// check if it exists in our existing cache
if (mLoadedPreviews.containsKey(name) && mLoadedPreviews.get(name).get() != null) {
return mLoadedPreviews.get(name).get();
}
}
}
Bitmap unusedBitmap = null;
synchronized (mUnusedBitmaps) {
// not in cache; we need to load it from the db
while ((unusedBitmap == null || !unusedBitmap.isMutable() || unusedBitmap.getWidth() != mPreviewBitmapWidth || unusedBitmap.getHeight() != mPreviewBitmapHeight) && mUnusedBitmaps.size() > 0) {
unusedBitmap = mUnusedBitmaps.remove(0).get();
}
if (unusedBitmap != null) {
final Canvas c = mCachedAppWidgetPreviewCanvas.get();
c.setBitmap(unusedBitmap);
c.drawColor(0, PorterDuff.Mode.CLEAR);
c.setBitmap(null);
}
}
if (unusedBitmap == null) {
unusedBitmap = Bitmap.createBitmap(mPreviewBitmapWidth, mPreviewBitmapHeight, Bitmap.Config.ARGB_8888);
}
Bitmap preview = null;
if (packageValid) {
preview = readFromDb(name, unusedBitmap);
}
if (preview != null) {
synchronized (mLoadedPreviews) {
mLoadedPreviews.put(name, new WeakReference<Bitmap>(preview));
}
return preview;
} else {
// it's not in the db... we need to generate it
final Bitmap generatedPreview = generatePreview(o, unusedBitmap);
preview = generatedPreview;
if (preview != unusedBitmap) {
throw new RuntimeException("generatePreview is not recycling the bitmap " + o);
}
synchronized (mLoadedPreviews) {
mLoadedPreviews.put(name, new WeakReference<Bitmap>(preview));
}
// write to db on a thread pool... this can be done lazily and improves the performance
// of the first time widget previews are loaded
new AsyncTask<Void, Void, Void>() {
public Void doInBackground(Void... args) {
writeToDb(o, generatedPreview);
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
return preview;
}
}
use of android.graphics.Canvas in project Launcher3 by chislon.
the class WidgetPreviewLoader method renderDrawableToBitmap.
private static void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h, float scale) {
if (bitmap != null) {
Canvas c = new Canvas(bitmap);
c.scale(scale, scale);
Rect oldBounds = d.copyBounds();
d.setBounds(x, y, x + w, y + h);
d.draw(c);
// Restore the bounds
d.setBounds(oldBounds);
c.setBitmap(null);
}
}
use of android.graphics.Canvas in project photo-picker-plus-android by chute.
the class RippleView method getCircleBitmap.
private Bitmap getCircleBitmap(final int radius) {
final Bitmap output = Bitmap.createBitmap(originBitmap.getWidth(), originBitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect((int) (x - radius), (int) (y - radius), (int) (x + radius), (int) (y + radius));
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(x, y, radius, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(originBitmap, rect, rect, paint);
return output;
}
Aggregations