Example usage for android.graphics.drawable Drawable setChangingConfigurations

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

Introduction

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

Prototype

public void setChangingConfigurations(@Config int configs) 

Source Link

Document

Set a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.

Usage

From source file:Main.java

/**
 * Copies various properties from one drawable to the other.
 * @param to drawable to copy properties to
 * @param from drawable to copy properties from
 *///from  w  w w.  j a  va 2s . co  m
public static void copyProperties(Drawable to, Drawable from) {
    if (from == null || to == null || to == from) {
        return;
    }

    to.setBounds(from.getBounds());
    to.setChangingConfigurations(from.getChangingConfigurations());
    to.setLevel(from.getLevel());
    to.setVisible(from.isVisible(), /* restart */false);
    to.setState(from.getState());
}

From source file:com.negusoft.greenmatter.drawable.CompoundDrawableWrapper.java

@Override
public void setChangingConfigurations(int configs) {
    for (Drawable d : mSecondaryDrawables)
        d.setChangingConfigurations(configs);
    super.setChangingConfigurations(configs);
}

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

private Drawable createDrawableIfNeeded(@NonNull Context context, @DrawableRes final int resId) {
    if (mTypedValue == null) {
        mTypedValue = new TypedValue();
    }//from  w ww. j  a  v a 2  s . co m
    final TypedValue tv = mTypedValue;
    context.getResources().getValue(resId, tv, true);
    final long key = createCacheKey(tv);

    Drawable dr = getCachedDrawable(context, key);
    if (dr != null) {
        // If we got a cached drawable, return it
        return dr;
    }

    // Else we need to try and create one...
    if (resId == R.drawable.abc_cab_background_top_material) {
        dr = new LayerDrawable(new Drawable[] { getDrawable(context, R.drawable.abc_cab_background_internal_bg),
                getDrawable(context, R.drawable.abc_cab_background_top_mtrl_alpha) });
    }

    if (dr != null) {
        dr.setChangingConfigurations(tv.changingConfigurations);
        // If we reached here then we created a new drawable, add it to the cache
        addDrawableToCache(context, key, dr);
    }

    return dr;
}

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

