android.graphics.drawable.LayerDrawable源码实例Demo

类android.graphics.drawable.LayerDrawable源码实例Demo

下面列出了android.graphics.drawable.LayerDrawable 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: mollyim-android   文件: GeneratedContactPhoto.java
@Override
public Drawable asDrawable(Context context, int color, boolean inverted) {
  int targetSize = context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size);
  String character = getAbbreviation(name);

  if (!TextUtils.isEmpty(character)) {
    Drawable base = TextDrawable.builder()
                                .beginConfig()
                                .width(targetSize)
                                .height(targetSize)
                                .useFont(TYPEFACE)
                                .fontSize(ViewUtil.dpToPx(context, 24))
                                .textColor(inverted ? color : Color.WHITE)
                                .endConfig()
                                .buildRound(character, inverted ? Color.WHITE : color);

    Drawable gradient = context.getResources().getDrawable(ThemeUtil.isDarkTheme(context) ? R.drawable.avatar_gradient_dark
                                                                                          : R.drawable.avatar_gradient_light);
    return new LayerDrawable(new Drawable[] { base, gradient });
  }

  return new ResourceContactPhoto(fallbackResId).asDrawable(context, color, inverted);
}
 
源代码2 项目: QuickReturn   文件: RoundedImageView.java
private void updateAttrs(Drawable drawable) {
    if (drawable == null) { return; }

    if (drawable instanceof RoundedDrawable) {
        ((RoundedDrawable) drawable)
                .setScaleType(mScaleType)
                .setCornerRadius(cornerRadius)
                .setBorderWidth(borderWidth)
                .setBorderColor(borderColor)
                .setOval(isOval);
    } else if (drawable instanceof LayerDrawable) {
        // loop through layers to and set drawable attrs
        LayerDrawable ld = ((LayerDrawable) drawable);
        for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
            updateAttrs(ld.getDrawable(i));
        }
    }
}
 
/**
 * This method is called when the view's background was null, or did not
 * have any states assigned. The call will directly update the views in the
 * context list with the background image generated by each of the contexts.
 * A new StateListDrawable that will be applied as the View's background.
 * Subclasses may overwrite.
 * 
 * @param ruleSets
 * @param contexts
 */
protected void updateWithNewStates(List<PXRuleSet> ruleSets, List<PXStylerContext> contexts) {
    // We should have the same view in all contexts, so grab the first
    // and set it with the new background
    StateListDrawable drawable = PXDrawableUtil.createNewStateListDrawable(this, ruleSets,
            contexts);
    if (drawable != null) {
        // Set the background on the right level of the toggle button
        ToggleButton toggleButton = (ToggleButton) contexts.get(0).getStyleable();
        Drawable background = toggleButton.getBackground();
        if (background instanceof LayerDrawable) {
            LayerDrawable layerDrawable = (LayerDrawable) background;
            layerDrawable.setDrawableByLayerId(android.R.id.background, drawable);
        } else {
            // we don't deal with any other background type (non-default)
            // (TODO: We may need to hack our way to create a new
            // LayerDrawable with the right layers id's)
            PXDrawableUtil.setBackgroundDrawable((View) toggleButton, drawable);
        }
    }
}
 
源代码4 项目: hkm-progress-button   文件: ArrowButton.java
protected LayerDrawable createCommonFace() {
    if (resIcon != -1) {
        LayerDrawable ly = afterProcessLayerDrawable(new LayerDrawable(
                new Drawable[]{
                        createFillDrawable(),
                        getArrow(),
                        getIcon()
                }));
        ly.setLayerInset(2, icon_size_text_padding, 0, 0, 0);
        return ly;
    } else {
        return afterProcessLayerDrawable(new LayerDrawable(
                new Drawable[]{
                        createFillDrawable(),
                        getArrow()
                }));
    }
}
 
