Example usage for android.graphics.drawable LayerDrawable draw

List of usage examples for android.graphics.drawable LayerDrawable draw

Introduction

In this page you can find the example usage for android.graphics.drawable LayerDrawable draw.

Prototype

@Override
    public void draw(Canvas canvas) 

Source Link

Usage

From source file:com.webonise.gardenIt.MyGcmListenerService.java

private Bitmap getBitmapDrawable() {
    Drawable d = getResources().getDrawable(R.drawable.drawable_notification_icon);
    Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

    LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.drawable.drawable_notification_icon);
    ld.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    ld.draw(new Canvas(b));

    return b;// w w  w . j  a v a2 s  . co m
}

From source file:org.ciasaboark.tacere.manager.NotificationManagerWrapper.java

private Bitmap combineDrawablesToBitmap(final Drawable backgroundImage, final BitmapDrawable overlayImage,
        int width, int height) {

    Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    // Create a canvas, that will draw on to canvasBitmap.
    Canvas imageCanvas = new Canvas(canvasBitmap);

    // Draw the image to our canvas
    backgroundImage.setBounds(0, 0, width, height);
    backgroundImage.draw(imageCanvas);/*from  w  w  w .  j a v a  2 s  .  c  o m*/
    overlayImage.setBounds(0, 0, (int) (overlayImage.getIntrinsicWidth() * 0.5),
            (int) (overlayImage.getIntrinsicHeight() * 0.5));
    final int scaleWidth = 24;
    final int scaleHeight = 24;
    final float scaleWidthPercent = ((float) scaleWidth) / overlayImage.getIntrinsicWidth();
    final float scaleHeightPercent = ((float) scaleHeight) / overlayImage.getIntrinsicHeight();
    ScaleDrawable scaleDrawable = new ScaleDrawable(overlayImage, 0, scaleWidthPercent, scaleHeightPercent);
    Drawable scaledOverlay = scaleDrawable.getDrawable();
    scaledOverlay.setBounds(0, 0, width, height);
    scaledOverlay.draw(imageCanvas);

    // Combine background and text to a LayerDrawable
    LayerDrawable layerDrawable = new LayerDrawable(
            new Drawable[] { backgroundImage, new BitmapDrawable(canvasBitmap) });
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    layerDrawable.setBounds(0, 0, width, height);
    layerDrawable.draw(new Canvas(newBitmap));
    return newBitmap;
}

From source file:org.ciasaboark.tacere.manager.NotificationManagerWrapper.java

private Bitmap createMarkerIcon(Drawable backgroundImage, String text, int width, int height) {

    Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    // Create a canvas, that will draw on to canvasBitmap.
    Canvas imageCanvas = new Canvas(canvasBitmap);

    // Draw the image to our canvas
    backgroundImage.draw(imageCanvas);/*from  www.  j  a  va 2  s.  com*/

    // Set up the paint for use with our Canvas
    TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG);
    textPaint.setTextAlign(TextPaint.Align.CENTER);
    textPaint.setTypeface(Typeface.DEFAULT);
    textPaint.setTextSize(100f);
    textPaint.setColor(context.getResources().getColor(android.R.color.white));

    int xPos = (imageCanvas.getWidth() / 2);
    int yPos = (int) ((imageCanvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2));
    Rect r = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), r);
    //        yPos += (Math.abs(r.height()))/2;

    // Draw the text on top of our image
    imageCanvas.drawText(text, xPos, yPos, textPaint);

    // Combine background and text to a LayerDrawable
    LayerDrawable layerDrawable = new LayerDrawable(
            new Drawable[] { backgroundImage, new BitmapDrawable(canvasBitmap) });
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    layerDrawable.setBounds(0, 0, width, height);
    layerDrawable.draw(new Canvas(newBitmap));
    return newBitmap;
}

From source file:cw.kop.autobackground.LiveWallpaperService.java

