Java Code Examples for android.graphics.drawable.Drawable#getConstantState()

The following examples show how to use android.graphics.drawable.Drawable#getConstantState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SkinCompatDrawableManager.java    From Android-skin-support with MIT License 6 votes vote down vote up
private boolean addDrawableToCache(@NonNull final Context context, final long key,
                                   @NonNull final Drawable drawable) {
    final ConstantState cs = drawable.getConstantState();
    if (cs != null) {
        synchronized (mDrawableCacheLock) {
            LongSparseArray<WeakReference<ConstantState>> cache = mDrawableCaches.get(context);
            if (cache == null) {
                cache = new LongSparseArray<>();
                mDrawableCaches.put(context, cache);
            }
            cache.put(key, new WeakReference<ConstantState>(cs));
        }
        return true;
    }
    return false;
}
 
Example 2
Source File: SuggestionsAdapter.java    From zhangshangwuda with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the activity or application icon for an activity.
 * Uses the local icon cache for fast repeated lookups.
 *
 * @param component Name of an activity.
 * @return A drawable, or {@code null} if neither the activity nor the application
 *         has an icon set.
 */
private Drawable getActivityIconWithCache(ComponentName component) {
    // First check the icon cache
    String componentIconKey = component.flattenToShortString();
    // Using containsKey() since we also store null values.
    if (mOutsideDrawablesCache.containsKey(componentIconKey)) {
        Drawable.ConstantState cached = mOutsideDrawablesCache.get(componentIconKey);
        return cached == null ? null : cached.newDrawable(mProviderContext.getResources());
    }
    // Then try the activity or application icon
    Drawable drawable = getActivityIcon(component);
    // Stick it in the cache so we don't do this lookup again.
    Drawable.ConstantState toCache = drawable == null ? null : drawable.getConstantState();
    mOutsideDrawablesCache.put(componentIconKey, toCache);
    return drawable;
}
 
Example 3
Source File: Style.java    From LaunchTime with GNU General Public License v3.0 6 votes vote down vote up
public Drawable getBgDrawableFor(View view, int color) {

        String key = view.toString() + color;

        Drawable newbg = bgDrawables.get(key);

        if (newbg==null) {

            Drawable base = mContext.getResources().getDrawable(R.drawable.rounded);
            if (base.getConstantState()==null) {
                newbg = base.mutate();  //shouldn't ever get here
                Log.d("Style", "base drawable had a null getConstantState");
            } else {
                newbg = base.getConstantState().newDrawable().mutate();
            }
            bgDrawables.put(key, newbg);
        }

        if (color!=-1) {
            newbg.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
        }

        return newbg;
    }
 
Example 4
Source File: BottomNavigationItemView.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Override
public void setIcon(@Nullable Drawable iconDrawable) {
  if (iconDrawable == originalIconDrawable) {
    return;
  }

  // Save the original icon to check if it has changed in future calls of this method.
  originalIconDrawable = iconDrawable;
  if (iconDrawable != null) {
    Drawable.ConstantState state = iconDrawable.getConstantState();
    iconDrawable =
        DrawableCompat.wrap(state == null ? iconDrawable : state.newDrawable()).mutate();
    wrappedIconDrawable = iconDrawable;
    if (iconTint != null) {
      DrawableCompat.setTintList(wrappedIconDrawable, iconTint);
    }
  }
  this.icon.setImageDrawable(iconDrawable);
}
 
Example 5
Source File: NavigationMenuItemView.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Override
public void setIcon(@Nullable Drawable icon) {
  if (icon != null) {
    if (hasIconTintList) {
      Drawable.ConstantState state = icon.getConstantState();
      icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate();
      DrawableCompat.setTintList(icon, iconTintList);
    }
    icon.setBounds(0, 0, iconSize, iconSize);
  } else if (needsEmptyIcon) {
    if (emptyDrawable == null) {
      emptyDrawable =
          ResourcesCompat.getDrawable(
              getResources(), R.drawable.navigation_empty_icon, getContext().getTheme());
      if (emptyDrawable != null) {
        emptyDrawable.setBounds(0, 0, iconSize, iconSize);
      }
    }
    icon = emptyDrawable;
  }
  TextViewCompat.setCompoundDrawablesRelative(textView, icon, null, null, null);
}
 