源代码5 项目: AGIKSwipeButton   文件: Swipe_Button_View.java
void set_default(AttributeSet attrs, int defStyle) {
    inflate(getContext(), R.layout.sb_swipe_view, this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        setBackground(ContextCompat.getDrawable(getContext(), R.drawable.sb_swipe_view_bg));
    } else {
        setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.drawable.sb_swipe_view_bg));
    }

    swipeTextView = findViewById(R.id.sb_text);
    swipe_button_controller = findViewById(R.id.sb_swipe);
    swipe_button_controller.setOnSeekBarChangeListener(this);
    swipe_bg_drawable = getBackground();
    swipeLayers = (LayerDrawable) swipe_button_controller.getThumb();
    thumb_bg_drawable = swipeLayers.findDrawableByLayerId(R.id.thumb_background);

    TypedArray attributes = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.SwipeButtonView,
            defStyle, defStyle);
    try {
        getAttributes(attributes);
    } finally {
        attributes.recycle();
    }
}
 
源代码6 项目: BigApp_Discuz_Android   文件: RoundedDrawable.java
public static Drawable fromDrawable(Drawable drawable) {
  if (drawable != null) {
    if (drawable instanceof RoundedDrawable) {
      // just return if it's already a RoundedDrawable
      return drawable;
    } else if (drawable instanceof LayerDrawable) {
      LayerDrawable ld = (LayerDrawable) drawable;
      int num = ld.getNumberOfLayers();

      // loop through layers to and change to RoundedDrawables if possible
      for (int i = 0; i < num; i++) {
        Drawable d = ld.getDrawable(i);
        ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d));
      }
      return ld;
    }

    // try to get a bitmap from the drawable and
    Bitmap bm = drawableToBitmap(drawable);
    if (bm != null) {
      return new RoundedDrawable(bm);
    }
  }
  return drawable;
}
 
源代码7 项目: Passbook   文件: PasswordGenerator.java
private void tintSeekBar(SeekBar sb) {
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        if( getContext() == null) {
            return;
        }
        LayerDrawable progress = (LayerDrawable)ContextCompat.getDrawable(getContext(),
                R.drawable.progress);
        if (progress == null) {
            return;
        }
        progress.getDrawable(0).setColorFilter(C.ThemedColors[C.colorIconNormal],
                PorterDuff.Mode.SRC_ATOP);
        progress.getDrawable(1).setColorFilter(C.ThemedColors[C.colorAccent],
                PorterDuff.Mode.SRC_ATOP);
        sb.setProgressDrawable(progress);

        Drawable thumb = ContextCompat.getDrawable(getContext(), R.drawable.thumb);
        if (thumb == null) {
            return;
        }
        thumb.setColorFilter(C.ThemedColors[C.colorAccent], PorterDuff.Mode.SRC_ATOP);
        sb.setThumb(thumb);
    }
}
 
源代码8 项目: photo-editor-android   文件: ColorPickerAdapter.java
private void buildColorPickerView(View view, int colorCode) {
    view.setVisibility(View.VISIBLE);

    ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
    biggerCircle.setIntrinsicHeight(20);
    biggerCircle.setIntrinsicWidth(20);
    biggerCircle.setBounds(new Rect(0, 0, 20, 20));
    biggerCircle.getPaint().setColor(colorCode);

    ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape());
    smallerCircle.setIntrinsicHeight(5);
    smallerCircle.setIntrinsicWidth(5);
    smallerCircle.setBounds(new Rect(0, 0, 5, 5));
    smallerCircle.getPaint().setColor(Color.WHITE);
    smallerCircle.setPadding(10, 10, 10, 10);
    Drawable[] drawables = {smallerCircle, biggerCircle};

    LayerDrawable layerDrawable = new LayerDrawable(drawables);

    view.setBackgroundDrawable(layerDrawable);
}
 
源代码9 项目: UILibrary   文件: ExampleListActivity.java
/**
 * 苹果桌面图标下载风格的进度条
 */
