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:it.ndorigatti.android.view.MulticolorProgressBar.java

/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 *//*from   w  w  w  . j  a  va  2 s .  c  o m*/
private Drawable tileify(Drawable drawable, boolean clip) {

    if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i),
                    (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }

        LayerDrawable newBg = new LayerDrawable(outDrawables);
        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }
        return newBg;
    }
    return drawable;
}

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

/**
 *
 * @return a Drawable with a youtube logo in the center
 *///from  w  ww.  ja v a2s . 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:com.landenlabs.all_devtool.IconBaseFragment.java

/**
 * Show a 'StateListDrawable' information
 *
 * @param imageView// w w w .java  2  s  .c  o  m
 * @param row1
 * @param row2
 * @param stateListDrawable
 * @param state
 * @param desc
 * @param stateIcons
 */
private void showStateIcon(final ImageView imageView, TableRow row1, TableRow row2,
        StateListDrawable stateListDrawable, int state, String desc, Set<Drawable> stateIcons) {

    stateListDrawable.setState(new int[] { state });
    Drawable stateD = stateListDrawable.getCurrent();
    if (stateD != null && !stateIcons.contains(stateD)) {
        stateIcons.add(stateD);
        ImageButton stateImageView = new ImageButton(imageView.getContext());
        Drawable[] drawables = new Drawable[] { stateD,
                getResources().getDrawable(R.drawable.button_border_sel) };

        LayerDrawable layerDrawable = new LayerDrawable(drawables);
        stateImageView.setImageDrawable(layerDrawable);
        //   stateImageView.setBackgroundResource(R.drawable.button_border_sel);
        stateImageView.setPadding(10, 10, 10, 10);
        stateImageView.setMinimumHeight(8);
        stateImageView.setMinimumWidth(8);
        stateImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView.setImageDrawable(((ImageView) v).getDrawable());
            }
        });

        TextView stateTextView = new TextView(imageView.getContext());
        stateTextView.setText(desc);
        stateTextView.setTextSize(12);
        stateTextView.setGravity(Gravity.CENTER);

        row1.addView(stateTextView);
        row2.addView(stateImageView);
    }
}

From source file:com.nttec.everychan.ui.tabs.TabsAdapter.java

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView == null ? inflater.inflate(R.layout.sidebar_tabitem, parent, false) : convertView;
    View dragHandler = view.findViewById(R.id.tab_drag_handle);
    ImageView favIcon = (ImageView) view.findViewById(R.id.tab_favicon);
    TextView title = (TextView) view.findViewById(R.id.tab_text_view);
    ImageView closeBtn = (ImageView) view.findViewById(R.id.tab_close_button);

    dragHandler.getLayoutParams().width = position == draggingItem ? ViewGroup.LayoutParams.WRAP_CONTENT : 0;
    dragHandler.setLayoutParams(dragHandler.getLayoutParams());

    if (position == selectedItem) {
        TypedValue typedValue = ThemeUtils.resolveAttribute(context.getTheme(), R.attr.sidebarSelectedItem,
                true);/* w ww.  j  av  a 2  s  . co  m*/
        if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT
                && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
            view.setBackgroundColor(typedValue.data);
        } else {
            view.setBackgroundResource(typedValue.resourceId);
        }
    } else {
        view.setBackgroundColor(Color.TRANSPARENT);
    }

    TabModel model = this.getItem(position);

    switch (model.type) {
    case TabModel.TYPE_NORMAL:
    case TabModel.TYPE_LOCAL:
        closeBtn.setVisibility(View.VISIBLE);
        String titleText = model.title;
        if (model.unreadPostsCount > 0 || model.autoupdateError) {
            StringBuilder titleStringBuilder = new StringBuilder();
            if (model.unreadSubscriptions)
                titleStringBuilder.append("[*] ");
            if (model.autoupdateError)
                titleStringBuilder.append("[X] ");
            if (model.unreadPostsCount > 0)
                titleStringBuilder.append('[').append(model.unreadPostsCount).append("] ");
            titleText = titleStringBuilder.append(titleText).toString();
        }
        title.setText(titleText);
        ChanModule chan = MainApplication.getInstance().getChanModule(model.pageModel.chanName);
        Drawable icon = chan != null ? chan.getChanFavicon()
                : ResourcesCompat.getDrawable(context.getResources(), android.R.drawable.ic_delete, null);
        if (icon != null) {
            if (model.type == TabModel.TYPE_LOCAL) {
                Drawable[] layers = new Drawable[] { icon, ResourcesCompat.getDrawable(context.getResources(),
                        R.drawable.favicon_overlay_local, null) };
                icon = new LayerDrawable(layers);
            }
            /* XXX
             ?       ( overlay  favicon    ),
             ?   ?? ,   ,     (   ).
            ?    ? ? , , ? -? -  ,    ?.
               ??  ?,   ? ?  .
                    
            else if (model.type == TabModel.TYPE_NORMAL && model.pageModel != null &&
                    model.pageModel.type == UrlPageModel.TYPE_THREADPAGE && model.autoupdateBackground &&
                    MainApplication.getInstance().settings.isAutoupdateEnabled() &&
                    MainApplication.getInstance().settings.isAutoupdateBackground()) {
                Drawable[] layers = new Drawable[] {
                        icon, ResourcesCompat.getDrawable(context.getResources(), R.drawable.favicon_overlay_autoupdate, null) };
                icon = new LayerDrawable(layers);
            }
            */
            favIcon.setImageDrawable(icon);
            favIcon.setVisibility(View.VISIBLE);
        } else {
            favIcon.setVisibility(View.GONE);
        }
        break;
    default:
        closeBtn.setVisibility(View.GONE);
        title.setText(R.string.error_deserialization);
        favIcon.setVisibility(View.GONE);
    }

    closeBtn.setTag(position);
    closeBtn.setOnClickListener(onCloseClick);
    return view;
}

