Example usage for android.graphics.drawable GradientDrawable GradientDrawable

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

Introduction

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

Prototype

private GradientDrawable(@NonNull GradientState state, @Nullable Resources res) 

Source Link

Document

Creates a new themed GradientDrawable based on the specified constant state.

Usage

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

private void processImage() {

    Bitmap image = ((BitmapDrawable) mAlbumArt.getDrawable()).getBitmap();

    ColorArt colorArt = new ColorArt(image);

    // get the colors
    colorArt.getBackgroundColor();//w  ww .j  av a 2  s  .  c o m
    colorArt.getPrimaryColor();
    colorArt.getSecondaryColor();
    colorArt.getDetailColor();

    Bitmap imagen = ((BitmapDrawable) mAlbumArt.getDrawable()).getBitmap();
    long reds = 0L;
    long greens = 0L;
    long blues = 0L;
    int[] pixeles = new int[imagen.getWidth() * imagen.getHeight()];
    imagen.getPixels(pixeles, 0, imagen.getWidth(), 0, 0, imagen.getWidth(), imagen.getHeight());
    for (int cursor = 0; cursor < pixeles.length; cursor++) {
        reds += Color.red(pixeles[cursor]);
        greens += Color.green(pixeles[cursor]);
        blues += Color.blue(pixeles[cursor]);
    }
    long numPixels = imagen.getWidth() * imagen.getHeight();
    reds /= numPixels;
    greens /= numPixels;
    blues /= numPixels;

    Punto3D[] pixelesRef = { new Punto3D(255, 0, 0), new Punto3D(0, 255, 0), new Punto3D(0, 0, 255),
            new Punto3D(255, 255, 0), new Punto3D(0, 255, 255), new Punto3D(255, 0, 255), new Punto3D(0, 0, 0),
            new Punto3D(255, 255, 255) };
    Punto3D pixelActual = new Punto3D(reds, greens, blues);

    double[] distancias = { pixelActual.distancia(pixelesRef[0]), pixelActual.distancia(pixelesRef[1]),
            pixelActual.distancia(pixelesRef[2]), pixelActual.distancia(pixelesRef[3]),
            pixelActual.distancia(pixelesRef[4]), pixelActual.distancia(pixelesRef[5]),
            pixelActual.distancia(pixelesRef[6]), pixelActual.distancia(pixelesRef[7]) };
    String[] colors = { "Red", "Green", "Blue", "Yellow", "Cyan ", "Magenta", " Black ", "White" };

    double dist_minima = 255;
    int indice_minima = 0;
    for (int index = 0; index < distancias.length; index++) {
        if (distancias[index] <= dist_minima) {
            indice_minima = index;
            dist_minima = distancias[index];
        }
        Log.i("Distancias", "Distancias en pos " + index + ": " + distancias[index]);
    }

    GradientDrawable gd2 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {

            Color.rgb((int) pixelesRef[indice_minima].getX(), (int) pixelesRef[indice_minima].getY(),
                    (int) pixelesRef[indice_minima].getZ()),
            colorArt.getBackgroundColor(), colorArt.getDetailColor() });

    gd2.setCornerRadius(0f);

    player_bg.setBackgroundDrawable(gd2);

}

From source file:com.tiange.hz.wheelview.widget.WheelView.java

/**
 * Initializes resources/*w w  w .  j av a2s .c om*/
 */
private void initResourcesIfNecessary() {
    if (centerDrawable == null && wheelForeground != 0x0) {
        centerDrawable = ContextCompat.getDrawable(getContext(), wheelForeground);
    }

    if (topShadow == null) {
        topShadow = new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);
    }

    if (bottomShadow == null) {
        bottomShadow = new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);
    }

    if (wheelBackground != 0x0)
        setBackgroundResource(wheelBackground);
    if (wheelBackgroundColor != Color.TRANSPARENT)
        setBackgroundColor(wheelBackgroundColor);
}

From source file:kankan.wheel.widget.WheelView.java