private void addAppStoreStyleDrawableToList(){
    ProgressiveDrawable.DrawContentProvider appStoreProvider = new AppStoreStyleProgressProvider(0x99000000);

    final ProgressiveDrawable drawable = new ProgressiveDrawable(appStoreProvider);
    final Drawable backgroundDrawable = ContextCompat.getDrawable(this, R.drawable.test1);
    final LayerDrawable contentDrawalbe = new LayerDrawable(new Drawable[]{backgroundDrawable, drawable});
    mExampleItemList.add(new ExampleItem("AppStoreStyleDrawable", new ExampleDetailActivity.DetailContentProvider() {
        @Override
        public Object provideInstance() {
            return contentDrawalbe;
        }

        @Override
        public void onGestureMove(float x, float y, int action) {
            if(x >= -1 && x <= 1){
                drawable.setProgress(x);
            }
        }

        @Override
        public void destroy(){

        }
    }));
}
 
源代码10 项目: Loop   文件: RoundedDrawable.java
public static Drawable fromDrawable(Drawable drawable) {
    if (drawable != null) {
        if (drawable instanceof RoundedDrawable) {
            // just return if it's already a RoundedDrawable
            return drawable;
        } else if (drawable instanceof LayerDrawable) {
            LayerDrawable ld = (LayerDrawable) drawable;
            int num = ld.getNumberOfLayers();

            // loop through layers to and change to RoundedDrawables if possible
            for (int i = 0; i < num; i++) {
                Drawable d = ld.getDrawable(i);
                ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d));
            }
            return ld;
        }

        // try to get a bitmap from the drawable and
        Bitmap bm = drawableToBitmap(drawable);
        if (bm != null) {
            return new RoundedDrawable(bm);
        }
    }
    return drawable;
}
 
private Bitmap createTransformedBitmap(Bitmap cropOriginal, Bitmap staticMap) {
    Drawable[] layers = new Drawable[2];

    BitmapDrawable baseMapDrawable = new BitmapDrawable(mContext.getResources(), staticMap);
    baseMapDrawable.setGravity(Gravity.CENTER);

    BitmapDrawable mapDrawable = new BitmapDrawable(mContext.getResources(), cropOriginal);
    mapDrawable.setGravity(Gravity.CENTER);

    layers[0] = baseMapDrawable;
    layers[1] = mapDrawable;
    LayerDrawable layerDrawable = new LayerDrawable(layers);


    Bitmap transformedBitmap = Bitmap.createBitmap(
            staticMap.getWidth(), staticMap.getHeight(), Bitmap.Config.ARGB_8888);
    layerDrawable.setBounds(0, 0, staticMap.getWidth(), staticMap.getHeight());
    layerDrawable.draw(new Canvas(transformedBitmap));

    return transformedBitmap;
}
 
源代码12 项目: PhotoEditor   文件: ColorPickerAdapter.java
private void buildColorPickerView(View view, int colorCode) {
    view.setVisibility(View.VISIBLE);

    ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
    biggerCircle.setIntrinsicHeight(20);
    biggerCircle.setIntrinsicWidth(20);
    biggerCircle.setBounds(new Rect(0, 0, 20, 20));
    biggerCircle.getPaint().setColor(colorCode);

    ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape());
    smallerCircle.setIntrinsicHeight(5);
    smallerCircle.setIntrinsicWidth(5);
    smallerCircle.setBounds(new Rect(0, 0, 5, 5));
    smallerCircle.getPaint().setColor(Color.WHITE);
    smallerCircle.setPadding(10, 10, 10, 10);
    Drawable[] drawables = {smallerCircle, biggerCircle};

    LayerDrawable layerDrawable = new LayerDrawable(drawables);

    view.setBackgroundDrawable(layerDrawable);
}
 
源代码13 项目: RangeSeekBar   文件: RangeProgressBar.java
private void updateDrawableBounds(int w, int h) {
    mPaddingLeft = getPaddingLeft();
    mPaddingRight = getPaddingRight();
    mPaddingTop = getPaddingTop();
    mPaddingBottom = getPaddingBottom();

    w -= mPaddingRight + mPaddingLeft;
    h -= mPaddingTop + mPaddingBottom;

    int right = w;
    int bottom = h;
    int top = 0;
    int left = 0;

    mProgressDrawableIndicator = null;
    mProgressIndicatorBounds = null;
    mComputedWidth = w;

    if (mProgressDrawable != null) {
        mProgressDrawable.setBounds(left, top, right, bottom);
        mProgressDrawableIndicator = ((LayerDrawable) mProgressDrawable).findDrawableByLayerId(android.R.id.progress);
        mProgressIndicatorBounds = mProgressDrawableIndicator.getBounds();
    }
}
 
