List of usage examples for android.util Xml asAttributeSet
public static AttributeSet asAttributeSet(XmlPullParser parser)
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)); }/* w w w . j a v a 2s.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 = 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.graphics.drawable.VectorDrawableCompat.java
/** * Create a VectorDrawableCompat object. * * @param res the resources.//w ww. jav a2 s .c o m * @param resId the resource ID for VectorDrawableCompat object. * @param theme the theme of this vector drawable, it can be null. * @return a new VectorDrawableCompat or null if parsing error is found. */ @Nullable public static VectorDrawableCompat create(@NonNull Resources res, @DrawableRes int resId, @Nullable Theme theme) { if (Build.VERSION.SDK_INT >= 23) { final VectorDrawableCompat drawable = new VectorDrawableCompat(); drawable.mDelegateDrawable = ResourcesCompat.getDrawable(res, resId, theme); drawable.mCachedConstantStateDelegate = new VectorDrawableDelegateState( drawable.mDelegateDrawable.getConstantState()); return drawable; } 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"); } return createFromXmlInner(res, parser, attrs, theme); } catch (XmlPullParserException e) { Log.e(LOGTAG, "parser error", e); } catch (IOException e) { Log.e(LOGTAG, "parser error", e); } return null; }
From source file:com.grottworkshop.gwsvectorsandboxlib.VectorDrawable.java
/** @hide */ static VectorDrawable create(Resources resources, int rid) { try {/* w w w.j a v a 2 s . co m*/ final XmlPullParser parser = resources.getXml(rid); 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 VectorDrawable drawable = new VectorDrawable(); drawable.inflate(resources, parser, attrs); return drawable; } catch (XmlPullParserException e) { Log.e(LOGTAG, "parser error", e); } catch (IOException e) { Log.e(LOGTAG, "parser error", e); } return null; }
From source file:com.anysoftkeyboard.addons.AddOnsFactory.java
private ArrayList<E> parseAddOnsFromXml(Context packContext, XmlPullParser xml) { final ArrayList<E> addOns = new ArrayList<>(); try {//from w w w . j a v a 2s .c o m int event; boolean inRoot = false; while ((event = xml.next()) != XmlPullParser.END_DOCUMENT) { final String tag = xml.getName(); if (event == XmlPullParser.START_TAG) { if (mRootNodeTag.equals(tag)) { inRoot = true; } else if (inRoot && mAddonNodeTag.equals(tag)) { final AttributeSet attrs = Xml.asAttributeSet(xml); E addOn = createAddOnFromXmlAttributes(attrs, packContext); if (addOn != null) { addOns.add(addOn); } } } else if (event == XmlPullParser.END_TAG) { if (mRootNodeTag.equals(tag)) { inRoot = false; break; } } } } catch (final IOException e) { Logger.e(mTag, "IO error:" + e); e.printStackTrace(); } catch (final XmlPullParserException e) { Logger.e(mTag, "Parse error:" + e); e.printStackTrace(); } return addOns; }
From source file:com.achep.base.ui.activities.SettingsActivity.java
/** * Parse the given XML file as a categories description, adding each * parsed categories and tiles into the target list. * * @param resourceId The XML resource to load and parse. * @param target The list in which the parsed categories and tiles should be placed. *///ww w . j a v a2 s .c o m protected final void loadDashboardFromResource(@XmlRes int resourceId, @NonNull List<DashboardCategory> target) { XmlResourceParser parser = null; try { parser = getResources().getXml(resourceId); AttributeSet attrs = Xml.asAttributeSet(parser); int type; for (type = parser.next(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) { if (type == XmlPullParser.START_TAG) break; } String nodeName = parser.getName(); if (!RESOURCE_TAG_DASHBOARD.equals(nodeName)) throw new RuntimeException(String.format("XML document must start with <%s> tag; found %s at %s", RESOURCE_TAG_DASHBOARD, nodeName, parser.getPositionDescription())); for (type = parser.next(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) { if (type == XmlPullParser.END_TAG && parser.getDepth() <= 1 /* root tag */) break; if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { continue; } switch (parser.getName()) { case RESOURCE_TAG_DASHBOARD_CATEGORY: DashboardCategory category = new DashboardCategory(this, attrs); final int categoryDepth = parser.getDepth(); for (type = parser.next(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) { if (type == XmlPullParser.END_TAG && parser.getDepth() <= categoryDepth) break; if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { continue; } switch (parser.getName()) { case RESOURCE_TAG_DASHBOARD_TILE: DashboardTile tile = new DashboardTile(this, attrs); Bundle bundle = null; final int tileDepth = parser.getDepth(); for (type = parser.next(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) { if (type == XmlPullParser.END_TAG && parser.getDepth() <= tileDepth) break; if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { continue; } switch (parser.getName()) { case RESOURCE_TAG_DASHBOARD_TILE_EXTRA: if (bundle == null) { bundle = new Bundle(); } getResources().parseBundleExtra(RESOURCE_TAG_DASHBOARD_TILE_EXTRA, attrs, bundle); XmlUtils.skipCurrentTag(parser); break; case RESOURCE_TAG_DASHBOARD_TILE_INTENT: tile.intent = Intent.parseIntent(getResources(), parser, attrs); break; default: XmlUtils.skipCurrentTag(parser); } } tile.fragmentArguments = bundle; category.add(tile); break; default: XmlUtils.skipCurrentTag(parser); } } target.add(category); break; default: XmlUtils.skipCurrentTag(parser); } } } catch (XmlPullParserException | IOException e) { throw new RuntimeException("Error parsing categories", e); } finally { if (parser != null) parser.close(); } }
From source file:com.hippo.vector.VectorDrawable.java
public static VectorDrawable create(Context context, int rid) { try {// w ww . j a v a2s . co m final XmlPullParser parser = context.getResources().getXml(rid); 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 name = parser.getName(); if (!SHAPE_VECTOR.equals(name)) { throw new IllegalStateException("It is not vector"); } final VectorDrawable drawable = new VectorDrawable(); drawable.inflate(context, parser, attrs); return drawable; } catch (XmlPullParserException e) { Log.e(LOGTAG, "parser error", e); } catch (IOException e) { Log.e(LOGTAG, "parser error", e); } return null; }
From source file:com.bettervectordrawable.lib.graphics.drawable.VectorDrawable.java
/** @hide */ public static VectorDrawable create(Resources resources, int rid) { try {// ww w . j av a2 s . c om final XmlPullParser parser = resources.getXml(rid); 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 VectorDrawable drawable = new VectorDrawable(); drawable.inflate(resources, parser, attrs); return drawable; } catch (XmlPullParserException e) { Log.e(LOGTAG, "parser error", e); } catch (IOException e) { Log.e(LOGTAG, "parser error", e); } return null; }
From source file:android.support.graphics.drawable.AnimatorInflaterCompat.java
private static Animator createAnimatorFromXml(Context context, Resources res, Theme theme, XmlPullParser parser, float pixelSize) throws XmlPullParserException, IOException { return createAnimatorFromXml(context, res, theme, parser, Xml.asAttributeSet(parser), null, 0, pixelSize); }
From source file:android.support.graphics.drawable.AnimatorInflaterCompat.java
private static Animator createAnimatorFromXml(Context context, Resources res, Theme theme, XmlPullParser parser, AttributeSet attrs, AnimatorSet parent, int sequenceOrdering, float pixelSize) throws XmlPullParserException, IOException { Animator anim = null;/* w ww . j a v a 2 s. c o m*/ ArrayList<Animator> childAnims = null; // Make sure we are on a start tag. int type; int depth = parser.getDepth(); while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { if (type != XmlPullParser.START_TAG) { continue; } String name = parser.getName(); boolean gotValues = false; if (name.equals("objectAnimator")) { anim = loadObjectAnimator(context, res, theme, attrs, pixelSize, parser); } else if (name.equals("animator")) { anim = loadAnimator(context, res, theme, attrs, null, pixelSize, parser); } else if (name.equals("set")) { anim = new AnimatorSet(); TypedArray a = TypedArrayUtils.obtainAttributes(res, theme, attrs, AndroidResources.STYLEABLE_ANIMATOR_SET); int ordering = TypedArrayUtils.getNamedInt(a, parser, "ordering", AndroidResources.STYLEABLE_ANIMATOR_SET_ORDERING, TOGETHER); createAnimatorFromXml(context, res, theme, parser, attrs, (AnimatorSet) anim, ordering, pixelSize); a.recycle(); } else if (name.equals("propertyValuesHolder")) { PropertyValuesHolder[] values = loadValues(context, res, theme, parser, Xml.asAttributeSet(parser)); if (values != null && anim != null && (anim instanceof ValueAnimator)) { ((ValueAnimator) anim).setValues(values); } gotValues = true; } else { throw new RuntimeException("Unknown animator name: " + parser.getName()); } if (parent != null && !gotValues) { if (childAnims == null) { childAnims = new ArrayList<Animator>(); } childAnims.add(anim); } } if (parent != null && childAnims != null) { Animator[] animsArray = new Animator[childAnims.size()]; int index = 0; for (Animator a : childAnims) { animsArray[index++] = a; } if (sequenceOrdering == TOGETHER) { parent.playTogether(animsArray); } else { parent.playSequentially(animsArray); } } return anim; }
From source file:android.support.graphics.drawable.AnimatorInflaterCompat.java
private static PropertyValuesHolder loadPvh(Context context, Resources res, Theme theme, XmlPullParser parser, String propertyName, int valueType) throws XmlPullParserException, IOException { PropertyValuesHolder value = null;// w ww .j a va2s . c o m ArrayList<Keyframe> keyframes = null; int type; while ((type = parser.next()) != XmlPullParser.END_TAG && type != XmlPullParser.END_DOCUMENT) { String name = parser.getName(); if (name.equals("keyframe")) { if (valueType == VALUE_TYPE_UNDEFINED) { valueType = inferValueTypeOfKeyframe(res, theme, Xml.asAttributeSet(parser), parser); } Keyframe keyframe = loadKeyframe(context, res, theme, Xml.asAttributeSet(parser), valueType, parser); if (keyframe != null) { if (keyframes == null) { keyframes = new ArrayList<Keyframe>(); } keyframes.add(keyframe); } parser.next(); } } int count; if (keyframes != null && (count = keyframes.size()) > 0) { // make sure we have keyframes at 0 and 1 // If we have keyframes with set fractions, add keyframes at start/end // appropriately. If start/end have no set fractions: // if there's only one keyframe, set its fraction to 1 and add one at 0 // if >1 keyframe, set the last fraction to 1, the first fraction to 0 Keyframe firstKeyframe = keyframes.get(0); Keyframe lastKeyframe = keyframes.get(count - 1); float endFraction = lastKeyframe.getFraction(); if (endFraction < 1) { if (endFraction < 0) { lastKeyframe.setFraction(1); } else { keyframes.add(keyframes.size(), createNewKeyframe(lastKeyframe, 1)); ++count; } } float startFraction = firstKeyframe.getFraction(); if (startFraction != 0) { if (startFraction < 0) { firstKeyframe.setFraction(0); } else { keyframes.add(0, createNewKeyframe(firstKeyframe, 0)); ++count; } } Keyframe[] keyframeArray = new Keyframe[count]; keyframes.toArray(keyframeArray); for (int i = 0; i < count; ++i) { Keyframe keyframe = keyframeArray[i]; if (keyframe.getFraction() < 0) { if (i == 0) { keyframe.setFraction(0); } else if (i == count - 1) { keyframe.setFraction(1); } else { // figure out the start/end parameters of the current gap // in fractions and distribute the gap among those keyframes int startIndex = i; int endIndex = i; for (int j = startIndex + 1; j < count - 1; ++j) { if (keyframeArray[j].getFraction() >= 0) { break; } endIndex = j; } float gap = keyframeArray[endIndex + 1].getFraction() - keyframeArray[startIndex - 1].getFraction(); distributeKeyframes(keyframeArray, gap, startIndex, endIndex); } } } value = PropertyValuesHolder.ofKeyframe(propertyName, keyframeArray); if (valueType == VALUE_TYPE_COLOR) { value.setEvaluator(ArgbEvaluator.getInstance()); } } return value; }