From source file:com.google.android.finsky.detailspage.WarningMessageModule.java

public final void bindView(View paramView) {
      WarningMessageModuleLayout localWarningMessageModuleLayout = (WarningMessageModuleLayout) paramView;
      int j;//from  www  .j  a va2  s . c  o m
      if ((!localWarningMessageModuleLayout.mBinded) || (this.mNeedsRefresh)) {
          String str = ((WarningMessageModuleData) this.mModuleData).messageText;
          boolean bool = ((WarningMessageModuleData) this.mModuleData).isMessageLink;
          int i = ((WarningMessageModuleData) this.mModuleData).messageIconResId;
          j = ((WarningMessageModuleData) this.mModuleData).detailsDoc.mDocument.backendId;
          localWarningMessageModuleLayout.mBinded = true;
          localWarningMessageModuleLayout.mWarningMessageText.setText(str);
          localWarningMessageModuleLayout.mWarningMessageIcon.setImageResource(i);
          if (!bool) {
              break label231;
          }
          localWarningMessageModuleLayout.mWarningMessageText.setMovementMethod(LinkMovementMethod.getInstance());
      }
      for (;;) {
          Context localContext = localWarningMessageModuleLayout.getContext();
          ColorStateList localColorStateList = CorpusResourceUtils.getSecondaryTextColor(localContext, j);
          localWarningMessageModuleLayout.mWarningMessageText.setTextColor(localColorStateList);
          int k = UiUtils.interpolateColor$4868c7be(CorpusResourceUtils.getPrimaryColor(localContext, j));
          int m = localWarningMessageModuleLayout.getPaddingTop();
          int n = localWarningMessageModuleLayout.getPaddingBottom();
          int i1 = ViewCompat.getPaddingEnd(localWarningMessageModuleLayout);
          int i2 = ViewCompat.getPaddingStart(localWarningMessageModuleLayout);
          Drawable[] arrayOfDrawable = new Drawable[2];
          arrayOfDrawable[0] = new ColorDrawable(k);
          arrayOfDrawable[1] = ContextCompat.getDrawable(localContext, 2130837958);
          localWarningMessageModuleLayout.setBackgroundDrawable(new LayerDrawable(arrayOfDrawable));
          ViewCompat.setPaddingRelative(localWarningMessageModuleLayout, i2, m, i1, n);
          this.mNeedsRefresh = false;
          return;
          label231: localWarningMessageModuleLayout.mWarningMessageText.setMovementMethod(null);
      }
  }

