Example usage for android.graphics.drawable Drawable getConstantState

List of usage examples for android.graphics.drawable Drawable getConstantState

Introduction

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

Prototype

public @Nullable ConstantState getConstantState() 

Source Link

Document

Return a ConstantState instance that holds the shared state of this Drawable.

Usage

From source file:github.madmarty.madsonic.util.ImageLoader.java

public void loadImage(View view, MusicDirectory.Entry entry, boolean large, boolean crossfade,
        boolean reflection) {
    if (entry == null || entry.getCoverArt() == null) {
        setUnknownImage(view, large);/*  www  . j a v  a2 s.c o  m*/
        return;
    }

    int size = large ? imageSizeLarge : imageSizeDefault;

    //        Bitmap bitmap = cache.get(getKey(entry.getCoverArt(), size));
    //        if (bitmap != null) {
    //         final Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap);
    //            setImage(view, drawable, large);
    //            return;

    Bitmap bitmap = cache.get(getKey(entry.getCoverArt(), size));
    if (bitmap != null) {
        // Create a clone since the images can be modified by the caller.
        Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap);
        Drawable clone = drawable.getConstantState().newDrawable();
        setImage(view, clone, large);
        return;
    }

    if (!large) {
        setUnknownImage(view, large);
    }
    queue.offer(new Task(view.getContext(), entry, size, imageSizeLarge, large ? true : false,
            new ViewTaskHandler(view, crossfade)));
}

From source file:android.support.v7.widget.AppCompatDrawableManager.java

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);
            }/*w  w w.  ja v  a2 s .c om*/
            cache.put(key, new WeakReference<ConstantState>(cs));
        }
        return true;
    }
    return false;
}

From source file:com.retroteam.studio.retrostudio.MeasureEditor.java

/**
 * Assign a note visually and in the project.
 * @param view/*from  w  w w.  j a va 2s.  c  om*/
 */
private void paintNote(View view) {
    com.getbase.floatingactionbutton.FloatingActionButton ptool = (com.getbase.floatingactionbutton.FloatingActionButton) findViewById(
            R.id.pencilTool);
    ImageView iview = (ImageView) view;
    Drawable notestatus = iview.getDrawable();
    if (pencil) {
        if (notestatus.getConstantState().equals(ContextCompat
                .getDrawable(getApplicationContext(), R.drawable.note_filled).getConstantState())) {

            //blank all other notes in the column
            TableRow parent = (TableRow) iview.getParent();
            TableLayout layoutparent = (TableLayout) parent.getParent();
            int notedrawlen = layoutparent.getChildCount();
            for (int x = 0; x < notedrawlen; x++) {
                TableRow noterow = (TableRow) layoutparent.getChildAt(x);
                for (int i = 0; i < noterow.getChildCount(); i++) {
                    ImageView note = (ImageView) noterow.getChildAt(i);
                    if (note.getTag(R.id.TAG_COLUMN) == iview.getTag(R.id.TAG_COLUMN)) {
                        note.setImageDrawable(
                                ContextCompat.getDrawable(getApplicationContext(), R.drawable.measure_outline));
                        for (int n = 0; n < filledNotes.size(); n++) {
                            int[] comp = filledNotes.get(n);
                            if (comp[0] == (int) note.getTag(R.id.TAG_ROW)
                                    && comp[1] == (int) note.getTag(R.id.TAG_COLUMN)) {
                                filledNotes.remove(n);
                            }
                        }
                    }
                }
            }
            //set the drawable
            iview.setImageDrawable(
                    ContextCompat.getDrawable(getApplicationContext(), R.drawable.measure_outline));
            filledNotes.remove(
                    new int[] { (int) iview.getTag(R.id.TAG_ROW), (int) iview.getTag(R.id.TAG_COLUMN) });

            // set the other notes to rests
            List<Integer> guiSNAPRange = (List<Integer>) iview.getTag(R.id.TAG_GUISNAPRANGE);
            for (int z = guiSNAPRange.get(0); z <= guiSNAPRange.get(guiSNAPRange.size() - 1); z++) {
                theproject.track(trackNum).measure(measureNum).replace(z, new Note(Note.REST, noteSUB));
            }
        } else {
            //blank all other notes in the column
            TableRow parent = (TableRow) iview.getParent();
            TableLayout layoutparent = (TableLayout) parent.getParent();
            int notedrawlen = layoutparent.getChildCount();
            for (int x = 0; x < notedrawlen; x++) {
                TableRow noterow = (TableRow) layoutparent.getChildAt(x);
                for (int i = 0; i < noterow.getChildCount(); i++) {
                    ImageView note = (ImageView) noterow.getChildAt(i);
                    if (note.getTag(R.id.TAG_COLUMN) == iview.getTag(R.id.TAG_COLUMN)) {
                        note.setImageDrawable(
                                ContextCompat.getDrawable(getApplicationContext(), R.drawable.measure_outline));
                        for (int n = 0; n < filledNotes.size(); n++) {
                            int[] comp = filledNotes.get(n);
                            if (comp[0] == (int) note.getTag(R.id.TAG_ROW)
                                    && comp[1] == (int) note.getTag(R.id.TAG_COLUMN)) {
                                filledNotes.remove(n);
                            }
                        }
                    }
                }
            }
            //set the drawable
            iview.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.note_filled));
            filledNotes
                    .add(new int[] { (int) iview.getTag(R.id.TAG_ROW), (int) iview.getTag(R.id.TAG_COLUMN) });

            //set the note in the data structure
            double notetype = stringToNoteDouble((String) iview.getTag(R.id.TAG_NOTE));
            List<Integer> guiSNAPRange = (List<Integer>) iview.getTag(R.id.TAG_GUISNAPRANGE);
            for (int z = guiSNAPRange.get(0); z <= guiSNAPRange.get(guiSNAPRange.size() - 1); z++) {
                theproject.track(trackNum).measure(measureNum).replace(z, new Note(notetype, noteSUB));
            }
        }
    }
}