Example 6
Source File: TintManager.java    From MagicaSakura with Apache License 2.0 6 votes vote down vote up
private boolean addCachedDrawable(final int key, @NonNull final Drawable drawable) {
    if (drawable instanceof FilterableStateListDrawable) {
        return false;
    }
    final Drawable.ConstantState cs = drawable.getConstantState();
    if (cs != null) {
        synchronized (mDrawableCacheLock) {
            if (mCacheDrawables == null) {
                mCacheDrawables = new SparseArray<>();
            }
            mCacheDrawables.put(key, new WeakReference<>(cs));
        }
        return true;
    }
    return false;
}
 
Example 7
Source File: DrawableUtils.java    From RangeSeekBar with MIT License 6 votes vote down vote up
public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
    if (drawable instanceof DrawableContainer) {
        // If we have a DrawableContainer, let's traverse it's child array
        final Drawable.ConstantState state = drawable.getConstantState();
        if (state instanceof DrawableContainer.DrawableContainerState) {
            final DrawableContainer.DrawableContainerState containerState =
                (DrawableContainer.DrawableContainerState) state;
            for (final Drawable child : containerState.getChildren()) {
                if (!canSafelyMutateDrawable(child)) {
                    return false;
                }
            }
        }

    } else if (drawable instanceof DrawableWrapper) {
        return canSafelyMutateDrawable(
            Objects.requireNonNull(((DrawableWrapper) drawable).getDrawable()));
    } else if (drawable instanceof ScaleDrawable) {
        return canSafelyMutateDrawable(Objects.requireNonNull(((ScaleDrawable) drawable).getDrawable()));
    }

    return true;
}
 
Example 8
Source File: ThemeUtils.java    From timecat with Apache License 2.0 6 votes vote down vote up
public static boolean containsNinePatch(Drawable drawable) {
    drawable = getWrapperDrawable(drawable);
    if (drawable instanceof NinePatchDrawable || drawable instanceof InsetDrawable || drawable instanceof LayerDrawable) {
        return true;
    } else if (drawable instanceof StateListDrawable) {
        final DrawableContainer.DrawableContainerState containerState = ((DrawableContainer.DrawableContainerState) drawable.getConstantState());
        //can't get containState from drawable which is containing DrawableWrapperDonut
        //https://code.google.com/p/android/issues/detail?id=169920
        if (containerState == null) {
            return true;
        }
        for (Drawable dr : containerState.getChildren()) {
            dr = getWrapperDrawable(dr);
            if (dr instanceof NinePatchDrawable || dr instanceof InsetDrawable || dr instanceof LayerDrawable) {
                return true;
            }
        }
    }
    return false;
}
 
Example 9
Source File: SuggestionsAdapter.java    From Libraries-for-Android-Developers with MIT License 6 votes vote down vote up
/**
 * Gets the activity or application icon for an activity.
 * Uses the local icon cache for fast repeated lookups.
 *
 * @param component Name of an activity.
 * @return A drawable, or {@code null} if neither the activity nor the application
 *         has an icon set.
 */
private Drawable getActivityIconWithCache(ComponentName component) {
    // First check the icon cache
    String componentIconKey = component.flattenToShortString();
    // Using containsKey() since we also store null values.
    if (mOutsideDrawablesCache.containsKey(componentIconKey)) {
        Drawable.ConstantState cached = mOutsideDrawablesCache.get(componentIconKey);
        return cached == null ? null : cached.newDrawable(mProviderContext.getResources());
    }
    // Then try the activity or application icon
    Drawable drawable = getActivityIcon(component);
    // Stick it in the cache so we don't do this lookup again.
    Drawable.ConstantState toCache = drawable == null ? null : drawable.getConstantState();
    mOutsideDrawablesCache.put(componentIconKey, toCache);
    return drawable;
}
 
Example 10
Source File: SkinCompatDrawableManager.java    From Android-skin-support with MIT License 6 votes vote down vote up
private boolean addDrawableToCache(@NonNull final Context context, final long key,
                                   @NonNull final Drawable drawable) {
    final ConstantState cs = drawable.getConstantState();
    if (cs != null) {
        synchronized (mDrawableCacheLock) {
            LongSparseArray<WeakReference<ConstantState>> cache = mDrawableCaches.get(context);
            if (cache == null) {
                cache = new LongSparseArray<>();
                mDrawableCaches.put(context, cache);
            }
            cache.put(key, new WeakReference<ConstantState>(cs));
        }
        return true;
    }
    return false;
}
 
