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) 

Source Link

Document

Fill the entire canvas' bitmap (restricted to the current clip) with the specified color, using srcover porterduff mode.

Usage

From source file:Main.java

public static Bitmap getColorPreviewBitmap(final Context context, final int color, final boolean border) {
    if (context == null)
        return null;
    final float density = context.getResources().getDisplayMetrics().density;
    final int width = (int) (32 * density), height = (int) (32 * density);

    final Bitmap bm = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bm);

    final int rectrangleSize = (int) (density * 5);
    final int numRectanglesHorizontal = (int) Math.ceil(width / rectrangleSize);
    final int numRectanglesVertical = (int) Math.ceil(height / rectrangleSize);
    final Rect r = new Rect();
    boolean verticalStartWhite = true;
    for (int i = 0; i <= numRectanglesVertical; i++) {

        boolean isWhite = verticalStartWhite;
        for (int j = 0; j <= numRectanglesHorizontal; j++) {

            r.top = i * rectrangleSize;/*from w  w w .j a  v  a2s  .co  m*/
            r.left = j * rectrangleSize;
            r.bottom = r.top + rectrangleSize;
            r.right = r.left + rectrangleSize;
            final Paint paint = new Paint();
            paint.setColor(isWhite ? Color.WHITE : Color.GRAY);

            canvas.drawRect(r, paint);

            isWhite = !isWhite;
        }

        verticalStartWhite = !verticalStartWhite;

    }
    canvas.drawColor(color);
    if (border) {
        final Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(1f * density);
        final float[] points = new float[] { 0, 0, width, 0, 0, 0, 0, height, width, 0, width, height, 0,
                height, width, height };
        canvas.drawLines(points, paint);
    }
    return bm;
}

From source file:com.tr4android.support.extension.drawable.ColorTransitionDrawable.java

@Override
public void draw(Canvas canvas) {
    canvas.drawColor(mColor);
}

From source file:de.bahnhoefe.deutschlands.bahnhofsfotos.notification.NearbyBahnhofWithPhotoNotificationManager.java

/**
 * Construct a Bitmap object from a given drawable resource ID.
 *
 * @param id the resource ID denoting a drawable resource
 * @return the Bitmap. May be null.//from   w  w w.j  ava  2s .c om
 */
@Nullable
private Bitmap getBitmapFromResource(int id) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Drawable vectorDrawable = context.getDrawable(id);
        int h = 400;
        int w = 400;
        vectorDrawable.setBounds(0, 0, w, h);
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bm);
        canvas.drawColor(Color.WHITE);
        vectorDrawable.draw(canvas);
        return bm;
    } else
        return null;
}

From source file:com.jamessimshaw.wallpaperhelper.datasources.WallpaperFileHelper.java

public Wallpaper loadWallpaper(Context context, boolean isLandscape) {
    Bitmap bitmap;/*from ww  w .ja  v a  2  s  . c om*/
    String filename = isLandscape ? LANDSCAPE_FILENAME : PORTRAIT_FILENAME;

    try {
        //TODO: Set this up to be an async task
        FileInputStream fileInputStream = context.openFileInput(filename);
        bitmap = BitmapFactory.decodeStream(fileInputStream);
        fileInputStream.close();
    } catch (IOException e) {
        //Set a default Wallpaper of a black screen and send a notification
        Bitmap.Config bitmapOptions = Bitmap.Config.ARGB_8888;
        bitmap = Bitmap.createBitmap(100, 100, bitmapOptions);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.BLACK);

        Intent settingsIntent = new Intent(context, SettingsActivity.class);
        PendingIntent notificationIntent = PendingIntent.getActivity(context, 0, settingsIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        String notificationString = isLandscape ? context.getString(R.string.noLandscapeImage)
                : context.getString(R.string.noPortraitImage);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_notification_small)
                .setContentTitle(context.getString(R.string.app_name)).setContentText(notificationString)
                .setContentIntent(notificationIntent);

        int notificationId = 1;
        NotificationManager manager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(notificationId, notificationBuilder.build());
    }

    return new Wallpaper(bitmap, isLandscape);
}

From source file:im.neon.util.VectorUtils.java

/**
 * Create an avatar bitmap from a text.//from   w  w  w .ja  v a 2s .c  o m
 *
 * @param backgroundColor the background color.
 * @param text            the text to display.
 * @param pixelsSide      the avatar side in pixels
 * @return the generated bitmap
 */
