Example usage for android.graphics Canvas drawColor

List of usage examples for android.graphics Canvas drawColor

Introduction

In this page you can find the example usage for android.graphics Canvas drawColor.

Prototype

public void drawColor(@ColorInt int color, @NonNull PorterDuff.Mode mode) 

Source Link

Document

Fill the entire canvas' bitmap (restricted to the current clip) with the specified color and porter-duff xfermode.

Usage

From source file:Main.java

public static void clearCanvas(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
}

From source file:Main.java

public static void fillTransparent(Canvas canvas) {
    canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
}

From source file:Main.java

/**
 * This method updates the opacity of the bitmap.
 *
 * @param bitmap  The bitmap./*from   ww w.j a  v a  2s . c o m*/
 * @param opacity The value of alpha.
 * @return The bitmap after adjusting the opacity.
 */
public static Bitmap adjustOpacity(Bitmap bitmap, int opacity) {

    Bitmap mutableBitmap = bitmap.isMutable() ? bitmap : bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    int color = (opacity & 0xFF) << 24;
    canvas.drawColor(color, PorterDuff.Mode.DST_IN);
    return mutableBitmap;
}

From source file:Main.java

public static synchronized void cleansurfaceview(SurfaceHolder postureholder, SurfaceView posturesurface) {
    Canvas canvas = postureholder
            .lockCanvas(new Rect(0, 0, posturesurface.getWidth(), posturesurface.getHeight()));
    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    postureholder.unlockCanvasAndPost(canvas);
    Canvas canvasa = postureholder
            .lockCanvas(new Rect(0, 0, posturesurface.getWidth(), posturesurface.getHeight()));
    canvasa.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    postureholder.unlockCanvasAndPost(canvasa);
}

From source file:Main.java

public static void clearCanvas(Canvas canvas) {
    if (USE_DRAWCOLOR_TO_CLEAR_CANVAS) {
        if (USE_DRAWCOLOR_MODE_CLEAR) {
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        } else {//w  w  w .  j  av  a  2s. com
            canvas.drawColor(Color.TRANSPARENT);
        }
    } else {
        RECT.set(0, 0, canvas.getWidth(), canvas.getHeight());
        clearCanvas(canvas, RECT);
    }
}

From source file:com.dm.wallpaper.board.helpers.DrawableHelper.java

@Nullable
public static Drawable getDefaultImage(@NonNull Context context, @DrawableRes int res, @ColorInt int color,
        int padding) {
    try {/* w  ww.java  2s .  co m*/
        Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, res);
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        Bitmap tintedBitmap = Bitmap.createBitmap(bitmap.getWidth() + padding, bitmap.getHeight() + padding,
                Bitmap.Config.ARGB_8888);
        Canvas tintedCanvas = new Canvas(tintedBitmap);
        int background = ColorHelper.getAttributeColor(context, R.attr.card_background);
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        paint.setAntiAlias(true);
        tintedCanvas.drawColor(background, PorterDuff.Mode.ADD);
        tintedCanvas.drawBitmap(bitmap, (tintedCanvas.getWidth() - bitmap.getWidth()) / 2,
                (tintedCanvas.getHeight() - bitmap.getHeight()) / 2, paint);
        return new BitmapDrawable(context.getResources(), tintedBitmap);
    } catch (Exception | OutOfMemoryError e) {
        return null;
    }
}

From source file:com.dm.material.dashboard.candybar.helpers.DrawableHelper.java

@Nullable
public static Drawable getDefaultImage(@NonNull Context context, @DrawableRes int res) {
    try {/*from w w  w  .  j  av a  2s .  c o m*/
        int color = ColorHelper.getAttributeColor(context, android.R.attr.textColorSecondary);
        int padding = context.getResources().getDimensionPixelSize(R.dimen.default_image_padding);

        Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, res);
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        Bitmap tintedBitmap = Bitmap.createBitmap(bitmap.getWidth() + padding, bitmap.getHeight() + padding,
                Bitmap.Config.ARGB_8888);
        Canvas tintedCanvas = new Canvas(tintedBitmap);
        int background = ColorHelper.getAttributeColor(context, R.attr.card_background);
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        paint.setAntiAlias(true);
        tintedCanvas.drawColor(background, PorterDuff.Mode.ADD);
        tintedCanvas.drawBitmap(bitmap, (tintedCanvas.getWidth() - bitmap.getWidth()) / 2,
                (tintedCanvas.getHeight() - bitmap.getHeight()) / 2, paint);
        return new BitmapDrawable(context.getResources(), tintedBitmap);
    } catch (Exception | OutOfMemoryError e) {
        return null;
    }
}

From source file:com.facebook.imagepipeline.animated.impl.AnimatedDrawableCachingBackendImpl.java

/**
 * Copies the source bitmap for the specified frame and caches it.
 *
 * @param frameNumber the frame number//from w w w  . j a va  2  s  .  c om
 * @param sourceBitmap the rendered bitmap to be cached (after copying)
 */
private void copyAndCacheBitmapDuringRendering(int frameNumber, Bitmap sourceBitmap) {
    CloseableReference<Bitmap> destBitmapReference = obtainBitmapInternal();
    try {
        Canvas copyCanvas = new Canvas(destBitmapReference.get());
        copyCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SRC);
        copyCanvas.drawBitmap(sourceBitmap, 0, 0, null);
        maybeCacheRenderedBitmap(frameNumber, destBitmapReference);
    } finally {
        destBitmapReference.close();
    }
}

From source file:com.bigpigs.fragments.SearchFragment.java

private Bitmap getMarkerBitmapFromView(@DrawableRes int resId) {

    View customMarkerView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.custom_marker, null);
    ImageView markerImageView = (ImageView) customMarkerView.findViewById(R.id.marker_icon);
    markerImageView.setImageResource(resId);
    customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
    customMarkerView.buildDrawingCache();
    Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(),
            customMarkerView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
    Drawable drawable = customMarkerView.getBackground();
    if (drawable != null)
        drawable.draw(canvas);//from  w w  w  .ja  va2s  . c  om
    customMarkerView.draw(canvas);
    return returnedBitmap;
}

From source file:com.android.leanlauncher.WidgetPreviewLoader.java

public Bitmap getPreview(final Object o) {
    final String name = getObjectName(o);
    final String packageName = getObjectPackage(o);
    // check if the package is valid
    synchronized (sInvalidPackages) {
        boolean packageValid = !sInvalidPackages.contains(packageName);
        if (!packageValid) {
            return null;
        }/* w  ww  .j  a  va 2  s  . co  m*/
    }
    synchronized (mLoadedPreviews) {
        // check if it exists in our existing cache
        if (mLoadedPreviews.containsKey(name)) {
            WeakReference<Bitmap> bitmapReference = mLoadedPreviews.get(name);
            Bitmap bitmap = bitmapReference.get();
            if (bitmap != null) {
                return bitmap;
            }
        }
    }

    Bitmap unusedBitmap = null;
    synchronized (mUnusedBitmaps) {
        // not in cache; we need to load it from the db
        while (unusedBitmap == null && mUnusedBitmaps.size() > 0) {
            Bitmap candidate = mUnusedBitmaps.remove(0).get();
            if (candidate != null && candidate.isMutable() && candidate.getWidth() == mPreviewBitmapWidth
                    && candidate.getHeight() == mPreviewBitmapHeight) {
                unusedBitmap = candidate;
            }
        }
        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 = 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;
    }
}