Example 11
Source File: ThemeUtils.java    From MagicaSakura with Apache License 2.0 6 votes vote down vote up
public static boolean containsNinePatch(Drawable drawable) {
    drawable = getWrapperDrawable(drawable);
    if (drawable instanceof NinePatchDrawable
            || drawable instanceof InsetDrawable
            || drawable instanceof LayerDrawable) {
        return true;
    } else if (drawable instanceof StateListDrawable) {
        final DrawableContainer.DrawableContainerState containerState = ((DrawableContainer.DrawableContainerState) drawable.getConstantState());
        //can't get containState from drawable which is containing DrawableWrapperDonut
        //https://code.google.com/p/android/issues/detail?id=169920
        if (containerState == null) {
            return true;
        }
        for (Drawable dr : containerState.getChildren()) {
            dr = getWrapperDrawable(dr);
            if (dr instanceof NinePatchDrawable
                    || dr instanceof InsetDrawable
                    || dr instanceof LayerDrawable) {
                return true;
            }
        }
    }
    return false;
}
 
Example 12
Source File: TintAwareDrawable.java    From ProjectX with Apache License 2.0 6 votes vote down vote up
public final void setWrappedDrawable(Drawable dr) {
    if (this.mDrawable != null) {
        this.mDrawable.setCallback((Callback) null);
    }

    this.mDrawable = dr;
    if (dr != null) {
        dr.setCallback(this);
        this.setVisible(dr.isVisible(), true);
        this.setState(dr.getState());
        this.setLevel(dr.getLevel());
        this.setBounds(dr.getBounds());
        if (this.mState != null) {
            this.mState.mDrawableState = dr.getConstantState();
        }
    }

    this.invalidateSelf();
}
 
Example 13
Source File: SuggestionsAdapter.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the activity or application icon for an activity.
 * Uses the local icon cache for fast repeated lookups.
 *
 * @param component Name of an activity.
 * @return A drawable, or {@code null} if neither the activity nor the application
 *         has an icon set.
 */
private Drawable getActivityIconWithCache(ComponentName component) {
    // First check the icon cache
    String componentIconKey = component.flattenToShortString();
    // Using containsKey() since we also store null values.
    if (mOutsideDrawablesCache.containsKey(componentIconKey)) {
        Drawable.ConstantState cached = mOutsideDrawablesCache.get(componentIconKey);
        return cached == null ? null : cached.newDrawable(mProviderContext.getResources());
    }
    // Then try the activity or application icon
    Drawable drawable = getActivityIcon(component);
    // Stick it in the cache so we don't do this lookup again.
    Drawable.ConstantState toCache = drawable == null ? null : drawable.getConstantState();
    mOutsideDrawablesCache.put(componentIconKey, toCache);
    return drawable;
}
 
Example 14
Source File: CommonUtil.java    From WanAndroid with Apache License 2.0 5 votes vote down vote up
public static Drawable getTintDrawable(Drawable originalDrawable, ColorStateList colors) {
    Drawable.ConstantState state = originalDrawable.getConstantState();
    Drawable tintDrawable = DrawableCompat.wrap(state == null ? originalDrawable : state.newDrawable()).mutate();
    tintDrawable.setBounds(0, 0, originalDrawable.getIntrinsicWidth(), originalDrawable.getIntrinsicHeight());
    DrawableCompat.setTintList(tintDrawable, colors);
    return tintDrawable;
}
 
Example 15
Source File: ReactTextInputManager.java    From react-native-GPay with MIT License 5 votes vote down vote up
@ReactProp(name = "underlineColorAndroid", customType = "Color")
public void setUnderlineColor(ReactEditText view, @Nullable Integer underlineColor) {
  // Drawable.mutate() can sometimes crash due to an AOSP bug:
  // See https://code.google.com/p/android/issues/detail?id=191754 for more info
  Drawable background = view.getBackground();
  Drawable drawableToMutate = background.getConstantState() != null ?
    background.mutate() :
    background;

  if (underlineColor == null) {
    drawableToMutate.clearColorFilter();
  } else {
    drawableToMutate.setColorFilter(underlineColor, PorterDuff.Mode.SRC_IN);
  }
}
 
