Java Code Examples for android.graphics.drawable.LayerDrawable#setId()

The following examples show how to use android.graphics.drawable.LayerDrawable#setId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DrawableValue.java    From proteus with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(ProteusView view, Context context, ProteusLayoutInflater.ImageLoader loader, Callback callback) {
  final Drawable[] drawables = new Drawable[layers.length];
  int index = 0;
  for (Value layer : layers) {
    drawables[index] = DrawableResourceProcessor.evaluate(layer, view);
    index++;
  }

  LayerDrawable layerDrawable = new LayerDrawable(drawables);

  // we could have avoided the following loop if layer drawable
  // had a method to add drawable and set id at same time
  for (int i = 0; i < drawables.length; i++) {
    layerDrawable.setId(i, ids[i]);
  }

  callback.apply(layerDrawable);
}
 
Example 2
Source File: WorldMapSeekBar.java    From SuntimesWidget with GNU General Public License v3.0 6 votes vote down vote up
private void initDrawables(@NonNull Context context)
{
    majorTick = ContextCompat.getDrawable(context, R.drawable.ic_tick);
    minorTick = ContextCompat.getDrawable(context, R.drawable.ic_tick);
    centerTick = ContextCompat.getDrawable(context, R.drawable.ic_tick_center);
    initTick(majorTick, true);
    initTick(minorTick, false);
    initTick(centerTick, true, centerTickColor);

    Drawable background = ContextCompat.getDrawable(context, R.drawable.seekbar_background);
    SuntimesUtils.tintDrawable(background, trackColor, trackColor, 0);

    Drawable secondaryProgress = new ColorDrawable(Color.TRANSPARENT);
    Drawable primaryProgress = new ScaleDrawable(new ColorDrawable(Color.TRANSPARENT), Gravity.START, 1, -1);

    LayerDrawable progressDrawable = new LayerDrawable(new Drawable[] { background, secondaryProgress, primaryProgress });
    progressDrawable.setId(0, android.R.id.background);
    progressDrawable.setId(1, android.R.id.secondaryProgress);
    progressDrawable.setId(2, android.R.id.progress);

    Rect bounds = getProgressDrawable().getBounds();
    setProgressDrawable(progressDrawable);
    getProgressDrawable().setBounds(bounds);
}
 
Example 3
Source File: PXDrawableUtil.java    From pixate-freestyle-android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets a {@link Bitmap} background. In case the call is set to check for a
 * layer-drawable and there is an existing {@link LayerDrawable} on the
 * given View, set/replace the layer with the
 * {@code android.R.id.background} id.
 * 
 * @param view
 * @param bitmap
 * @param checkForLayer Indicate if this method should check for a
 *            {@link LayerDrawable} when applying a background.
 */
public static void setBackground(View view, Bitmap bitmap, boolean checkForLayer) {
    if (view instanceof ImageView) {
        ((ImageView) view).setImageBitmap(bitmap);
        return;
    }
    BitmapDrawable newDrawable = new BitmapDrawable(PixateFreestyle.getAppContext()
            .getResources(), bitmap);
    Drawable background = view.getBackground();
    if (background instanceof ColorDrawable) {
        // keep the background color so it would show when the bitmap is
        // transparent
        LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { background,
                newDrawable });
        layerDrawable.setId(1, android.R.id.background);
        setBackgroundDrawable(view, layerDrawable);
    } else {
        setBackgroundDrawable(view, newDrawable, checkForLayer);
    }
}
 