private void buildColorPickerView(View view, int colorCode) {
    view.setVisibility(View.VISIBLE);

    ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
    biggerCircle.setIntrinsicHeight(20);
    biggerCircle.setIntrinsicWidth(20);
    biggerCircle.setBounds(new Rect(0, 0, 20, 20));
    biggerCircle.getPaint().setColor(colorCode);

    ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape());
    smallerCircle.setIntrinsicHeight(5);
    smallerCircle.setIntrinsicWidth(5);
    smallerCircle.setBounds(new Rect(0, 0, 5, 5));
    smallerCircle.getPaint().setColor(Color.WHITE);
    smallerCircle.setPadding(10, 10, 10, 10);
    Drawable[] drawables = {smallerCircle, biggerCircle};

    LayerDrawable layerDrawable = new LayerDrawable(drawables);

    view.setBackgroundDrawable(layerDrawable);
}
 
源代码15 项目: SuntimesWidget   文件: WorldMapSeekBar.java
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);
}
 
源代码16 项目: 365browser   文件: ColorSuggestionListAdapter.java
/**
 * Sets up the color button to represent a color suggestion.
 *
 * @param button The button view to set up.
 * @param index The index of the suggestion in mSuggestions.
 */
private void setUpColorButton(View button, int index) {
    if (index >= mSuggestions.length) {
        button.setTag(null);
        button.setContentDescription(null);
        button.setVisibility(View.INVISIBLE);
        return;
    }
    button.setTag(mSuggestions[index]);
    button.setVisibility(View.VISIBLE);
    ColorSuggestion suggestion = mSuggestions[index];
    LayerDrawable layers = (LayerDrawable) button.getBackground();
    GradientDrawable swatch =
            (GradientDrawable) layers.findDrawableByLayerId(R.id.color_button_swatch);
    swatch.setColor(suggestion.mColor);
    String description = suggestion.mLabel;
    if (TextUtils.isEmpty(description)) {
        description = String.format("#%06X", (0xFFFFFF & suggestion.mColor));
    }
    button.setContentDescription(description);
    button.setOnClickListener(this);
}
 
源代码17 项目: Noyze   文件: CmBatteryBar.java
/**
 * Updates the {@link ShapeDrawable} which displays the
 * color of the bar across the screen.
 */
public void setProgressDrawable(Drawable mDrawable, int mNewColor)
{
	if (mDrawable instanceof LayerDrawable &&
		getProgressDrawable() instanceof LayerDrawable)
	{
        final LayerDrawable mDraw = (LayerDrawable) getProgressDrawable();
        final ClipDrawable mShape = (ClipDrawable)
            mDraw.findDrawableByLayerId(android.R.id.progress);

        // Make sure we've got everything.
        if (mShape != null && mProgress != null &&
            mProgress.getPaint() != null)
        {
            mProgress.getPaint().setColor(mNewColor);
            final Rect mBounds = mDraw.getBounds();
            super.setProgressDrawable(mDraw);
            getProgressDrawable().setBounds(mBounds);
            return;
        }
	}
	
	super.setProgressDrawable(mDrawable);
}
 
源代码18 项目: Phonograph   文件: AppShortcutIconGenerator.java
private static IconCompat generateThemedIcon(Context context, int iconId, int foregroundColor, int backgroundColor) {
    // Get and tint foreground and background drawables
    Drawable vectorDrawable = ImageUtil.getTintedVectorDrawable(context, iconId, foregroundColor);
    Drawable backgroundDrawable = ImageUtil.getTintedVectorDrawable(context, R.drawable.ic_app_shortcut_background, backgroundColor);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        AdaptiveIconDrawable adaptiveIconDrawable = new AdaptiveIconDrawable(backgroundDrawable, vectorDrawable);
        return IconCompat.createWithAdaptiveBitmap(ImageUtil.createBitmap(adaptiveIconDrawable));
    } else {
        // Squash the two drawables together
        LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{backgroundDrawable, vectorDrawable});

        // Return as an Icon
        return IconCompat.createWithBitmap(ImageUtil.createBitmap(layerDrawable));
    }
}
 