/**
 * Initializes resources//from   ww  w .  j a  v  a 2 s  .  c  o m
 */
private void initResourcesIfNecessary() {
    if (centerDrawable == null) {
        centerDrawable = getContext().getResources().getDrawable(wheelForeground);
    }

    if (topShadow == null) {
        topShadow = new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);
    }

    if (bottomShadow == null) {
        bottomShadow = new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);
    }

    setBackgroundResource(wheelBackground);
}

From source file:fr.tvbarthel.apps.simpleweatherforcast.MainActivity.java

@SuppressLint("NewApi")
private void setGradientBackgroundColor(int currentPosition, float positionOffset) {
    final GradientDrawable g = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
            new int[] { getColor(currentPosition, positionOffset),
                    getColor(currentPosition, (float) Math.pow(positionOffset, 0.40)) });
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
        mRootView.setBackgroundDrawable(g);
    } else {//from  ww  w. jav a  2 s  .c  o  m
        mRootView.setBackground(g);
    }
}

From source file:org.alfresco.mobile.android.application.fragments.menu.MainMenuFragment.java

private void refresh() {
    if (currentAccount == null) {
        currentAccount = AccountsPreferences.getDefaultAccount(getActivity());
        if (currentAccount == null) {
            return;
        }//from ww  w.  j  a  v  a  2 s . c  om
    }

    List<AlfrescoAccount> list = AlfrescoAccountManager.retrieveAccounts(getActivity());
    // We add all extra parameters at the end of the list.
    if (currentAccount.getTypeId() == AlfrescoAccount.TYPE_ALFRESCO_CLOUD) {
        list.add(new AlfrescoAccount(AccountsAdapter.NETWORK_ITEM, getString(R.string.cloud_networks_switch),
                null, null, null, null, "0", null, "false"));
    }

    // We add profiles management option if at least there's 2 profiles
    // available
    if (configManager != null && configManager.getConfig(currentAccount.getId()) != null
            && configManager.getConfig(currentAccount.getId()).getProfiles().size() > 1) {
        list.add(new AlfrescoAccount(AccountsAdapter.PROFILES_ITEM, getString(R.string.profiles_switch), null,
                null, null, null, "0", null, "false"));
    }

    list.add(new AlfrescoAccount(AccountsAdapter.MANAGE_ITEM, getString(R.string.manage_accounts), null, null,
            null, null, "0", null, "false"));

    // Init the adapter and create the menu
    if (accountsAdapter == null) {
        accountsAdapter = new AccountsAdapter(getActivity(), list, R.layout.row_single_line, null);
    } else {
        accountsAdapter.clear();
        accountsAdapter.addAll(list);
    }

    accountsAdapter.setNotifyOnChange(false);

    ((TextView) accountsSpinnerButton.findViewById(R.id.accounts_spinner_title))
            .setText(currentAccount.getTitle());
    AccountsAdapter.displayAvatar(getActivity(), currentAccount, R.drawable.ic_account_light,
            ((ImageView) accountsSpinnerButton.findViewById(R.id.accounts_spinner_icon)));
    accountsSpinnerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (listPopupWindow != null) {
                listPopupWindow.dismiss();
                listPopupWindow = null;
            } else {
                listPopupWindow = new ListPopupWindow(getActivity());
                GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                        new int[] { 0xFF282828, 0xFF282828 });
                gd.setCornerRadius(0f);
                listPopupWindow.setBackgroundDrawable(gd);
                listPopupWindow.setAnchorView(accountsSpinnerButton);
                listPopupWindow.setAdapter(accountsAdapter);
                listPopupWindow.setOnItemClickListener(MainMenuFragment.this);
                listPopupWindow.setWidth(ListPopupWindow.WRAP_CONTENT);
                listPopupWindow.show();
            }
        }
    });

    if (OperationsFragment.canDisplay(getActivity(), currentAccount)) {
        show(R.id.menu_notifications);
        showOperationsMenu = true;
    } else {
        hide(R.id.menu_notifications);
        showOperationsMenu = false;
    }
    getActivity().invalidateOptionsMenu();
}

