Search in sources :

Example 76 with TypedArray

use of android.content.res.TypedArray in project Conversations by siacs.

the class UnreadCountCustomView method initXMLAttrs.

private void initXMLAttrs(Context context, AttributeSet attrs) {
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UnreadCountCustomView);
    setBackgroundColor(a.getColor(a.getIndex(0), ContextCompat.getColor(context, R.color.unreadcountlight)));
    a.recycle();
}
Also used : TypedArray(android.content.res.TypedArray)

Example 77 with TypedArray

use of android.content.res.TypedArray in project StylishMusicPlayer by ryanhoo.

the class CharacterView method init.

private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CharacterView, defStyleAttr, defStyleRes);
    try {
        mCharacter = typedArray.getString(R.styleable.CharacterView_character);
        mCharacterTextColor = typedArray.getColor(R.styleable.CharacterView_characterTextColor, 0);
        mBackgroundRoundAsCircle = typedArray.getBoolean(R.styleable.CharacterView_backgroundRoundAsCircle, false);
        mBackgroundColor = typedArray.getColor(R.styleable.CharacterView_backgroundColor, 0);
        mBackgroundRadius = typedArray.getDimension(R.styleable.CharacterView_backgroundRadius, 0);
        mCharacterPadding = typedArray.getDimension(R.styleable.CharacterView_characterPadding, 0);
        mDrawable = new CharacterDrawable.Builder().setCharacter(mCharacter).setCharacterTextColor(mCharacterTextColor).setBackgroundRoundAsCircle(mBackgroundRoundAsCircle).setBackgroundColor(mBackgroundColor).setBackgroundRadius(mBackgroundRadius).setCharacterPadding(mCharacterPadding).build();
    } finally {
        typedArray.recycle();
    }
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
Also used : TypedArray(android.content.res.TypedArray)

Example 78 with TypedArray

use of android.content.res.TypedArray in project android-ripple-background by skyfishjy.

the class RippleBackground method init.

private void init(final Context context, final AttributeSet attrs) {
    if (isInEditMode())
        return;
    if (null == attrs) {
        throw new IllegalArgumentException("Attributes should be provided to this view,");
    }
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RippleBackground);
    rippleColor = typedArray.getColor(R.styleable.RippleBackground_rb_color, getResources().getColor(R.color.rippelColor));
    rippleStrokeWidth = typedArray.getDimension(R.styleable.RippleBackground_rb_strokeWidth, getResources().getDimension(R.dimen.rippleStrokeWidth));
    rippleRadius = typedArray.getDimension(R.styleable.RippleBackground_rb_radius, getResources().getDimension(R.dimen.rippleRadius));
    rippleDurationTime = typedArray.getInt(R.styleable.RippleBackground_rb_duration, DEFAULT_DURATION_TIME);
    rippleAmount = typedArray.getInt(R.styleable.RippleBackground_rb_rippleAmount, DEFAULT_RIPPLE_COUNT);
    rippleScale = typedArray.getFloat(R.styleable.RippleBackground_rb_scale, DEFAULT_SCALE);
    rippleType = typedArray.getInt(R.styleable.RippleBackground_rb_type, DEFAULT_FILL_TYPE);
    typedArray.recycle();
    rippleDelay = rippleDurationTime / rippleAmount;
    paint = new Paint();
    paint.setAntiAlias(true);
    if (rippleType == DEFAULT_FILL_TYPE) {
        rippleStrokeWidth = 0;
        paint.setStyle(Paint.Style.FILL);
    } else
        paint.setStyle(Paint.Style.STROKE);
    paint.setColor(rippleColor);
    rippleParams = new LayoutParams((int) (2 * (rippleRadius + rippleStrokeWidth)), (int) (2 * (rippleRadius + rippleStrokeWidth)));
    rippleParams.addRule(CENTER_IN_PARENT, TRUE);
    animatorSet = new AnimatorSet();
    animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
    animatorList = new ArrayList<Animator>();
    for (int i = 0; i < rippleAmount; i++) {
        RippleView rippleView = new RippleView(getContext());
        addView(rippleView, rippleParams);
        rippleViewList.add(rippleView);
        final ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(rippleView, "ScaleX", 1.0f, rippleScale);
        scaleXAnimator.setRepeatCount(ObjectAnimator.INFINITE);
        scaleXAnimator.setRepeatMode(ObjectAnimator.RESTART);
        scaleXAnimator.setStartDelay(i * rippleDelay);
        scaleXAnimator.setDuration(rippleDurationTime);
        animatorList.add(scaleXAnimator);
        final ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(rippleView, "ScaleY", 1.0f, rippleScale);
        scaleYAnimator.setRepeatCount(ObjectAnimator.INFINITE);
        scaleYAnimator.setRepeatMode(ObjectAnimator.RESTART);
        scaleYAnimator.setStartDelay(i * rippleDelay);
        scaleYAnimator.setDuration(rippleDurationTime);
        animatorList.add(scaleYAnimator);
        final ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(rippleView, "Alpha", 1.0f, 0f);
        alphaAnimator.setRepeatCount(ObjectAnimator.INFINITE);
        alphaAnimator.setRepeatMode(ObjectAnimator.RESTART);
        alphaAnimator.setStartDelay(i * rippleDelay);
        alphaAnimator.setDuration(rippleDurationTime);
        animatorList.add(alphaAnimator);
    }
    animatorSet.playTogether(animatorList);
}
Also used : ObjectAnimator(android.animation.ObjectAnimator) Animator(android.animation.Animator) ObjectAnimator(android.animation.ObjectAnimator) TypedArray(android.content.res.TypedArray) AccelerateDecelerateInterpolator(android.view.animation.AccelerateDecelerateInterpolator) AnimatorSet(android.animation.AnimatorSet) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 79 with TypedArray