private Drawable loadDrawableFromDelegates(@NonNull Context context, @DrawableRes int resId) {
    if (mDelegates != null && !mDelegates.isEmpty()) {
        if (mKnownDrawableIdTags != null) {
            final String cachedTagName = mKnownDrawableIdTags.get(resId);
            if (SKIP_DRAWABLE_TAG.equals(cachedTagName)
                    || (cachedTagName != null && mDelegates.get(cachedTagName) == null)) {
                // If we don't have a delegate for the drawable tag, or we've been set to
                // skip it, fail fast and return null
                if (DEBUG) {
                    Log.d(TAG, "[loadDrawableFromDelegates] Skipping drawable: "
                            + context.getResources().getResourceName(resId));
                }/* ww w.j  a  v  a2 s .c om*/
                return null;
            }
        } else {
            // Create an id cache as we'll need one later
            mKnownDrawableIdTags = new SparseArray<>();
        }

        if (mTypedValue == null) {
            mTypedValue = new TypedValue();
        }
        final TypedValue tv = mTypedValue;
        final Resources res = context.getResources();
        res.getValue(resId, tv, true);

        final long key = createCacheKey(tv);

        Drawable dr = getCachedDrawable(context, key);
        if (dr != null) {
            if (DEBUG) {
                Log.i(TAG, "[loadDrawableFromDelegates] Returning cached drawable: "
                        + context.getResources().getResourceName(resId));
            }
            // We have a cached drawable, return it!
            return dr;
        }

        if (tv.string != null && tv.string.toString().endsWith(".xml")) {
            // If the resource is an XML file, let's try and parse it
            try {
                final XmlPullParser parser = res.getXml(resId);
                final AttributeSet attrs = Xml.asAttributeSet(parser);
                int type;
                while ((type = parser.next()) != XmlPullParser.START_TAG
                        && type != XmlPullParser.END_DOCUMENT) {
                    // Empty loop
                }
                if (type != XmlPullParser.START_TAG) {
                    throw new XmlPullParserException("No start tag found");
                }

                final String tagName = parser.getName();
                // Add the tag name to the cache
                mKnownDrawableIdTags.append(resId, tagName);

                // Now try and find a delegate for the tag name and inflate if found
                final InflateDelegate delegate = mDelegates.get(tagName);
                if (delegate != null) {
                    dr = delegate.createFromXmlInner(context, parser, attrs, context.getTheme());
                }
                if (dr != null) {
                    // Add it to the drawable cache
                    dr.setChangingConfigurations(tv.changingConfigurations);
                    if (addDrawableToCache(context, key, dr) && DEBUG) {
                        Log.i(TAG, "[loadDrawableFromDelegates] Saved drawable to cache: "
                                + context.getResources().getResourceName(resId));
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, "Exception while inflating drawable", e);
            }
        }
        if (dr == null) {
            // If we reach here then the delegate inflation of the resource failed. Mark it as
            // bad so we skip the id next time
            mKnownDrawableIdTags.append(resId, SKIP_DRAWABLE_TAG);
        }
        return dr;
    }

    return null;
}

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

private Drawable loadDrawableFromDelegates(@NonNull Context context, @DrawableRes int resId) {
    if (mDelegates != null && !mDelegates.isEmpty()) {
        if (mKnownDrawableIdTags != null) {
            final String cachedTagName = mKnownDrawableIdTags.get(resId);
            if (SKIP_DRAWABLE_TAG.equals(cachedTagName)
                    || (cachedTagName != null && mDelegates.get(cachedTagName) == null)) {
                // If we don't have a delegate for the drawable tag, or we've been set to
                // skip it, fail fast and return null
                if (DEBUG) {
                    Log.d(TAG, "[loadDrawableFromDelegates] Skipping drawable: "
                            + context.getResources().getResourceName(resId));
                }// www .j a  v  a  2  s . c  o  m
                return null;
            }
        } else {
            // Create an id cache as we'll need one later
            mKnownDrawableIdTags = new SparseArray<>();
        }

        if (mTypedValue == null) {
            mTypedValue = new TypedValue();
        }

        final TypedValue tv = mTypedValue;
        final Resources res = context.getResources();
        res.getValue(resId, tv, true);

        final long key = (((long) tv.assetCookie) << 32) | tv.data;

        Drawable dr = getCachedDelegateDrawable(context, key);
        if (dr != null) {
            if (DEBUG) {
                Log.i(TAG, "[loadDrawableFromDelegates] Returning cached drawable: "
                        + context.getResources().getResourceName(resId));
            }
            // We have a cached drawable, return it!
            return dr;
        }

        if (tv.string != null && tv.string.toString().endsWith(".xml")) {
            // If the resource is an XML file, let's try and parse it
            try {
                final XmlPullParser parser = res.getXml(resId);
                final AttributeSet attrs = Xml.asAttributeSet(parser);
                int type;
                while ((type = parser.next()) != XmlPullParser.START_TAG
                        && type != XmlPullParser.END_DOCUMENT) {
                    // Empty loop
                }
                if (type != XmlPullParser.START_TAG) {
                    throw new XmlPullParserException("No start tag found");
                }

                final String tagName = parser.getName();
                // Add the tag name to the cache
                mKnownDrawableIdTags.append(resId, tagName);

                // Now try and find a delegate for the tag name and inflate if found
                final InflateDelegate delegate = mDelegates.get(tagName);
                if (delegate != null) {
                    dr = delegate.createFromXmlInner(context, parser, attrs, context.getTheme());
                }
                if (dr != null) {
                    // Add it to the drawable cache
                    dr.setChangingConfigurations(tv.changingConfigurations);
                    if (addCachedDelegateDrawable(context, key, dr) && DEBUG) {
                        Log.i(TAG, "[loadDrawableFromDelegates] Saved drawable to cache: "
                                + context.getResources().getResourceName(resId));
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, "Exception while inflating drawable", e);
            }
        }
        if (dr == null) {
            // If we reach here then the delegate inflation of the resource failed. Mark it as
            // bad so we skip the id next time
            mKnownDrawableIdTags.append(resId, SKIP_DRAWABLE_TAG);
        }
        return dr;
    }

    return null;
}

From source file:android.content.res.VectorResources.java

@Override
Drawable loadDrawable(TypedValue value, int id) throws NotFoundException {
    boolean isColorDrawable = false;
    if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        isColorDrawable = true;//from   www. j a v a2s.co  m
    }
    final long key = isColorDrawable ? value.data : (((long) value.assetCookie) << 32) | value.data;

    Drawable dr = getCachedDrawable(isColorDrawable ? mColorDrawableCache : mDrawableCache, key);

    if (dr != null) {
        return dr;
    }

    if (isColorDrawable) {
        dr = new ColorDrawable(value.data);
    }

    if (dr == null) {
        if (value.string == null) {
            throw new NotFoundException("Resource is not a Drawable (color or path): " + value);
        }

        String file = value.string.toString();

        if (file.endsWith(".xml")) {
            // Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
            try {
                XmlResourceParser rp = getXml(id);
                // XmlResourceParser rp = loadXmlResourceParser(
                //         file, id, value.assetCookie, "drawable");
                dr = createDrawableFromXml(rp);
                rp.close();
            } catch (Exception e) {
                // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
                NotFoundException rnf = new NotFoundException(
                        "File " + file + " from drawable resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(e);
                throw rnf;
            }
            // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);

        } else {
            // Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
            try {
                InputStream is = openRawResource(id, value);
                //InputStream is = mAssets.openNonAsset(
                //        value.assetCookie, file, AssetManager.ACCESS_STREAMING);
                //                System.out.println("Opened file " + file + ": " + is);
                dr = Drawable.createFromResourceStream(this, value, is, file, null);
                is.close();
                //                System.out.println("Created stream: " + dr);
            } catch (Exception e) {
                // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
                NotFoundException rnf = new NotFoundException(
                        "File " + file + " from drawable resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(e);
                throw rnf;
            }
            // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
        }
    }
    Drawable.ConstantState cs;
    if (dr != null) {
        dr.setChangingConfigurations(value.changingConfigurations);
        cs = dr.getConstantState();
        if (cs != null) {
            synchronized (mAccessLock) {
                //Log.i(TAG, "Saving cached drawable @ #" +
                //        Integer.toHexString(key.intValue())
                //        + " in " + this + ": " + cs);
                if (isColorDrawable) {
                    mColorDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
                } else {
                    mDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
                }
            }
        }
    }

    return dr;
}