From source file:de.madvertise.android.sdk.MadView.java

/**
 * Draw the ad background for a text banner
 * //  ww  w. j a  va2  s .c o  m
 * @param canvas
 * @param rectangle
 * @param backgroundColor
 * @param textColor
 */
private void drawTextBannerBackground(Canvas canvas, Rect rectangle, int backgroundColor, int shineColor) {
    Paint paint = new Paint();
    paint.setColor(backgroundColor);
    paint.setAntiAlias(true);
    canvas.drawRect(rectangle, paint);

    int upperColor = Color.argb(GRADIENT_TOP_ALPHA, Color.red(shineColor), Color.green(shineColor),
            Color.blue(shineColor));
    int[] gradientColors = { upperColor, shineColor };
    GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
            gradientColors);

    int stop = (int) (rectangle.height() * GRADIENT_STOP) + rectangle.top;
    gradientDrawable.setBounds(rectangle.left, rectangle.top, rectangle.right, stop);
    gradientDrawable.draw(canvas);

    Rect shadowRect = new Rect(rectangle.left, stop, rectangle.right, rectangle.bottom);
    Paint shadowPaint = new Paint();
    shadowPaint.setColor(shineColor);
    canvas.drawRect(shadowRect, shadowPaint);
}

From source file:com.example.view.wheel.WheelView.java

/**
 * Initializes resources//from ww w  .  j ava2s .  com
 */
private void initResourcesIfNecessary() {
    if (itemsPaint == null) {
        itemsPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
        //itemsPaint.density = getResources().getDisplayMetrics().density;
        itemsPaint.setTextSize(BaseUtil.dp2px(context, TEXT_SIZE));
    }

    if (valuePaint == null) {
        valuePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG | Paint.DITHER_FLAG);
        //valuePaint.density = getResources().getDisplayMetrics().density;
        valuePaint.setTextSize(BaseUtil.dp2px(context, TEXT_SIZE));
        valuePaint.setColor(Color.BLUE);
        //   valuePaint.setShadowLayer(0.1f, 0, 0.1f, 0xFFC0C0C0);
    }
    if (linePaint == null) {
        linePaint = new Paint();
        linePaint.setColor(Color.parseColor("#FF6D4B"));
        linePaint.setStrokeWidth(10.0F);
    }
    if (centerDrawable == null) {
        centerDrawable = ContextCompat.getDrawable(context, R.drawable.wheel_val);
    }

    if (topShadow == null) {
        topShadow = new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);
    }

    if (bottomShadow == null) {
        bottomShadow = new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);
    }

    //setBackgroundResource(R.drawable.wheel_bg);
    setBackgroundColor(Color.WHITE);
}

From source file:de.madvertise.android.sdk.MadvertiseView.java

/**
 * Draw the ad background for a text banner
 *
 * @param canvas/*from   www.j  a  v  a2  s .com*/
 * @param rectangle
 * @param backgroundColor
 * @param mTextColor
 */
private void drawTextBannerBackground(final Canvas canvas, final Rect rectangle, final int backgroundColor,
        int shineColor) {
    Paint paint = new Paint();
    paint.setColor(backgroundColor);
    paint.setAntiAlias(true);
    canvas.drawRect(rectangle, paint);

    int upperColor = Color.argb(GRADIENT_TOP_ALPHA, Color.red(shineColor), Color.green(shineColor),
            Color.blue(shineColor));
    int[] gradientColors = { upperColor, shineColor };
    GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
            gradientColors);

    int stop = (int) (rectangle.height() * GRADIENT_STOP) + rectangle.top;
    gradientDrawable.setBounds(rectangle.left, rectangle.top, rectangle.right, stop);
    gradientDrawable.draw(canvas);

    Rect shadowRect = new Rect(rectangle.left, stop, rectangle.right, rectangle.bottom);
    Paint shadowPaint = new Paint();
    shadowPaint.setColor(shineColor);
    canvas.drawRect(shadowRect, shadowPaint);
}

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