use of android.content.res.TypedArray in project platform_frameworks_base by android.

the class BridgeInflater method createViewFromTag.

@Override
public View createViewFromTag(View parent, String name, Context context, AttributeSet attrs, boolean ignoreThemeAttr) {
    View view;
    try {
        view = super.createViewFromTag(parent, name, context, attrs, ignoreThemeAttr);
    } catch (InflateException e) {
        // Apply a theme wrapper, if allowed and one is specified.
        if (!ignoreThemeAttr) {
            final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
            final int themeResId = ta.getResourceId(0, 0);
            if (themeResId != 0) {
                context = new ContextThemeWrapper(context, themeResId);
            }
            ta.recycle();
        }
        if (!(e.getCause() instanceof ClassNotFoundException)) {
            // There is some unknown inflation exception in inflating a View that was found.
            view = new MockView(context, attrs);
            ((MockView) view).setText(name);
            Bridge.getLog().error(LayoutLog.TAG_BROKEN, e.getMessage(), e, null);
        } else {
            final Object lastContext = mConstructorArgs[0];
            mConstructorArgs[0] = context;
            // try to load the class from using the custom view loader
            try {
                view = loadCustomView(name, attrs);
            } catch (Exception e2) {
                // Wrap the real exception in an InflateException so that the calling
                // method can deal with it.
                InflateException exception = new InflateException();
                if (!e2.getClass().equals(ClassNotFoundException.class)) {
                    exception.initCause(e2);
                } else {
                    exception.initCause(e);
                }
                throw exception;
            } finally {
                mConstructorArgs[0] = lastContext;
            }
        }
    }
    setupViewInContext(view, attrs);
    return view;
}
Also used : MockView(com.android.layoutlib.bridge.MockView) TypedArray(android.content.res.TypedArray) MockView(com.android.layoutlib.bridge.MockView)

Example 80 with TypedArray

use of android.content.res.TypedArray in project platform_frameworks_base by android.

the class LayoutInflater_Delegate method parseInclude.