@SuppressLint("NewApi")
private void notifyChangeImage() {

    if (normalView != null && bigView != null && notificationManager != null) {
        int drawable = AppSettings.getNotificationIcon();

        if (AppSettings.getNotificationTitle().equals("Location") && FileHandler.getBitmapLocation() != null) {
            normalView.setTextViewText(R.id.notification_title, FileHandler.getBitmapLocation());
            normalView.setOnClickPendingIntent(R.id.notification_title, pendingToastIntent);
            if (Build.VERSION.SDK_INT >= 16) {
                bigView.setTextViewText(R.id.notification_big_title, FileHandler.getBitmapLocation());
                bigView.setOnClickPendingIntent(R.id.notification_big_title, pendingToastIntent);
            } else {
                notificationBuilder.setContentTitle(FileHandler.getBitmapLocation());
            }/*from w  ww.  j  a  v  a2 s . c o  m*/
        } else {
            normalView.setOnClickPendingIntent(R.id.notification_title, null);
            bigView.setOnClickPendingIntent(R.id.notification_big_title, null);
        }

        if (AppSettings.getNotificationSummary().equals("Location")
                && FileHandler.getBitmapLocation() != null) {
            normalView.setTextViewText(R.id.notification_summary, FileHandler.getBitmapLocation());
            normalView.setOnClickPendingIntent(R.id.notification_summary, pendingToastIntent);
            if (Build.VERSION.SDK_INT >= 16) {
                bigView.setTextViewText(R.id.notification_big_summary, FileHandler.getBitmapLocation());
                bigView.setOnClickPendingIntent(R.id.notification_big_summary, pendingToastIntent);
            } else {
                notificationBuilder.setContentText(FileHandler.getBitmapLocation());
            }
        } else {
            normalView.setOnClickPendingIntent(R.id.notification_summary, null);
            bigView.setOnClickPendingIntent(R.id.notification_big_summary, null);
        }

        if (AppSettings.useNotificationIconFile() && AppSettings.getNotificationIconFile() != null) {

            File image = new File(AppSettings.getNotificationIconFile());

            if (image.exists() && image.isFile()) {
                Picasso.with(LiveWallpaperService.this).load(image)
                        .resizeDimen(android.R.dimen.notification_large_icon_width,
                                android.R.dimen.notification_large_icon_height)
                        .centerCrop().into(targetIcon);
            }

        } else if (drawable == R.drawable.ic_photo_white_24dp) {
            if (FileHandler.getCurrentBitmapFile() == null) {
                return;
            }

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        if (!AppSettings.useHighQuality()) {
                            options.inPreferredConfig = Bitmap.Config.RGB_565;
                        }

                        if (AppSettings.useHighResolutionNotificationIcon()) {

                            options.inJustDecodeBounds = true;
                            BitmapFactory.decodeFile(FileHandler.getCurrentBitmapFile().getAbsolutePath(),
                                    options);

                            int bitWidth = options.outWidth;
                            int bitHeight = options.outHeight;
                            int minWidth = LiveWallpaperService.this.getResources()
                                    .getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
                            int minHeight = LiveWallpaperService.this.getResources()
                                    .getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
                            int sampleSize = 1;
                            if (bitHeight > minHeight || bitWidth > minWidth) {

                                final int halfHeight = bitHeight / 2;
                                final int halfWidth = bitWidth / 2;
                                while ((halfHeight / sampleSize) > minHeight
                                        && (halfWidth / sampleSize) > minWidth) {
                                    sampleSize *= 2;
                                }
                            }
                            options.inJustDecodeBounds = false;
                            options.inSampleSize = sampleSize;
                        } else {
                            options.inSampleSize = NOTIFICATION_ICON_SAMPLE_SIZE;
                        }
                        Log.i(TAG, "sampleSize: " + options.inSampleSize);
                        Bitmap bitmap = BitmapFactory
                                .decodeFile(FileHandler.getCurrentBitmapFile().getAbsolutePath(), options);
                        targetIcon.onBitmapLoaded(bitmap, null);
                    } catch (OutOfMemoryError e) {
                        if (AppSettings.useToast()) {
                            Toast.makeText(LiveWallpaperService.this, "Out of memory error", Toast.LENGTH_SHORT)
                                    .show();
                        }
                    }
                }
            }).start();
        } else {
            if (pinned && AppSettings.usePinIndicator()) {
                Drawable[] layers = new Drawable[2];
                layers[0] = LiveWallpaperService.this.getResources().getDrawable(drawable);
                layers[1] = LiveWallpaperService.this.getResources().getDrawable(R.drawable.pin_overlay);

                LayerDrawable layerDrawable = new LayerDrawable(layers);

                Bitmap mutableBitmap = Bitmap.createBitmap(layers[0].getIntrinsicWidth(),
                        layers[0].getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(mutableBitmap);
                layerDrawable.setBounds(0, 0, layers[0].getIntrinsicWidth(), layers[0].getIntrinsicHeight());
                layerDrawable.draw(canvas);

                normalView.setImageViewBitmap(R.id.notification_icon, mutableBitmap);
                if (Build.VERSION.SDK_INT >= 16) {
                    bigView.setImageViewBitmap(R.id.notification_big_icon, mutableBitmap);
                } else {
                    notificationBuilder.setLargeIcon(mutableBitmap);
                }
            } else {
                normalView.setImageViewResource(R.id.notification_icon, drawable);
                bigView.setImageViewResource(R.id.notification_big_icon, drawable);
            }
            pushNotification();
        }
    }
}