Example 16
Source File: BottomNavigationItemView.java    From MiPushFramework with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setIcon(Drawable icon) {
    if (icon != null) {
        Drawable.ConstantState state = icon.getConstantState();
        icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate();
        DrawableCompat.setTintList(icon, mIconTint);
    }
    mIcon.setImageDrawable(icon);
}
 
Example 17
Source File: DialogAdapter.java    From YCDialog with Apache License 2.0 5 votes vote down vote up
private Drawable icon(Drawable drawable) {
    if (drawable != null) {
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        @SuppressWarnings("SuspiciousNameCombination")
        Drawable resizeIcon = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, leftIcon, leftIcon, true));
        Drawable.ConstantState state = resizeIcon.getConstantState();
        resizeIcon = DrawableCompat.wrap(state == null ? resizeIcon : state.newDrawable().mutate());
        return resizeIcon;
    }
    return null;
}
 
Example 18
Source File: AlbumMediaAdapter.java    From Matisse with Apache License 2.0 5 votes vote down vote up
@Override
protected void onBindViewHolder(final RecyclerView.ViewHolder holder, Cursor cursor) {
    if (holder instanceof CaptureViewHolder) {
        CaptureViewHolder captureViewHolder = (CaptureViewHolder) holder;
        Drawable[] drawables = captureViewHolder.mHint.getCompoundDrawables();
        TypedArray ta = holder.itemView.getContext().getTheme().obtainStyledAttributes(
                new int[]{R.attr.capture_textColor});
        int color = ta.getColor(0, 0);
        ta.recycle();

        for (int i = 0; i < drawables.length; i++) {
            Drawable drawable = drawables[i];
            if (drawable != null) {
                final Drawable.ConstantState state = drawable.getConstantState();
                if (state == null) {
                    continue;
                }

                Drawable newDrawable = state.newDrawable().mutate();
                newDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
                newDrawable.setBounds(drawable.getBounds());
                drawables[i] = newDrawable;
            }
        }
        captureViewHolder.mHint.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
    } else if (holder instanceof MediaViewHolder) {
        MediaViewHolder mediaViewHolder = (MediaViewHolder) holder;

        final Item item = Item.valueOf(cursor);
        mediaViewHolder.mMediaGrid.preBindMedia(new MediaGrid.PreBindInfo(
                getImageResize(mediaViewHolder.mMediaGrid.getContext()),
                mPlaceholder,
                mSelectionSpec.countable,
                holder
        ));
        mediaViewHolder.mMediaGrid.bindMedia(item);
        mediaViewHolder.mMediaGrid.setOnMediaGridClickListener(this);
        setCheckStatus(item, mediaViewHolder.mMediaGrid);
    }
}
 
Example 19
Source File: AppMenuAdapter.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private void highlightItemIfNecessary(View view, boolean isIcon, int itemId) {
    if (mHighlightedItemId == null) return;

    Drawable background = (Drawable) view.getTag(R.id.menu_item_original_background);
    if (background == null) return;

    if (itemId != mHighlightedItemId) {
        view.setBackground(background);
        return;
    }

    if (mHighlightDrawableSource == null) {
        mHighlightDrawableSource = isIcon ? PulseDrawable.createCircle(view.getContext())
                                          : PulseDrawable.createHighlight();
    }

    Resources resources = ContextUtils.getApplicationContext().getResources();

    PulseDrawable pulse =
            (PulseDrawable) mHighlightDrawableSource.getConstantState().newDrawable(resources);
    if (background.getConstantState() != null) {
        background = background.getConstantState().newDrawable(resources);
    }

    LayerDrawable drawable = new LayerDrawable(new Drawable[] {pulse, background});
    view.setBackground(drawable);
    pulse.start();
}
 
Example 20
Source File: LauncherIcons.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public ShadowDrawable(Bitmap shadow, Drawable dr) {
    super(dr);
    mState = new MyConstantState(shadow, dr.getConstantState());
}