Example 4
Source File: FixedRatingBar.java    From proteus with Apache License 2.0 5 votes vote down vote up
/**
 * Taken from AOSP !!
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 */
public Drawable getTiledDrawable(Drawable drawable, boolean clip) {

  if (drawable instanceof LayerDrawable) {
    LayerDrawable background = (LayerDrawable) drawable;
    final int N = background.getNumberOfLayers();
    Drawable[] outDrawables = new Drawable[N];

    for (int i = 0; i < N; i++) {
      int id = background.getId(i);
      outDrawables[i] = getTiledDrawable(background.getDrawable(i),
        (id == android.R.id.progress || id == android.R.id.secondaryProgress));
    }

    LayerDrawable newBg = new LayerDrawable(outDrawables);

    for (int i = 0; i < N; i++) {
      newBg.setId(i, background.getId(i));
    }

    return newBg;

  } else if (drawable instanceof BitmapDrawable) {

    final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
    if (sampleTile == null) {
      sampleTile = tileBitmap;
    }
    final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());

    final BitmapShader bitmapShader = new BitmapShader(tileBitmap,
      Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
    shapeDrawable.getPaint().setShader(bitmapShader);

    return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
      ClipDrawable.HORIZONTAL) : shapeDrawable;
  }

  return drawable;
}
 
Example 5
Source File: ZProgressBar.java    From AccountBook with GNU General Public License v3.0 5 votes vote down vote up
private void createDrawable(){
    Drawable[] layers = new Drawable[2];
    Drawable background = makeBackground();
    Drawable progress = makeProgress();
    ClipDrawable clip = new ClipDrawable(progress
            , Gravity.LEFT, ClipDrawable.HORIZONTAL);
    layers[0] = background;
    layers[1] = clip;
    LayerDrawable layer = new LayerDrawable(layers);
    layer.setId(0, android.R.id.background);
    layer.setId(1, android.R.id.progress);
    setProgressDrawable(layer);
}
 
Example 6
Source File: BadgerTest.java    From Badger with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void sett_on_LayerDrawable_with_invalid_badge() {
    LayerDrawable layer = new LayerDrawable(new Drawable[]{new TestDrawable(), new TestDrawable()});
    layer.setId(0, R.id.badger_drawable);

    Badger.sett(layer, factory);
}
 
Example 7
Source File: BadgerTest.java    From Badger with Apache License 2.0 5 votes vote down vote up
@Test
public void sett_on_LayerDrawable_with_valid_badge() {
    Drawable[] drawables = {factory.createBadge(), new TestDrawable()};
    LayerDrawable layer = new LayerDrawable(drawables);
    layer.setId(0, R.id.badger_drawable);

    Badger<TestBadgeDrawable> badger = Badger.sett(layer, factory);

    assertBadged(badger.drawable, badger.badge, drawables);
}
 
Example 8
Source File: BadgerTest.java    From Badger with Apache License 2.0 5 votes vote down vote up
@Test
public void sett_and_mutate() {
    Drawable[] drawables = {factory.createBadge(), new TestDrawable()};
    LayerDrawable layer = new LayerDrawable(drawables);
    layer.setId(0, R.id.badger_drawable);

    Badger.sett(layer, factory).drawable.mutate();
}
 
Example 9
Source File: UiUtils.java    From tindroid with Apache License 2.0 5 votes vote down vote up
private static void constructToolbarLogo(final Activity activity, Bitmap avatar, String name,
                                         String uid, boolean online) {
    final Toolbar toolbar = activity.findViewById(R.id.toolbar);
    if (toolbar == null) {
        return;
    }

    Drawable avatarDrawable;
    if (avatar != null) {
        avatarDrawable = new RoundImageDrawable(activity.getResources(), avatar);
    } else {
        avatarDrawable = new LetterTileDrawable(activity)
                .setLetterAndColor(name, uid)
                .setContactTypeAndColor(Topic.getTopicTypeByName(uid) == Topic.TopicType.P2P ?
                        LetterTileDrawable.ContactType.PERSON : LetterTileDrawable.ContactType.GROUP);
    }
    AnimationDrawable typing = (AnimationDrawable)
            activity.getResources().getDrawable(R.drawable.typing_indicator);
    typing.setOneShot(false);
    typing.setVisible(false, true);
    typing.setAlpha(0);
    LayerDrawable layers = new LayerDrawable(
            new Drawable[]{
                    avatarDrawable,
                    new OnlineDrawable(online),
                    typing});
    layers.setId(0, LOGO_LAYER_AVATAR);
    layers.setId(1, LOGO_LAYER_ONLINE);
    layers.setId(2, LOGO_LAYER_TYPING);
    toolbar.setLogo(layers);
    Rect b = toolbar.getLogo().getBounds();
    if (!b.isEmpty()) {
        typing.setBounds(b.right - b.width() / 4, b.bottom - b.height() / 4, b.right, b.bottom);
    }
}
 
