Example usage for android.graphics.drawable LayerDrawable LayerDrawable

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

Introduction

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

Prototype

public LayerDrawable(@NonNull Drawable[] layers) 

Source Link

Document

Creates a new layer drawable with the list of specified layers.

Usage

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

    // 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: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);/* www . j  ava  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:com.ksharkapps.musicnow.ui.activities.SearchActivity.java

public void changeActionBarColor(int newColor) {

    int color = newColor != 0 ? newColor : SettingsActivity.getActionBarColor(this);
    Drawable colorDrawable = new ColorDrawable(color);
    Drawable bottomDrawable = getResources().getDrawable(R.drawable.transparent_overlay);
    LayerDrawable ld = new LayerDrawable(new Drawable[] { colorDrawable, bottomDrawable });

    if (oldBackground == null) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            ld.setCallback(drawableCallback);
        } else {//www.jav a 2  s  .co  m
            actionBar.setBackgroundDrawable(colorDrawable);
        }

    } else {
        TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, ld });
        // workaround for broken ActionBarContainer drawable handling on
        // pre-API 17 builds
        // https://github.com/android/platform_frameworks_base/commit/a7cc06d82e45918c37429a59b14545c6a57db4e4
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            td.setCallback(drawableCallback);
        } else {
            actionBar.setBackgroundDrawable(td);
        }
        td.startTransition(200);
    }

    oldBackground = ld;

    // http://stackoverflow.com/questions/11002691/actionbar-setbackgrounddrawable-nulling-background-from-thread-handler
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowTitleEnabled(true);

}

From source file:zemin.notification.NotificationView.java

private void updateContentBackground() {
    Drawable background = mBackground;//from  w  ww  . j  av a2 s .c om
    if (hasState(CONTENT_BACKGROUND_CHANGED)) {
        if (mContentBackground != null) {
            if (mShadowEnabled) {
                final Drawable[] layers = new Drawable[] {
                        getResources().getDrawable(android.R.drawable.dialog_holo_light_frame),
                        mContentBackground, };
                background = new LayerDrawable(layers);

                int l, t, r, b;
                l = t = r = b = 0;

                for (int i = 0, size = layers.length; i < size; i++) {
                    Rect rect = new Rect();
                    layers[i].getPadding(rect);

                    l += rect.left;
                    t += rect.top;
                    r += rect.right;
                    b += rect.bottom;
                }
                setContentPadding(l, t, r, b);

            } else {

                background = mContentBackground;

                Rect rect = new Rect();
                background.getPadding(rect);
                setContentPadding(rect.left, rect.top, rect.right, rect.bottom);
            }

            if (mContentBackground instanceof GradientDrawable) {
                GradientDrawable b = (GradientDrawable) mContentBackground;
                b.setCornerRadius(mCornerRadius);
                b.setStroke(mStrokeWidth, mStrokeColor);

                if (mBackgroundColorAnimator != null) {
                    mBackgroundColorAnimator.cancel();
                    mBackgroundColorAnimator = null;
                }

                ColorProperty colorProperty = new ColorProperty();
                mBackgroundColorAnimator = ObjectAnimator.ofObject(b, colorProperty, new ArgbEvaluator(), 0, 0);
                mBackgroundColorAnimator.setDuration(mBackgroundTransitionTime);

                if (mBackgroundAlphaAnimator != null) {
                    mBackgroundAlphaAnimator.cancel();
                    mBackgroundAlphaAnimator = null;
                }

                AlphaProperty alphaProperty = new AlphaProperty();
                mBackgroundAlphaAnimator = ObjectAnimator.ofObject(b, alphaProperty, new IntEvaluator(), 0, 0);
                mBackgroundAlphaAnimator.setDuration(mBackgroundTransitionTime);
            }
        }

        clearState(CONTENT_BACKGROUND_CHANGED);
        clearState(CONTENT_BACKGROUND_CHANGED_MINOR);

    } else if (hasState(CONTENT_BACKGROUND_CHANGED_MINOR)) {
        if (mContentBackground instanceof GradientDrawable) {
            GradientDrawable b = (GradientDrawable) mContentBackground;
            b.setStroke(mStrokeWidth, mStrokeColor);
            mBackgroundColorAnimator.setDuration(mBackgroundTransitionTime);
            mBackgroundAlphaAnimator.setDuration(mBackgroundTransitionTime);
        }

        clearState(CONTENT_BACKGROUND_CHANGED_MINOR);
    }

    mBackground = background;
    mContentView.setBackground(background);
}

From source file:com.taobao.weex.ui.component.WXComponent.java

protected BorderDrawable getOrCreateBorder() {
    if (mBackgroundDrawable == null) {
        Drawable backgroundDrawable = mHost.getBackground();
        WXViewUtils.setBackGround(mHost, null);
        mBackgroundDrawable = new BorderDrawable();
        if (backgroundDrawable == null) {
            WXViewUtils.setBackGround(mHost, mBackgroundDrawable);
        } else {/*  w w  w.j a  va  2 s. c o m*/
            //TODO Not strictly clip according to background-clip:border-box
            WXViewUtils.setBackGround(mHost,
                    new LayerDrawable(new Drawable[] { mBackgroundDrawable, backgroundDrawable }));
        }
    }
    return mBackgroundDrawable;
}

From source file:org.mythdroid.remote.TVRemote.java