From source file:uk.co.brightec.ratetheapp.RateTheApp.java

/**
 * Converts a drawable to a tiled version of itself. It will recursively traverse layer and state list drawables.
 * This method was copied from android.widget.ProgressBar, however it was highlighted in their code that this may be sub optimal.
 *
 * @param drawable The drawable to tileify
 * @param clip     Whether to clip drawable
 * @return The tiled drawable//ww w.java  2 s. com
 */
private Drawable tileify(Drawable drawable, boolean clip) {
    if (drawable instanceof LayerDrawable) {
        final LayerDrawable orig = (LayerDrawable) drawable;
        final int N = orig.getNumberOfLayers();
        final Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            final int id = orig.getId(i);
            outDrawables[i] = tileify(orig.getDrawable(i),
                    (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }

        final LayerDrawable clone = new LayerDrawable(outDrawables);
        for (int i = 0; i < N; i++) {
            clone.setId(i, orig.getId(i));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                clone.setLayerGravity(i, orig.getLayerGravity(i));
                clone.setLayerWidth(i, orig.getLayerWidth(i));
                clone.setLayerHeight(i, orig.getLayerHeight(i));
                clone.setLayerInsetLeft(i, orig.getLayerInsetLeft(i));
                clone.setLayerInsetRight(i, orig.getLayerInsetRight(i));
                clone.setLayerInsetTop(i, orig.getLayerInsetTop(i));
                clone.setLayerInsetBottom(i, orig.getLayerInsetBottom(i));
                clone.setLayerInsetStart(i, orig.getLayerInsetStart(i));
                clone.setLayerInsetEnd(i, orig.getLayerInsetEnd(i));
            }
        }

        return clone;
    }

    if (drawable instanceof BitmapDrawable) {
        final BitmapDrawable bitmap = (BitmapDrawable) drawable;

        final BitmapDrawable clone = (BitmapDrawable) bitmap.getConstantState().newDrawable();
        clone.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);

        if (clip) {
            return new ClipDrawable(clone, Gravity.LEFT, ClipDrawable.HORIZONTAL);
        } else {
            return clone;
        }
    }

    return drawable;
}

From source file:com.artemchep.horario.ui.fragments.master.LessonsFragment.java

@Override
protected void setupToolbar() {
    super.setupToolbar();
    CustomAppBar appBar = getMainActivity().mAppBar;
    appBar.setTitle(getString(R.string.nav_lessons));

    Toolbar toolbar = appBar.getToolbarSpecific();
    toolbar.inflateMenu(R.menu.master_lessons_week);
    toolbar.setOnMenuItemClickListener(this);

    // Load week icon
    TypedArray a = getActivity().getTheme()
            .obtainStyledAttributes(new int[] { R.attr.icon_calendar_empty_grey, R.attr.is_dark_theme });
    int iconDrawableRes = a.getResourceId(0, 0);
    boolean isDarkTheme = a.getBoolean(1, false);
    a.recycle();//from w w  w  .j  a va2s  .co m

    float density = getResources().getDisplayMetrics().density;
    mWeekDrawable = new TextDrawable();
    mWeekDrawable.setTranslationY(density * 2);
    mWeekDrawable.setTextSize(density * 10);
    mWeekDrawable.setColor(isDarkTheme ? Color.WHITE : Color.GRAY);
    mWeekCompositeDrawable = new LayerDrawable(
            new Drawable[] { ContextCompat.getDrawable(getContext(), iconDrawableRes), mWeekDrawable });

    Menu menu = toolbar.getMenu();
    mWeekMenuItem = menu.findItem(R.id.action_switch_week);
    mWeekMenuItem.setIcon(mWeekCompositeDrawable);
    mWeekMenuItem.setVisible(mWeekCycle > 1);
    updateWeekNumberMenuItemText();
}

