Example usage for android.graphics.drawable LayerDrawable setBounds

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

Introduction

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

Prototype

public void setBounds(int left, int top, int right, int bottom) 

Source Link

Document

Specify a bounding rectangle for the Drawable.

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;//from w  w  w.  j a  v  a  2 s  .c  o m
}

From source file:com.nononsenseapps.feeder.model.ImageTextLoader.java

/**
 *
 * @return a Drawable with a youtube logo in the center
 *//*from w  ww. ja  va 2  s . co m*/
protected Drawable getYoutubeThumb(final com.nononsenseapps.text.VideoTagHunter.Video video) {
    Drawable[] layers = new Drawable[2];

    int w1, h1;
    try {
        //final Bitmap b = p.load(video.imageurl).tag(ImageTextLoader.this).get();
        final Bitmap b = g.load(video.imageurl).asBitmap().fitCenter().into(maxSize.x, maxSize.y).get();
        //final Point newSize = scaleImage(b.getWidth(), b.getHeight());
        w1 = b.getWidth();
        h1 = b.getHeight();
        final BitmapDrawable d = new BitmapDrawable(getContext().getResources(), b);
        Log.d("JONASYOUTUBE", "Bounds: " + d.getIntrinsicWidth() + ", " + "" + d.getIntrinsicHeight() + " vs "
                + w1 + ", " + h1);
        // Settings bounds later
        //d.setBounds(0, 0, w1, h1);
        // Set in layer
        layers[0] = d;
    } catch (InterruptedException | ExecutionException e) {
        Log.e("JONASYOUTUBE", "" + e.getMessage());
        throw new NullPointerException(e.getLocalizedMessage());
    }

    // Add layer with play icon
    final Drawable playicon = getContext().getResources().getDrawable(R.drawable.youtube_icon);
    // 20% size, in middle
    int w2 = playicon.getIntrinsicWidth();
    int h2 = playicon.getIntrinsicHeight();

    final double ratio = ((double) h2) / ((double) w2);

    // Start with width which is known
    final double relSize = 0.2;
    w2 = (int) (relSize * w1);
    final int left = (int) (((double) (w1 - w2)) / 2.0);
    // Then height is simple
    h2 = (int) (ratio * w2);
    final int top = (int) (((double) (h1 - h2)) / 2.0);

    Log.d("JONASYOUTUBE", "l t w h: " + left + " " + top + " " + w2 + " " + h2);

    // And add to layer
    layers[1] = playicon;
    final LayerDrawable ld = new LayerDrawable(layers);
    // Need to set bounds on outer drawable first as it seems to override
    // child bounds
    ld.setBounds(0, 0, w1, h1);
    // Now set smaller bounds on youtube icon
    playicon.setBounds(left, top, left + w2, top + h2);
    return ld;
}

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 2s. c  om
    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   ww  w.j av a2s.  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 w w  .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();
        }
    }
}