Example #1
0
 LayerDrawable(LayerState state, Resources res) {
   LayerState as = createConstantState(state, res);
   mLayerState = as;
   if (as.mNum > 0) {
     ensurePadding();
   }
 }
  @Override
  public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
      throws XmlPullParserException, IOException {
    super.inflate(r, parser, attrs);

    int type;

    final int innerDepth = parser.getDepth() + 1;
    int depth;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
        && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
      if (type != XmlPullParser.START_TAG) {
        continue;
      }

      if (depth > innerDepth || !parser.getName().equals("item")) {
        continue;
      }

      TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.LayerDrawableItem);

      int left =
          a.getDimensionPixelOffset(com.android.internal.R.styleable.LayerDrawableItem_left, 0);
      int top =
          a.getDimensionPixelOffset(com.android.internal.R.styleable.LayerDrawableItem_top, 0);
      int right =
          a.getDimensionPixelOffset(com.android.internal.R.styleable.LayerDrawableItem_right, 0);
      int bottom =
          a.getDimensionPixelOffset(com.android.internal.R.styleable.LayerDrawableItem_bottom, 0);
      int drawableRes =
          a.getResourceId(com.android.internal.R.styleable.LayerDrawableItem_drawable, 0);
      int id = a.getResourceId(com.android.internal.R.styleable.LayerDrawableItem_id, View.NO_ID);

      a.recycle();

      Drawable dr;
      if (drawableRes != 0) {
        dr = r.getDrawable(drawableRes);
      } else {
        while ((type = parser.next()) == XmlPullParser.TEXT) {}
        if (type != XmlPullParser.START_TAG) {
          throw new XmlPullParserException(
              parser.getPositionDescription()
                  + ": <item> tag requires a 'drawable' attribute or "
                  + "child tag defining a drawable");
        }
        dr = Drawable.createFromXmlInner(r, parser, attrs);
      }

      addLayer(dr, id, left, top, right, bottom);
    }

    ensurePadding();
    onStateChange(getState());
  }
Example #3
0
  /**
   * Create a new layer drawable with the specified list of layers and the specified constant state.
   *
   * @param layers The list of layers to add to this drawable.
   * @param state The constant drawable state.
   */
  LayerDrawable(Drawable[] layers, LayerState state) {
    this(state, null);
    int length = layers.length;
    ChildDrawable[] r = new ChildDrawable[length];

    for (int i = 0; i < length; i++) {
      r[i] = new ChildDrawable();
      r[i].mDrawable = layers[i];
      layers[i].setCallback(this);
      mLayerState.mChildrenChangingConfigurations |= layers[i].getChangingConfigurations();
    }
    mLayerState.mNum = length;
    mLayerState.mChildren = r;

    ensurePadding();
  }