Example 10
Source File: LayerDrawableInflateImpl.java    From MagicaSakura with Apache License 2.0 4 votes vote down vote up
@Override
public Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    final int innerDepth = parser.getDepth() + 1;
    int type;
    int depth;
    int layerAttrUseCount = 0;
    int drawableUseCount = 0;
    int space = STEP << 1;
    //L,T,R,B,S,E,id
    int[][] childLayersAttrs = new int[space][ATTRS.length];
    Drawable[] drawables = new Drawable[space];

    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;
        }

        if (layerAttrUseCount >= childLayersAttrs.length) {
            int[][] dstInt = new int[drawables.length + STEP][ATTRS.length];
            System.arraycopy(childLayersAttrs, 0, dstInt, 0, childLayersAttrs.length);
            childLayersAttrs = dstInt;
        }
        updateLayerAttrs(context, attrs, childLayersAttrs[layerAttrUseCount]);
        layerAttrUseCount++;

        Drawable drawable = DrawableUtils.getAttrDrawable(context, attrs, android.R.attr.drawable);

        // If the layer doesn't have a drawable or unresolved theme
        // attribute for a drawable, attempt to parse one from the child
        // element.
        if (drawable == null) {
            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");
            }
            drawable = DrawableUtils.createFromXmlInner(context, parser, attrs);
        } else {
            final ColorStateList cls = DrawableUtils.getTintColorList(context, attrs, R.attr.drawableTint);
            if (cls != null) {
                drawable = ThemeUtils.tintDrawable(drawable, cls, DrawableUtils.getTintMode(context, attrs, R.attr.drawableTintMode));
            }
        }

        if (drawable != null) {
            if (drawableUseCount >= drawables.length) {
                Drawable[] dst = new Drawable[drawables.length + STEP];
                System.arraycopy(drawables, 0, dst, 0, drawables.length);
                drawables = dst;
            }
            drawables[drawableUseCount] = drawable;
            drawableUseCount++;
        }
    }

    if (drawables[0] == null || drawableUseCount != layerAttrUseCount) {
        return null;
    } else {
        LayerDrawable layerDrawable = new LayerDrawable(drawables);
        for (int i = 0; i < drawables.length; i++) {
            int[] childLayersAttr = childLayersAttrs[i];
            if (childLayersAttr[0] != 0 || childLayersAttr[1] != 0 || childLayersAttr[2] != 0 || childLayersAttr[3] != 0) {
                layerDrawable.setLayerInset(i, childLayersAttr[0], childLayersAttr[1], childLayersAttr[2], childLayersAttr[3]);
            }
            if (childLayersAttr[4] != 0) {
                layerDrawable.setId(i, childLayersAttr[4]);
            }
        }
        return layerDrawable;
    }
}
 
Example 11
Source File: IcsProgressBar.java    From Libraries-for-Android-Developers with MIT License 4 votes vote down vote up
/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 */
private Drawable tileify(Drawable drawable, boolean clip) {

    if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i),
                    (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }

        LayerDrawable newBg = new LayerDrawable(outDrawables);

        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }

        return newBg;

    }/* else if (drawable instanceof StateListDrawable) {
        StateListDrawable in = (StateListDrawable) drawable;
        StateListDrawable out = new StateListDrawable();
        int numStates = in.getStateCount();
        for (int i = 0; i < numStates; i++) {
            out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
        }
        return out;

    }*/ else if (drawable instanceof BitmapDrawable) {
        final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }

        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());

        final BitmapShader bitmapShader = new BitmapShader(tileBitmap,
                Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);

        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
                ClipDrawable.HORIZONTAL) : shapeDrawable;
    }

    return drawable;
}
 