源代码19 项目: TestChat   文件: FloatingActionButton.java
private Drawable createCircleDrawable(int color, float strokeWidth) {
        int alpha = Color.alpha(color);
        int opaqueColor = opaque(color);

        ShapeDrawable fillDrawable = new ShapeDrawable(new OvalShape());

        final Paint paint = fillDrawable.getPaint();
        paint.setAntiAlias(true);
        paint.setColor(opaqueColor);

        Drawable[] layers = {
                fillDrawable,
                createInnerStrokesDrawable(opaqueColor, strokeWidth)
        };

        LayerDrawable drawable = alpha == 255 || !mStrokeVisible
                ? new LayerDrawable(layers)
                : new TranslucentLayerDrawable(alpha, layers);

        int halfStrokeWidth = (int) (strokeWidth / 2f);
        drawable.setLayerInset(1, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth);

        return drawable;
}
 
private void updateAttrs(Drawable drawable, boolean background) {
    if (drawable == null) {
        return;
    }
    if (drawable instanceof RoundedDrawable) {
        ((RoundedDrawable) drawable)
                .setScaleType(mScaleType)
                .setCornerRadius(background && !mRoundBackground ? 0 : mCornerRadius)
                .setBorderWidth(mBorderWidth)
                .setBorderColors(mBorderColor)
                .setOval(mOval);
    } else if (drawable instanceof LayerDrawable) {
        // loop through layers to and set drawable attrs
        LayerDrawable ld = ((LayerDrawable) drawable);
        int layers = ld.getNumberOfLayers();
        for (int i = 0; i < layers; i++) {
            updateAttrs(ld.getDrawable(i), background);
        }
    }
}
 
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final JsonObject formDefinition = (JsonObject) formDefinitions.get(position);

    String name = formDefinition.get("name").getAsString();
    holder.name.setText(name);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onFormClicked(formDefinition);
        }
    });

    String color = formDefinition.get("color").getAsString();
    // Lets add a tiny bit of transparency to soften things up.
    color = "#D7" + color.substring(1, color.length());

    LayerDrawable pickerIcon = (LayerDrawable) holder.icon.getDrawable();
    Drawable background = pickerIcon.findDrawableByLayerId(R.id.background);
    DrawableCompat.setTint(background, Color.parseColor(color));
}
 
源代码22 项目: ucar-weex-core   文件: WXViewUtils.java
public static @Nullable
BorderDrawable getBorderDrawable(@NonNull View view){
  Drawable drawable=view.getBackground();
  if(drawable instanceof BorderDrawable){
    return (BorderDrawable) drawable;
  }
  else if(drawable instanceof LayerDrawable){
    if(((LayerDrawable) drawable).getNumberOfLayers()>1) {
      Drawable innerDrawable=((LayerDrawable) drawable).getDrawable(0);
      if(innerDrawable instanceof BorderDrawable){
        return (BorderDrawable) innerDrawable;
      }
    }
  }
  return null;
}
 
源代码23 项目: UltimateAndroid   文件: FloatingActionButton.java
void updateBackground() {
  float circleLeft = mShadowRadius;
  float circleTop = mShadowRadius - mShadowOffset;

  final RectF circleRect = new RectF(circleLeft, circleTop, circleLeft + mCircleSize, circleTop + mCircleSize);

  LayerDrawable layerDrawable = new LayerDrawable(
      new Drawable[] {
          getResources().getDrawable(mSize == SIZE_NORMAL ? R.drawable.floating_action_button_fab_bg_normal : R.drawable.floating_action_button_fab_bg_mini),
          createFillDrawable(circleRect),
          createStrokesDrawable(circleRect),
          getIconDrawable()
      });

  float iconOffset = (mCircleSize - getDimension(R.dimen.fab_icon_size)) / 2f;

  int iconInsetHorizontal = (int) (mShadowRadius + iconOffset);
  int iconInsetTop = (int) (circleTop + iconOffset);
  int iconInsetBottom = (int) (mShadowRadius + mShadowOffset + iconOffset);

  layerDrawable.setLayerInset(3, iconInsetHorizontal, iconInsetTop, iconInsetHorizontal, iconInsetBottom);

  setBackgroundCompat(layerDrawable);
}
 