@LayoutlibDelegate
public static void parseInclude(LayoutInflater thisInflater, XmlPullParser parser, Context context, View parent, AttributeSet attrs) throws XmlPullParserException, IOException {
    int type;
    if (parent instanceof ViewGroup) {
        // Apply a theme wrapper, if requested. This is sort of a weird
        // edge case, since developers think the <include> overwrites
        // values in the AttributeSet of the included View. So, if the
        // included View has a theme attribute, we'll need to ignore it.
        final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
        final int themeResId = ta.getResourceId(0, 0);
        final boolean hasThemeOverride = themeResId != 0;
        if (hasThemeOverride) {
            context = new ContextThemeWrapper(context, themeResId);
        }
        ta.recycle();
        // If the layout is pointing to a theme attribute, we have to
        // massage the value to get a resource identifier out of it.
        int layout = attrs.getAttributeResourceValue(null, ATTR_LAYOUT, 0);
        if (layout == 0) {
            final String value = attrs.getAttributeValue(null, ATTR_LAYOUT);
            if (value == null || value.length() <= 0) {
                throw new InflateException("You must specify a layout in the" + " include tag: <include layout=\"@layout/layoutID\" />");
            }
            // Attempt to resolve the "?attr/name" string to an identifier.
            layout = context.getResources().getIdentifier(value.substring(1), null, null);
        }
        // ---- START CHANGES
        if (layout != 0) {
            final TypedValue tempValue = new TypedValue();
            if (context.getTheme().resolveAttribute(layout, tempValue, true)) {
                layout = tempValue.resourceId;
            }
        }
        if (layout == 0) {
            final String value = attrs.getAttributeValue(null, ATTR_LAYOUT);
            if (value == null) {
                throw new InflateException("You must specifiy a layout in the" + " include tag: <include layout=\"@layout/layoutID\" />");
            } else {
                throw new InflateException("You must specifiy a valid layout " + "reference. The layout ID " + value + " is not valid.");
            }
        } else {
            final XmlResourceParser childParser = thisInflater.getContext().getResources().getLayout(layout);
            try {
                final AttributeSet childAttrs = Xml.asAttributeSet(childParser);
                while ((type = childParser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
                // Empty.
                }
                if (type != XmlPullParser.START_TAG) {
                    throw new InflateException(childParser.getPositionDescription() + ": No start tag found!");
                }
                final String childName = childParser.getName();
                if (TAG_MERGE.equals(childName)) {
                    // Inflate all children.
                    thisInflater.rInflate(childParser, parent, context, childAttrs, false);
                } else {
                    final View view = thisInflater.createViewFromTag(parent, childName, context, childAttrs, hasThemeOverride);
                    final ViewGroup group = (ViewGroup) parent;
                    final TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Include);
                    final int id = a.getResourceId(com.android.internal.R.styleable.Include_id, View.NO_ID);
                    final int visibility = a.getInt(com.android.internal.R.styleable.Include_visibility, -1);
                    a.recycle();
                    // We try to load the layout params set in the <include /> tag. If
                    // they don't exist, we will rely on the layout params set in the
                    // included XML file.
                    // During a layoutparams generation, a runtime exception is thrown
                    // if either layout_width or layout_height is missing. We catch
                    // this exception and set localParams accordingly: true means we
                    // successfully loaded layout params from the <include /> tag,
                    // false means we need to rely on the included layout params.
                    ViewGroup.LayoutParams params = null;
                    try {
                        // ---- START CHANGES
                        sIsInInclude = true;
                        // ---- END CHANGES
                        params = group.generateLayoutParams(attrs);
                    } catch (RuntimeException ignored) {
                    // Ignore, just fail over to child attrs.
                    } finally {
                        // ---- START CHANGES
                        sIsInInclude = false;
                    // ---- END CHANGES
                    }
                    if (params == null) {
                        params = group.generateLayoutParams(childAttrs);
                    }
                    view.setLayoutParams(params);
                    // Inflate all children.
                    thisInflater.rInflateChildren(childParser, view, childAttrs, true);
                    if (id != View.NO_ID) {
                        view.setId(id);
                    }
                    switch(visibility) {
                        case 0:
                            view.setVisibility(View.VISIBLE);
                            break;
                        case 1:
                            view.setVisibility(View.INVISIBLE);
                            break;
                        case 2:
                            view.setVisibility(View.GONE);
                            break;
                    }
                    group.addView(view);
                }
            } finally {
                childParser.close();
            }
        }
    } else {
        throw new InflateException("<include /> can only be used inside of a ViewGroup");
    }
    LayoutInflater.consumeChildElements(parser);
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) TypedArray(android.content.res.TypedArray) TypedValue(android.util.TypedValue) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Aggregations

TypedArray (android.content.res.TypedArray)2031 Paint (android.graphics.Paint)193 TypedValue (android.util.TypedValue)190 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)162 Drawable (android.graphics.drawable.Drawable)119 Resources (android.content.res.Resources)118 View (android.view.View)117 Context (android.content.Context)80 XmlResourceParser (android.content.res.XmlResourceParser)80 ColorStateList (android.content.res.ColorStateList)78 AttributeSet (android.util.AttributeSet)78 IOException (java.io.IOException)77 SuppressLint (android.annotation.SuppressLint)66 TextPaint (android.text.TextPaint)63 TextView (android.widget.TextView)63 ViewGroup (android.view.ViewGroup)59 Bundle (android.os.Bundle)48 Point (android.graphics.Point)43 LayoutInflater (android.view.LayoutInflater)42 ImageView (android.widget.ImageView)40