Example 12
Source File: IcsProgressBar.java    From zhangshangwuda with Apache License 2.0 4 votes vote down vote up
/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 */
private Drawable tileify(Drawable drawable, boolean clip) {

    if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i),
                    (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }

        LayerDrawable newBg = new LayerDrawable(outDrawables);

        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }

        return newBg;

    }/* else if (drawable instanceof StateListDrawable) {
        StateListDrawable in = (StateListDrawable) drawable;
        StateListDrawable out = new StateListDrawable();
        int numStates = in.getStateCount();
        for (int i = 0; i < numStates; i++) {
            out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
        }
        return out;

    }*/ else if (drawable instanceof BitmapDrawable) {
        final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }

        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());

        final BitmapShader bitmapShader = new BitmapShader(tileBitmap,
                Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);

        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
                ClipDrawable.HORIZONTAL) : shapeDrawable;
    }

    return drawable;
}
 
Example 13
Source File: IcsProgressBar.java    From zen4android with MIT License 4 votes vote down vote up
/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 */
private Drawable tileify(Drawable drawable, boolean clip) {

    if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i),
                    (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }

        LayerDrawable newBg = new LayerDrawable(outDrawables);

        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }

        return newBg;

    }/* else if (drawable instanceof StateListDrawable) {
        StateListDrawable in = (StateListDrawable) drawable;
        StateListDrawable out = new StateListDrawable();
        int numStates = in.getStateCount();
        for (int i = 0; i < numStates; i++) {
            out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
        }
        return out;

    }*/ else if (drawable instanceof BitmapDrawable) {
        final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }

        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());

        final BitmapShader bitmapShader = new BitmapShader(tileBitmap,
                Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);

        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
                ClipDrawable.HORIZONTAL) : shapeDrawable;
    }

    return drawable;
}
 
Example 14
Source File: IcsProgressBar.java    From android-apps with MIT License 4 votes vote down vote up
/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 */
private Drawable tileify(Drawable drawable, boolean clip) {

    if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i),
                    (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }

        LayerDrawable newBg = new LayerDrawable(outDrawables);

        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }

        return newBg;

    }/* else if (drawable instanceof StateListDrawable) {
        StateListDrawable in = (StateListDrawable) drawable;
        StateListDrawable out = new StateListDrawable();
        int numStates = in.getStateCount();
        for (int i = 0; i < numStates; i++) {
            out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
        }
        return out;

    }*/ else if (drawable instanceof BitmapDrawable) {
        final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }

        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());

        final BitmapShader bitmapShader = new BitmapShader(tileBitmap,
                Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);

        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
                ClipDrawable.HORIZONTAL) : shapeDrawable;
    }

    return drawable;
}
 