源代码24 项目: Trivia-Knowledge   文件: FButton.java
private LayerDrawable createDrawable(int radius, int topColor, int bottomColor) {

        float[] outerRadius = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};

        //Top
        RoundRectShape topRoundRect = new RoundRectShape(outerRadius, null, null);
        ShapeDrawable topShapeDrawable = new ShapeDrawable(topRoundRect);
        topShapeDrawable.getPaint().setColor(topColor);
        //Bottom
        RoundRectShape roundRectShape = new RoundRectShape(outerRadius, null, null);
        ShapeDrawable bottomShapeDrawable = new ShapeDrawable(roundRectShape);
        bottomShapeDrawable.getPaint().setColor(bottomColor);
        //Create array
        Drawable[] drawArray = {bottomShapeDrawable, topShapeDrawable};
        LayerDrawable layerDrawable = new LayerDrawable(drawArray);

        //Set shadow height
        if (isShadowEnabled && topColor != Color.TRANSPARENT) {
            //unpressed drawable
            layerDrawable.setLayerInset(0, 0, 0, 0, 0);  /*index, left, top, right, bottom*/
        } else {
            //pressed drawable
            layerDrawable.setLayerInset(0, 0, mShadowHeight, 0, 0);  /*index, left, top, right, bottom*/
        }
        layerDrawable.setLayerInset(1, 0, 0, 0, mShadowHeight);  /*index, left, top, right, bottom*/

        return layerDrawable;
    }
 
/**
 * Some drawable implementations have problems with mutation. This method returns false if
 * there is a known issue in the given drawable's implementation.
 */
public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
    if (Build.VERSION.SDK_INT < 15 && drawable instanceof InsetDrawable) {
        return false;
    } else if (Build.VERSION.SDK_INT < 15 && drawable instanceof GradientDrawable) {
        // GradientDrawable has a bug pre-ICS which results in mutate() resulting
        // in loss of color
        return false;
    } else if (Build.VERSION.SDK_INT < 17 && drawable instanceof LayerDrawable) {
        return false;
    }

    if (drawable instanceof DrawableContainer) {
        // If we have a DrawableContainer, let's traverse it's child array
        final Drawable.ConstantState state = drawable.getConstantState();
        if (state instanceof DrawableContainer.DrawableContainerState) {
            final DrawableContainer.DrawableContainerState containerState =
                    (DrawableContainer.DrawableContainerState) state;
            for (final Drawable child : containerState.getChildren()) {
                if (!canSafelyMutateDrawable(child)) {
                    return false;
                }
            }
        }
    } else if (SkinCompatVersionUtils.isV4DrawableWrapper(drawable)) {
        return canSafelyMutateDrawable(SkinCompatVersionUtils.getV4DrawableWrapperWrappedDrawable(drawable));
    } else if (SkinCompatVersionUtils.isV4WrappedDrawable(drawable)) {
        return canSafelyMutateDrawable(SkinCompatVersionUtils.getV4WrappedDrawableWrappedDrawable(drawable));
    } else if (SkinCompatVersionUtils.isV7DrawableWrapper(drawable)) {
        return canSafelyMutateDrawable(SkinCompatVersionUtils.getV7DrawableWrapperWrappedDrawable(drawable));
    } else if (drawable instanceof ScaleDrawable) {
        Drawable scaleDrawable = ((ScaleDrawable) drawable).getDrawable();
        if (scaleDrawable != null) {
            return canSafelyMutateDrawable(scaleDrawable);
        }
    }

    return true;
}
 