private void setupProgressBar(int max, int progress, Program prog, float fps) {

    pBar.setMax(max);/*from   w w  w .j a va2  s . c  om*/
    pBar.setProgress(progress);

    pBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (!fromUser || seekBar.getMax() <= 0)
                return;
            if (mddMgr != null) {
                FrontendLocation loc = null;
                try {
                    synchronized (feLock) {
                        loc = feMgr.getLoc();
                    }
                } catch (IOException e) {
                    ErrUtil.err(ctx, e);
                    return;
                } catch (IllegalArgumentException e) {
                    return;
                }
                if (loc.end <= 0)
                    return;
                progress = (loc.end * progress) / 1000;
            }
            try {
                synchronized (feLock) {
                    feMgr.seekTo(progress);
                }
            } catch (IOException e) {
                ErrUtil.err(ctx, e);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

    });

    if (prog == null || endTime <= 0 || fps == 0)
        return;

    ArrayList<Commercial> cuts = prog.getCutList(feMgr.addr);
    if (cuts.isEmpty())
        return;

    Drawable[] layers = new Drawable[2];
    layers[0] = pBar.getProgressDrawable();
    Rect bounds = layers[0].getBounds();
    layers[1] = new CutListDrawable(cuts, fps, endTime, bounds);
    LayerDrawable lg = new LayerDrawable(layers);
    lg.setBounds(bounds);
    pBar.setProgressDrawable(lg);

}

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());
            }/*  w w w  .  j  a v a  2 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();
        }
    }
}

From source file:com.ksharkapps.musicnow.ui.activities.AudioPlayerActivity.java

public static Drawable createDrawable(Context context, int color) {

    ShapeDrawable shape = new ShapeDrawable();
    shape.getPaint().setStyle(Style.FILL);
    shape.setIntrinsicHeight(1);/*from ww  w.  j a v  a 2  s. c o m*/
    shape.getPaint().setColor(context.getResources().getColor(R.color.transparent));

    shape.getPaint().setStyle(Style.STROKE);
    shape.getPaint().setStrokeWidth(4);
    shape.getPaint().setColor(color);

    ShapeDrawable shapeD = new ShapeDrawable();
    shapeD.getPaint().setStyle(Style.FILL);
    shapeD.getPaint().setColor(color);
    ClipDrawable clipDrawable = new ClipDrawable(shapeD, Gravity.LEFT, ClipDrawable.HORIZONTAL);

    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { clipDrawable, shape });
    return layerDrawable;
}

From source file:com.ksharkapps.musicnow.ui.activities.AudioPlayerActivity.java

private void updateSeekbartheme(int color) {

    /*// w  w w. j av  a2s . com
     * ShapeDrawable pgDrawable = new ShapeDrawable(new RectShape());
     * 
     * // Sets the progressBar color pgDrawable.getPaint().setColor(color);
     * 
     * // Adds the drawable to your progressBar ClipDrawable progress = new
     * ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
     * mProgress.setProgressDrawable(progress);
     * mProgress.setBackgroundDrawable
     * (getResources().getDrawable(R.drawable.progress_shape));
     */

    ShapeDrawable shape = new ShapeDrawable();
    shape.getPaint().setStyle(Style.FILL);
    shape.getPaint().setColor(getResources().getColor(R.color.transparent_black));

    ShapeDrawable shapeD = new ShapeDrawable();
    shapeD.getPaint().setStyle(Style.FILL);
    shapeD.getPaint().setColor(getResources().getColor(R.color.white));
    ClipDrawable clipDrawable = new ClipDrawable(shapeD, Gravity.LEFT, ClipDrawable.HORIZONTAL);

    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { shape, clipDrawable });
    mProgress.setProgressDrawable(layerDrawable);

}

From source file:com.eleybourn.bookcatalogue.utils.Utils.java

/**
  * Reuse of bitmaps in tiled backgrounds is a known cause of problems:
  *      http://stackoverflow.com/questions/4077487/background-image-not-repeating-in-android-layout
 * So we avoid reusing them./* w w  w .  j ava  2  s.c  om*/
 * 
 * This seems to have become further messed up in 4.1 so now, we just created them manually. No references,
 * but the old cleanup method (see below for cleanupTiledBackground()) no longer works. Since it effectively
 * un-cached the background, we just create it here.
 * 
 * The main problem with this approach is that the style is defined in code rather than XML.
 *
 * @param a         Activity context
 * @param bright   Flag indicating if background should be 'bright'
 * 
 * @return         Background Drawable
 */
public static Drawable makeTiledBackground(boolean bright) {
    // Storage for the layers
    Drawable[] drawables = new Drawable[2];
    // Get the BG image, put in tiled drawable
    Bitmap b = BitmapFactory.decodeResource(BookCatalogueApp.context.getResources(), R.drawable.books_bg);
    BitmapDrawable bmD = new BitmapDrawable(BookCatalogueApp.context.getResources(), b);
    bmD.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
    // Add to layers
    drawables[0] = bmD;

    // Set up the gradient colours based on 'bright' setting
    int[] colours = new int[3];
    if (bright) {
        colours[0] = Color.argb(224, 0, 0, 0);
        colours[1] = Color.argb(208, 0, 0, 0);
        colours[2] = Color.argb(48, 0, 0, 0);
    } else {
        colours[0] = Color.argb(255, 0, 0, 0);
        colours[1] = Color.argb(208, 0, 0, 0);
        colours[2] = Color.argb(160, 0, 0, 0);
    }

    // Create a gradient and add to layers
    GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colours);
    drawables[1] = gd;

    // Make the layers and we are done.
    LayerDrawable ll = new LayerDrawable(drawables);
    ll.setDither(true);

    return ll;
}