Example 15
Source File: LayerDrawableInflateImpl.java    From timecat with Apache License 2.0 4 votes vote down vote up
@Override
public Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    final int innerDepth = parser.getDepth() + 1;
    int type;
    int depth;
    int layerAttrUseCount = 0;
    int drawableUseCount = 0;
    int space = STEP << 1;
    //L,T,R,B,S,E,id
    int[][] childLayersAttrs = new int[space][ATTRS.length];
    Drawable[] drawables = new Drawable[space];

    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;
        }

        if (layerAttrUseCount >= childLayersAttrs.length) {
            int[][] dstInt = new int[drawables.length + STEP][ATTRS.length];
            System.arraycopy(childLayersAttrs, 0, dstInt, 0, childLayersAttrs.length);
            childLayersAttrs = dstInt;
        }
        updateLayerAttrs(context, attrs, childLayersAttrs[layerAttrUseCount]);
        layerAttrUseCount++;

        Drawable drawable = DrawableUtils.getAttrDrawable(context, attrs, android.R.attr.drawable);

        // If the layer doesn't have a drawable or unresolved theme
        // attribute for a drawable, attempt to parse one from the child
        // element.
        if (drawable == null) {
            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");
            }
            drawable = DrawableUtils.createFromXmlInner(context, parser, attrs);
        } else {
            final ColorStateList cls = DrawableUtils.getTintColorList(context, attrs, R.attr.drawableTint);
            if (cls != null) {
                drawable = ThemeUtils.tintDrawable(drawable, cls, DrawableUtils.getTintMode(context, attrs, R.attr.drawableTintMode));
            }
        }

        if (drawable != null) {
            if (drawableUseCount >= drawables.length) {
                Drawable[] dst = new Drawable[drawables.length + STEP];
                System.arraycopy(drawables, 0, dst, 0, drawables.length);
                drawables = dst;
            }
            drawables[drawableUseCount] = drawable;
            drawableUseCount++;
        }
    }

    if (drawables[0] == null || drawableUseCount != layerAttrUseCount) {
        return null;
    } else {
        LayerDrawable layerDrawable = new LayerDrawable(drawables);
        for (int i = 0; i < drawables.length; i++) {
            int[] childLayersAttr = childLayersAttrs[i];
            if (childLayersAttr[0] != 0 || childLayersAttr[1] != 0 || childLayersAttr[2] != 0 || childLayersAttr[3] != 0) {
                layerDrawable.setLayerInset(i, childLayersAttr[0], childLayersAttr[1], childLayersAttr[2], childLayersAttr[3]);
            }
            if (childLayersAttr[4] != 0) {
                layerDrawable.setId(i, childLayersAttr[4]);
            }
        }
        return layerDrawable;
    }
}
 
Example 16
Source File: SkinCompatProgressBarHelper.java    From Android-skin-support with MIT License 4 votes vote down vote up
/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 */
private Drawable tileify(Drawable drawable, boolean clip) {
    if (drawable instanceof DrawableWrapper) {
        Drawable inner = ((DrawableWrapper) drawable).getWrappedDrawable();
        if (inner != null) {
            inner = tileify(inner, clip);
            ((DrawableWrapper) drawable).setWrappedDrawable(inner);
        }
    } else if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i),
                    (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }
        LayerDrawable newBg = new LayerDrawable(outDrawables);

        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }

        return newBg;

    } else if (drawable instanceof BitmapDrawable) {
        final BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        final Bitmap tileBitmap = bitmapDrawable.getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }

        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
        final BitmapShader bitmapShader = new BitmapShader(tileBitmap,
                Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);
        shapeDrawable.getPaint().setColorFilter(bitmapDrawable.getPaint().getColorFilter());
        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
                ClipDrawable.HORIZONTAL) : shapeDrawable;
    }

    return drawable;
}
 