@Override
public Drawable getUserBadgeForDensity(UserHandle user, int density) {
    Drawable badgeColor = getManagedProfileIconForDensity(user,
            com.android.internal.R.drawable.ic_corp_badge_color, density);
    if (badgeColor == null) {
        return null;
    }
    badgeColor.setTint(getUserBadgeColor(user));
    Drawable badgeForeground = getDrawableForDensity(
            com.android.internal.R.drawable.ic_corp_badge_case, density);
    Drawable badge = new LayerDrawable(
            new Drawable[] {badgeColor, badgeForeground });
    return badge;
}
 
@Override
public Drawable getUserBadgeForDensity(UserHandle user, int density) {
    Drawable badgeColor = getManagedProfileIconForDensity(user,
            com.android.internal.R.drawable.ic_corp_badge_color, density);
    if (badgeColor == null) {
        return null;
    }
    Drawable badgeForeground = getDrawableForDensity(
            com.android.internal.R.drawable.ic_corp_badge_case, density);
    badgeForeground.setTint(getUserBadgeColor(user));
    Drawable badge = new LayerDrawable(new Drawable[] {badgeColor, badgeForeground });
    return badge;
}
 
源代码28 项目: opentasks   文件: ColoredShapeCheckBox.java
private void applyColor(int color)
{
    mCurrentColor = color;

    // get an approximation for the lightness of the given color
    int y = (3 * Color.red(color) + 4 * Color.green(color) + Color.blue(color)) >> 3;

    mBackgroundShape.setColor(color);

    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { mBackgroundShape, y > 190 ? mDarkCheckmark : mLightCheckmark });
    setButtonDrawable(layerDrawable);
}
 
/**
 * Constructor for inflating from XML.
 */
public SelectableItemHighlightView(Context context, AttributeSet attrs) {
    super(context, attrs);
    Drawable clickDrawable = context.obtainStyledAttributes(new int[] {
            android.R.attr.selectableItemBackground }).getDrawable(0);
    Drawable longClickDrawable = ApiCompatibilityUtils.getDrawable(context.getResources(),
            R.drawable.selectable_item_highlight);
    LayerDrawable ld = new LayerDrawable(new Drawable[] {clickDrawable, longClickDrawable});
    setBackground(ld);
}
 
源代码30 项目: android_gisapp   文件: FloatingActionButton.java
void updateBackground()
{
    final float strokeWidth = getDimension(R.dimen.fab_stroke_width);
    final float halfStrokeWidth = strokeWidth / 2f;

    LayerDrawable layerDrawable = new LayerDrawable(
            new Drawable[] {
                    getResources().getDrawable(
                            mSize == SIZE_NORMAL
                            ? R.drawable.fab_bg_normal
                            : R.drawable.fab_bg_mini),
                    createFillDrawable(strokeWidth),
                    createOuterStrokeDrawable(strokeWidth),
                    getIconDrawable()});

    int iconOffset = (int) (mCircleSize - getDimension(R.dimen.fab_icon_size)) / 2;

    int circleInsetHorizontal = (int) (mShadowRadius);
    int circleInsetTop = (int) (mShadowRadius - mShadowOffset);
    int circleInsetBottom = (int) (mShadowRadius + mShadowOffset);

    layerDrawable.setLayerInset(
            1, circleInsetHorizontal, circleInsetTop, circleInsetHorizontal, circleInsetBottom);

    layerDrawable.setLayerInset(
            2, (int) (circleInsetHorizontal - halfStrokeWidth),
            (int) (circleInsetTop - halfStrokeWidth),
            (int) (circleInsetHorizontal - halfStrokeWidth),
            (int) (circleInsetBottom - halfStrokeWidth));

    layerDrawable.setLayerInset(
            3, circleInsetHorizontal + iconOffset, circleInsetTop + iconOffset,
            circleInsetHorizontal + iconOffset, circleInsetBottom + iconOffset);

    setBackgroundCompat(layerDrawable);
}
 
如果文章对您有帮助,请点击页面广告。以鼓励作者做出的努力。
 同包方法