private static Bitmap createAvatar(int backgroundColor, String text, int pixelsSide) {
    android.graphics.Bitmap.Config bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;

    Bitmap bitmap = Bitmap.createBitmap(pixelsSide, pixelsSide, bitmapConfig);
    Canvas canvas = new Canvas(bitmap);

    canvas.drawColor(backgroundColor);

    // prepare the text drawing
    Paint textPaint = new Paint();
    textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    textPaint.setColor(Color.WHITE);
    // the text size is proportional to the avatar size.
    // by default, the avatar size is 42dp, the text size is 28 dp (not sp because it has to be fixed).
    textPaint.setTextSize(pixelsSide * 2 / 3);

    // get its size
    Rect textBounds = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), textBounds);

    // draw the text in center
    canvas.drawText(text, (canvas.getWidth() - textBounds.width() - textBounds.left) / 2,
            (canvas.getHeight() + textBounds.height() - textBounds.bottom) / 2, textPaint);

    // Return the avatar
    return bitmap;
}

From source file:io.plaidapp.ui.widget.CutoutTextView.java

private void createBitmap() {
    if (cutout != null && !cutout.isRecycled()) {
        cutout.recycle();//from   w ww.j  ava  2 s. c om
    }
    cutout = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    cutout.setHasAlpha(true);
    Canvas cutoutCanvas = new Canvas(cutout);
    cutoutCanvas.drawColor(foregroundColor);

    // this is the magic  Clear mode punches out the bitmap
    textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    cutoutCanvas.drawText(text, textX, textY, textPaint);
}

From source file:com.linkbubble.util.Util.java

/**
 * Returns a bitmap suitable for the all apps view.
 *//*from w w  w.  j  a v a  2 s . c  om*/
static Bitmap createIconBitmap(Drawable icon, Context context) {
    synchronized (sCanvas) { // we share the statics :-(
        if (sIconWidth == -1) {
            initStatics(context);
        }

        int width = sIconWidth;
        int height = sIconHeight;

        if (icon instanceof PaintDrawable) {
            PaintDrawable painter = (PaintDrawable) icon;
            painter.setIntrinsicWidth(width);
            painter.setIntrinsicHeight(height);
        } else if (icon instanceof BitmapDrawable) {
            // Ensure the bitmap has a density.
            BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
            }
        }
        int sourceWidth = icon.getIntrinsicWidth();
        int sourceHeight = icon.getIntrinsicHeight();
        if (sourceWidth > 0 && sourceHeight > 0) {
            // There are intrinsic sizes.
            if (width < sourceWidth || height < sourceHeight) {
                // It's too big, scale it down.
                final float ratio = (float) sourceWidth / sourceHeight;
                if (sourceWidth > sourceHeight) {
                    height = (int) (width / ratio);
                } else if (sourceHeight > sourceWidth) {
                    width = (int) (height * ratio);
                }
            } else if (sourceWidth < width && sourceHeight < height) {
                // Don't scale up the icon
                width = sourceWidth;
                height = sourceHeight;
            }
        }

        // no intrinsic size --> use default size
        int textureWidth = sIconTextureWidth;
        int textureHeight = sIconTextureHeight;

        final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888);
        final Canvas canvas = sCanvas;
        canvas.setBitmap(bitmap);

        final int left = (textureWidth - width) / 2;
        final int top = (textureHeight - height) / 2;

        @SuppressWarnings("all") // suppress dead code warning
        final boolean debug = false;
        if (debug) {
            // draw a big box for the icon for debugging
            canvas.drawColor(sColors[sColorIndex]);
            if (++sColorIndex >= sColors.length)
                sColorIndex = 0;
            Paint debugPaint = new Paint();
            debugPaint.setColor(0xffcccc00);
            canvas.drawRect(left, top, left + width, top + height, debugPaint);
        }

        sOldBounds.set(icon.getBounds());
        icon.setBounds(left, top, left + width, top + height);
        icon.draw(canvas);
        icon.setBounds(sOldBounds);
        canvas.setBitmap(null);

        return bitmap;
    }
}

From source file:com.lithidsw.wallbox.app.colorwall.ColorWallFrag.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_apply:
        try {//from   w  w  w.j a v a 2 s.c o m
            Bitmap bit = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
            Canvas can = new Canvas(bit);
            can.drawColor(mColor);
            wm.setBitmap(bit);
            Toast.makeText(fa, "Current color wallpaper applied!", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(fa, "Current color wallpaper had an error!", Toast.LENGTH_SHORT).show();
        }
        return true;
    default:
        return false;
    }
}

From source file:net.simno.dmach.ui.view.SettingView.java

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(backgroundColor);
    if (!TextUtils.isEmpty(hText)) {
        canvas.drawText(hText, originX, originY, textPaint);
    }//from ww  w .  ja v  a  2s.  c o  m
    if (!TextUtils.isEmpty(vText)) {
        canvas.drawTextOnPath(vText, path, hOffset, vOffset, textPaint);
    }
    canvas.drawCircle(x, y, circleRadius, shapePaint);
}

From source file:com.hippo.ehviewer.widget.GalleryGuideView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(mBgColor);
    if (0 == mStep) {
        canvas.drawLines(mPoints, mPaint);
    }/*from   w  w w .  ja va 2s. c  o  m*/
}