Example 17
Source File: MProgressBarDialog.java    From MNProgressHUD with Apache License 2.0 4 votes vote down vote up
private void configView() {
    try {
        //设置动画
        if (mBuilder != null && mBuilder.animationID != 0 && mDialog.getWindow() != null) {
            mDialog.getWindow().setWindowAnimations(mBuilder.animationID);
        }
    } catch (Exception e) {

    }
    dialog_window_background.setBackgroundColor(mBuilder.backgroundWindowColor);
    tvShow.setTextColor(mBuilder.textColor);

    GradientDrawable myGrad = (GradientDrawable) dialog_view_bg.getBackground();
    myGrad.setColor(mBuilder.backgroundViewColor);
    myGrad.setStroke(MSizeUtils.dp2px(mContext, mBuilder.strokeWidth), mBuilder.strokeColor);
    myGrad.setCornerRadius(MSizeUtils.dp2px(mContext, mBuilder.cornerRadius));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        dialog_view_bg.setBackground(myGrad);
    } else {
        dialog_view_bg.setBackgroundDrawable(myGrad);
    }

    //horizontalProgressBar 配置
    //背景
    GradientDrawable progressBarBackgroundDrawable = new GradientDrawable();
    progressBarBackgroundDrawable.setColor(mBuilder.progressbarBackgroundColor);
    progressBarBackgroundDrawable.setCornerRadius(MSizeUtils.dp2px(mContext, mBuilder.progressCornerRadius));
    //二级进度条
    GradientDrawable secondProgressDrawable = new GradientDrawable();
    secondProgressDrawable.setColor(mBuilder.progressbarBackgroundColor);
    secondProgressDrawable.setCornerRadius(MSizeUtils.dp2px(mContext, mBuilder.progressCornerRadius));
    ClipDrawable hProgressBar02 = new ClipDrawable(secondProgressDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    //一级进度条
    GradientDrawable progressDrawable = new GradientDrawable();
    progressDrawable.setColor(mBuilder.progressColor);
    progressDrawable.setCornerRadius(MSizeUtils.dp2px(mContext, mBuilder.progressCornerRadius));
    ClipDrawable hProgressBar03 = new ClipDrawable(progressDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    //组合
    Drawable[] layers = {progressBarBackgroundDrawable, hProgressBar02, hProgressBar03};
    LayerDrawable layerDrawable = new LayerDrawable(layers);
    layerDrawable.setId(0, android.R.id.background);
    layerDrawable.setId(1, android.R.id.secondaryProgress);
    layerDrawable.setId(2, android.R.id.progress);
    horizontalProgressBar.setProgressDrawable(layerDrawable);

    ViewGroup.LayoutParams layoutParams = horizontalProgressBar.getLayoutParams();
    layoutParams.height = MSizeUtils.dp2px(mContext, mBuilder.horizontalProgressBarHeight);
    horizontalProgressBar.setLayoutParams(layoutParams);

    //circularProgressBar 配置
    circularProgressBar.setBackgroundColor(mBuilder.progressbarBackgroundColor);
    circularProgressBar.setColor(mBuilder.progressColor);
    circularProgressBar.setProgressBarWidth(MSizeUtils.dp2px(mContext, mBuilder.circleProgressBarWidth));
    circularProgressBar.setBackgroundProgressBarWidth(MSizeUtils.dp2px(mContext, mBuilder.circleProgressBarBackgroundWidth));
}
 
Example 18
Source File: SkinCompatProgressBarHelper.java    From AndroidSkinAnimator with MIT License 4 votes vote down vote up
/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 */
private Drawable tileify(Drawable drawable, boolean clip) {
    if (drawable instanceof DrawableWrapper) {
        Drawable inner = ((DrawableWrapper) drawable).getWrappedDrawable();
        if (inner != null) {
            inner = tileify(inner, clip);
            ((DrawableWrapper) drawable).setWrappedDrawable(inner);
        }
    } else if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i),
                    (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }
        LayerDrawable newBg = new LayerDrawable(outDrawables);

        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }

        return newBg;

    } else if (drawable instanceof BitmapDrawable) {
        final BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        final Bitmap tileBitmap = bitmapDrawable.getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }

        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
        final BitmapShader bitmapShader = new BitmapShader(tileBitmap,
                Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);
        shapeDrawable.getPaint().setColorFilter(bitmapDrawable.getPaint().getColorFilter());
        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
                ClipDrawable.HORIZONTAL) : shapeDrawable;
    }

    return drawable;
}
 
Example 19
Source File: IcsProgressBar.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Converts a drawable to a tiled version of itself. It will recursively
 * traverse layer and state list drawables.
 */
private Drawable tileify(Drawable drawable, boolean clip) {

    if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];

        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i),
                    (id == android.R.id.progress || id == android.R.id.secondaryProgress));
        }

        LayerDrawable newBg = new LayerDrawable(outDrawables);

        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }

        return newBg;

    }/* else if (drawable instanceof StateListDrawable) {
        StateListDrawable in = (StateListDrawable) drawable;
        StateListDrawable out = new StateListDrawable();
        int numStates = in.getStateCount();
        for (int i = 0; i < numStates; i++) {
            out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
        }
        return out;

    }*/ else if (drawable instanceof BitmapDrawable) {
        final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }

        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());

        final BitmapShader bitmapShader = new BitmapShader(tileBitmap,
                Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);

        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
                ClipDrawable.HORIZONTAL) : shapeDrawable;
    }

    return drawable;
}