From source file:com.xxjwd.sjbg.MainActivity.java

 private void changeColor(int newColor) {

   tabs.setIndicatorColor(newColor);//  w  ww . j  a  v  a 2s .  c om

   // change ActionBar color just if an ActionBar is available
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

      Drawable colorDrawable = new ColorDrawable(newColor);
      Drawable bottomDrawable = getResources().getDrawable(R.drawable.actionbar_bottom);
      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 {
            getActionBar().setBackgroundDrawable(ld);
         }

      } 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 {
            getActionBar().setBackgroundDrawable(td);
         }

         td.startTransition(200);

      }

      oldBackground = ld;

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

   }

   currentColor = newColor;

}

From source file:com.nile.kmooc.view.custom.popup.menu.MenuPopupHelper.java

public boolean tryShow() {
    mPopup = new ListPopupWindow(mContext, null, mPopupStyleAttr, mPopupStyleRes);
    mPopup.setOnDismissListener(this);
    mPopup.setOnItemClickListener(this);
    mPopup.setAdapter(mAdapter);//  w w w. j a  va 2 s  . c  o  m
    mPopup.setModal(true);

    View anchor = mAnchorView;
    if (anchor != null) {
        final boolean addGlobalListener = mTreeObserver == null;
        mTreeObserver = anchor.getViewTreeObserver(); // Refresh to latest
        if (addGlobalListener)
            mTreeObserver.addOnGlobalLayoutListener(this);
        anchor.addOnAttachStateChangeListener(this);
        mPopup.setAnchorView(anchor);
        mPopup.setDropDownGravity(mDropDownGravity);
    } else {
        return false;
    }

    if (mContentWidth == Integer.MIN_VALUE) {
        mContentWidth = measureContentWidth();
    }
    mPopup.setContentWidth(mContentWidth);
    // Invert the horizontal offset in RTL mode.
    if (ViewCompat.getLayoutDirection(mAnchorView) == ViewCompat.LAYOUT_DIRECTION_RTL) {
        mPopup.setHorizontalOffset(-mPopup.getHorizontalOffset());
    }
    // Implement right gravity manually through horizontal offset pre-KitKat.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT
            && (Gravity.getAbsoluteGravity(mDropDownGravity, ViewCompat.getLayoutDirection(anchor))
                    & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.RIGHT) {
        mPopup.setHorizontalOffset(mPopup.getHorizontalOffset() + (mAnchorView.getWidth() - mPopup.getWidth()));
    }
    // If vertical offset is defined as 0, then ListPopupWindow infers
    // it as the negative of the top padding of the background, in
    // order to anchor the content area. Since that is not the effect
    // we want, we'll force it to use only the explicitly defined
    // offset by explicitly setting it dynamically as well, and thus
    // forcing it to discard it's 'unset' flag.
    mPopup.setVerticalOffset(mPopup.getVerticalOffset());
    // Top/bottom padding will be applied on the background drawable,
    // as the ListView is both initialized and set up only after show()
    // is called on the ListPopupWindow. Left/right padding will be
    // set up on the list items from the adapter, to keep the correct
    // item boundaries for the selector.
    ShapeDrawable paddedDrawable = new ShapeDrawable();
    paddedDrawable.setAlpha(0);
    // Don't apply top padding if the first item is a header, to
    // comply with the design.
    paddedDrawable.setPadding(0, mAdapter.hasHeader() ? 0 : (mPopupPaddingTop - mPopupItemVerticalPadding), 0,
            mPopupPaddingBottom - mPopupItemVerticalPadding);
    Drawable background = mPopup.getBackground();
    mPopup.setBackgroundDrawable(background == null ? paddedDrawable
            : new LayerDrawable(new Drawable[] { background, paddedDrawable }));
    mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
    mPopup.show();
    mPopup.getListView().setOnKeyListener(this);
    return true;
}

From source file:com.saulmm.cui.OrderDialogFragment.java

private Drawable createProductImageDrawable(Product product) {
    final ShapeDrawable background = new ShapeDrawable();
    background.setShape(new OvalShape());
    background.getPaint().setColor(ContextCompat.getColor(getContext(), product.color));

    final BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(),
            BitmapFactory.decodeResource(getResources(), product.image));

    final LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { background, bitmapDrawable });

    final int padding = (int) getResources().getDimension(R.dimen.spacing_huge);
    layerDrawable.setLayerInset(1, padding, padding, padding, padding);

    return layerDrawable;
}