From source file:com.waz.zclient.pages.main.profile.preferences.dialogs.AddPhoneNumberPreferenceDialogFragment.java

private void ensureBackgroundDrawableStateWorkaround(EditText editText) {
    final int sdk = Build.VERSION.SDK_INT;
    if (sdk != 21 && sdk != 22) {
        // The workaround is only required on API 21-22
        return;/*w ww  .j  a  v  a 2 s  .  c om*/
    }
    final Drawable bg = editText.getBackground();
    if (bg == null) {
        return;
    }

    // There is an issue in the platform which affects container Drawables
    // where the first drawable retrieved from resources will propogate any changes
    // (like color filter) to all instances from the cache. We'll try to workaround it...

    final Drawable newBg = bg.getConstantState().newDrawable();

    boolean hasReconstructedEditTextBackground = false;
    if (bg instanceof DrawableContainer) {
        // If we have a Drawable container, we can try and set it's constant state via
        // reflection from the new Drawable
        hasReconstructedEditTextBackground = DrawableUtils.setContainerConstantState((DrawableContainer) bg,
                newBg.getConstantState());
    }

    if (!hasReconstructedEditTextBackground) {
        // If we reach here then we just need to set a brand new instance of the Drawable
        // as the background. This has the unfortunate side-effect of wiping out any
        // user set padding, but I'd hope that use of custom padding on an EditText
        // is limited.
        editText.setBackgroundDrawable(newBg);
    }
}

From source file:android.support.v7.widget.SuggestionsAdapter.java

private void storeInIconCache(String resourceUri, Drawable drawable) {
    if (drawable != null) {
        mOutsideDrawablesCache.put(resourceUri, drawable.getConstantState());
    }/*from www  .  j a va2s .c o m*/
}

From source file:com.facebook.react.views.textinput.ReactTextInputManager.java

@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 {/*from   w  w  w.j a  v  a  2s .c o m*/
        drawableToMutate.setColorFilter(underlineColor, PorterDuff.Mode.SRC_IN);
    }
}

From source file:android.support.v7ox.widget.AppCompatDrawableManager.java

private boolean addCachedDelegateDrawable(@NonNull final Context context, final long key,
        @NonNull final Drawable drawable) {
    final ConstantState cs = drawable.getConstantState();
    if (cs != null) {
        synchronized (mDelegateDrawableCacheLock) {
            LongSparseArray<WeakReference<ConstantState>> cache = mDelegateDrawableCaches.get(context);
            if (cache == null) {
                cache = new LongSparseArray<>();
                mDelegateDrawableCaches.put(context, cache);
            }//from w w w.j  a va2  s . c  o  m
            cache.put(key, new WeakReference<ConstantState>(cs));
        }
        return true;
    }
    return false;
}

From source file:com.retroteam.studio.retrostudio.EditorLandscape.java

/**
 * Iterate through all the measures and return a list of measure tag group objects.
 * The list is serialized out to the .retro project file.
 * @return ArrayList<MeasureTagGroup>
 *///  w  ww  . j  a  v a  2  s.c  om

private ArrayList<MeasureTagGroup> getMeasureTags() {

    ArrayList<MeasureTagGroup> mtaglist = new ArrayList<>();

    LinearLayout track_layout = (LinearLayout) findViewById(R.id.track_layout);

    //iterate through all the measures
    for (int t = 0; t < track_layout.getChildCount(); t++) {
        //get hscrollview
        HorizontalScrollView hchild = (HorizontalScrollView) track_layout.getChildAt(t);
        for (int h = 0; h < hchild.getChildCount(); h++) {
            //get gridlayout
            GridLayout gchild = (GridLayout) hchild.getChildAt(h);
            for (int g = 0; g < gchild.getChildCount(); g++) {
                //get measure
                if (gchild.getChildAt(g) instanceof ImageView) {
                    ImageView ivchild = (ImageView) gchild.getChildAt(g);
                    Drawable measureFill = ivchild.getDrawable();
                    if (measureFill.getConstantState()
                            .equals(ContextCompat
                                    .getDrawable(getApplicationContext(), R.drawable.measure_new_empty)
                                    .getConstantState())) {
                        mtaglist.add(new MeasureTagGroup((int) ivchild.getTag(R.id.TAG_ROW),
                                (int) ivchild.getTag(R.id.TAG_COLUMN), false,
                                (int) ivchild.getTag(R.id.TAG_GUISNAP),
                                (ArrayList<int[]>) ivchild.getTag(R.id.TAG_FILLED_NOTES)));
                    } else if (measureFill.getConstantState()
                            .equals(ContextCompat
                                    .getDrawable(getApplicationContext(), R.drawable.measure_new_filled)
                                    .getConstantState())) {
                        mtaglist.add(new MeasureTagGroup((int) ivchild.getTag(R.id.TAG_ROW),
                                (int) ivchild.getTag(R.id.TAG_COLUMN), true,
                                (int) ivchild.getTag(R.id.TAG_GUISNAP),
                                (ArrayList<int[]>) ivchild.getTag(R.id.TAG_FILLED_NOTES)));
                    }
                }

            }
        }
    }

    return mtaglist;
}

From source file:android.support.v7.widget.SuggestionsAdapter.java

/**
 * 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./*from   w ww  .jav a2 s  .  co  m*/
 */
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;
}

From source file:com.iStudy.Study.ActionBarSherlock.Widget.SuggestionsAdapter.java

/**
 * 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.//from w  w  w  . j a  v  a 2s .  c o m
 */
@SuppressWarnings("unused")
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;
}