下面列出了android.graphics.drawable.LayerDrawable#mutate() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void setBadgeCount(Context context, LayerDrawable icon, int count) {
MenuCounterDrawable badge;
// Reuse drawable if possible
Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
if (reuse != null && reuse instanceof MenuCounterDrawable) {
badge = (MenuCounterDrawable) reuse;
} else {
badge = new MenuCounterDrawable(context);
}
badge.setCount(count);
icon.mutate();
icon.setDrawableByLayerId(R.id.ic_badge, badge);
}
private void configureActionbar(){
ActionBar bar = getActionBar();
bar.setHomeButtonEnabled(true);
bar.setDisplayShowHomeEnabled(false);
tint = new SystemBarTintManager(this);
TypedValue tintColor = new TypedValue();
if(getTheme().resolveAttribute(R.attr.statusBarBackground, tintColor, true)){
tint.setStatusBarTintEnabled(true);
tint.setTintColor(tintColor.data);
defaultActionbarColor = tintColor.data;
currentActionbarColor = tintColor.data;
}else{
tint.setStatusBarTintEnabled(false);
}
TypedValue actionbarBackground = new TypedValue();
if(getTheme().resolveAttribute(R.attr.actionbarBackgroundLayerList, actionbarBackground, false)){
actionbarBackgroundList = (LayerDrawable) getResources().getDrawable(actionbarBackground.data);
actionbarBackgroundList.mutate();
actionbarColor = (ColorDrawable) actionbarBackgroundList.findDrawableByLayerId(R.id.actionbar_background_color);
actionbarColor.mutate();
bar.setBackgroundDrawable(actionbarBackgroundList);
}
}
public void initResultDiceViews(int dice, ImageView myImageview) {
switch (dice) {
case 1:
layers[1] = ContextCompat.getDrawable(getBaseContext(), R.drawable.d1);
break;
case 2:
layers[1] = ContextCompat.getDrawable(getBaseContext(), R.drawable.d2);
break;
case 3:
layers[1] = ContextCompat.getDrawable(getBaseContext(), R.drawable.d3);
break;
case 4:
layers[1] = ContextCompat.getDrawable(getBaseContext(), R.drawable.d4);
break;
case 5:
layers[1] = ContextCompat.getDrawable(getBaseContext(), R.drawable.d5);
break;
case 6:
layers[1] = ContextCompat.getDrawable(getBaseContext(), R.drawable.d6);
break;
case 0:
myImageview.setImageResource(0);
break;
default:
break;
}
layersDrawable = new LayerDrawable(layers);
layersDrawable.mutate();
layersDrawable.setLayerInset(0, 0, 0, 0, 0);
layersDrawable.setLayerInset(0, 0, 0, 0, 0);
myImageview.setImageDrawable(layersDrawable);
}
private void createIndeterminateProgressDrawable(@ColorInt int backgroundColour, @ColorInt int progressColour) {
LayerDrawable layerDrawable = (LayerDrawable) getProgressDrawable();
if (layerDrawable != null) {
layerDrawable.mutate();
layerDrawable.setDrawableByLayerId(android.R.id.background, createShapeDrawable(backgroundColour));
layerDrawable.setDrawableByLayerId(android.R.id.progress, createClipDrawable(backgroundColour));
layerDrawable.setDrawableByLayerId(android.R.id.secondaryProgress, createClipDrawable(progressColour));
}
}
private void setEnrollmentInfo(List<Trio<String, String, String>> enrollmentsInfo) {
binding.chipContainer.removeAllViews();
Context parentContext = binding.chipContainer.getContext();
for (Trio<String, String, String> enrollmentInfo : enrollmentsInfo) {
if (binding.getPresenter().getProgram() == null || !binding.getPresenter().getProgram().displayName().equals(enrollmentInfo.val0())) {
Chip chip = new Chip(parentContext);
chip.setText(enrollmentInfo.val0());
int color = ColorUtils.getColorFrom(enrollmentInfo.val1(), ColorUtils.getPrimaryColor(parentContext, ColorUtils.ColorType.PRIMARY_LIGHT));
int icon;
if (!isEmpty(enrollmentInfo.val2())) {
Resources resources = parentContext.getResources();
String iconName = enrollmentInfo.val2().startsWith("ic_") ? enrollmentInfo.val2() : "ic_" + enrollmentInfo.val2();
icon = resources.getIdentifier(iconName, "drawable", parentContext.getPackageName());
} else {
icon = R.drawable.ic_program_default;
}
Drawable iconImage;
try {
iconImage = AppCompatResources.getDrawable(parentContext, icon);
iconImage.mutate();
} catch (Exception e) {
Timber.log(1, e);
iconImage = AppCompatResources.getDrawable(parentContext, R.drawable.ic_program_default);
iconImage.mutate();
}
Drawable bgDrawable = AppCompatResources.getDrawable(parentContext, R.drawable.ic_chip_circle_24);
Drawable wrappedIcon = DrawableCompat.wrap(iconImage);
Drawable wrappedBg = DrawableCompat.wrap(bgDrawable);
LayerDrawable finalDrawable = new LayerDrawable(new Drawable[]{wrappedBg, wrappedIcon});
finalDrawable.mutate();
finalDrawable.getDrawable(1).setColorFilter(new PorterDuffColorFilter(ColorUtils.getContrastColor(color), PorterDuff.Mode.SRC_IN));
finalDrawable.getDrawable(0).setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
chip.setChipIcon(finalDrawable);
binding.chipContainer.addView(chip);
binding.chipContainer.invalidate();
}
}
}
private FrameLayout getBar(final String title, final int value, final int index) {
int maxValue = (int) (mMaxValue * 100);
LinearLayout linearLayout = new LinearLayout(mContext);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT
);
params.gravity = Gravity.CENTER;
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER);
//Adding bar
Bar bar = new Bar(mContext, null, android.R.attr.progressBarStyleHorizontal);
bar.setProgress(value);
bar.setVisibility(View.VISIBLE);
bar.setIndeterminate(false);
bar.setMax(maxValue);
bar.setProgressDrawable(ContextCompat.getDrawable(mContext, R.drawable.progress_bar_shape));
LayoutParams progressParams = new LayoutParams(
mBarWidth,
mBarHeight
);
progressParams.gravity = Gravity.CENTER;
bar.setLayoutParams(progressParams);
BarAnimation anim = new BarAnimation(bar, 0, value);
anim.setDuration(250);
bar.startAnimation(anim);
LayerDrawable layerDrawable = (LayerDrawable) bar.getProgressDrawable();
layerDrawable.mutate();
GradientDrawable emptyLayer = (GradientDrawable) layerDrawable.getDrawable(0);
ScaleDrawable scaleDrawable = (ScaleDrawable) layerDrawable.getDrawable(1);
emptyLayer.setColor(ContextCompat.getColor(mContext, mEmptyColor));
emptyLayer.setCornerRadius(mBarRadius);
GradientDrawable progressLayer = (GradientDrawable) scaleDrawable.getDrawable();
if (progressLayer != null) {
progressLayer.setColor(ContextCompat.getColor(mContext, mProgressColor));
progressLayer.setCornerRadius(mBarRadius);
}
linearLayout.addView(bar);
//Adding txt below bar
TextView txtBar = new TextView(mContext);
LayoutParams txtParams = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
txtBar.setTextSize(getSP(mBarTitleTxtSize));
txtBar.setText(title);
txtBar.setGravity(Gravity.CENTER);
txtBar.setTextColor(ContextCompat.getColor(mContext, mBarTitleColor));
txtBar.setPadding(0, mBarTitleMarginTop, 0, 0);
txtBar.setLayoutParams(txtParams);
linearLayout.addView(txtBar);
FrameLayout rootFrameLayout = new FrameLayout(mContext);
LinearLayout.LayoutParams rootParams = new LinearLayout.LayoutParams(
0,
LayoutParams.MATCH_PARENT,
1f
);
rootParams.gravity = Gravity.CENTER;
//rootParams.setMargins(0, h, 0, h);
rootFrameLayout.setLayoutParams(rootParams);
//Adding bar + title
rootFrameLayout.addView(linearLayout);
if (isBarCanBeClick)
rootFrameLayout.setOnClickListener(barClickListener);
rootFrameLayout.setTag(index);
return rootFrameLayout;
}
private void clickBarOn(FrameLayout frameLayout) {
pins.get((int) frameLayout.getTag()).setVisibility(View.VISIBLE);
isOldBarClicked = true;
int childCount = frameLayout.getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = frameLayout.getChildAt(i);
if (childView instanceof LinearLayout) {
LinearLayout linearLayout = (LinearLayout) childView;
Bar bar = (Bar) linearLayout.getChildAt(0);
TextView titleTxtView = (TextView) linearLayout.getChildAt(1);
LayerDrawable layerDrawable = (LayerDrawable) bar.getProgressDrawable();
layerDrawable.mutate();
ScaleDrawable scaleDrawable = (ScaleDrawable) layerDrawable.getDrawable(1);
GradientDrawable progressLayer = (GradientDrawable) scaleDrawable.getDrawable();
if (mPinBackgroundColor != 0) {
if (progressLayer != null) {
progressLayer.setColor(ContextCompat.getColor(mContext, mProgressClickColor));
}
} else {
if (progressLayer != null) {
progressLayer.setColor(ContextCompat.getColor(mContext, android.R.color.holo_green_dark));
}
}
if (mBarTitleSelectedColor > 0) {
titleTxtView.setTextColor(ContextCompat.getColor(mContext, mBarTitleSelectedColor));
} else {
titleTxtView.setTextColor(ContextCompat.getColor(mContext, android.R.color.holo_green_dark));
}
}
}
}
public void disableBar(int index) {
final int barsCount = ((LinearLayout) this.getChildAt(0)).getChildCount();
for (int i = 0; i < barsCount; i++) {
FrameLayout rootFrame = (FrameLayout) ((LinearLayout) this.getChildAt(0)).getChildAt(i);
int rootChildCount = rootFrame.getChildCount();
for (int j = 0; j < rootChildCount; j++) {
if ((int) rootFrame.getTag() != index)
continue;
rootFrame.setEnabled(false);
rootFrame.setClickable(false);
View childView = rootFrame.getChildAt(j);
if (childView instanceof LinearLayout) {
//bar
LinearLayout barContainerLinear = ((LinearLayout) childView);
int barContainerCount = barContainerLinear.getChildCount();
for (int k = 0; k < barContainerCount; k++) {
View view = barContainerLinear.getChildAt(k);
if (view instanceof Bar) {
Bar bar = (Bar) view;
LayerDrawable layerDrawable = (LayerDrawable) bar.getProgressDrawable();
layerDrawable.mutate();
ScaleDrawable scaleDrawable = (ScaleDrawable) layerDrawable.getDrawable(1);
GradientDrawable progressLayer = (GradientDrawable) scaleDrawable.getDrawable();
if (progressLayer != null) {
if (mProgressDisableColor > 0)
progressLayer.setColor(ContextCompat.getColor(mContext, mProgressDisableColor));
else
progressLayer.setColor(ContextCompat.getColor(mContext, android.R.color.darker_gray));
}
} else {
TextView titleTxtView = (TextView) view;
if (mProgressDisableColor > 0)
titleTxtView.setTextColor(ContextCompat.getColor(mContext, mProgressDisableColor));
else
titleTxtView.setTextColor(ContextCompat.getColor(mContext, android.R.color.darker_gray));
}
}
}
}
}
}
public void enableBar(int index) {
final int barsCount = ((LinearLayout) this.getChildAt(0)).getChildCount();
for (int i = 0; i < barsCount; i++) {
FrameLayout rootFrame = (FrameLayout) ((LinearLayout) this.getChildAt(0)).getChildAt(i);
int rootChildCount = rootFrame.getChildCount();
for (int j = 0; j < rootChildCount; j++) {
if ((int) rootFrame.getTag() != index)
continue;
rootFrame.setEnabled(true);
rootFrame.setClickable(true);
View childView = rootFrame.getChildAt(j);
if (childView instanceof LinearLayout) {
//bar
LinearLayout barContainerLinear = ((LinearLayout) childView);
int barContainerCount = barContainerLinear.getChildCount();
for (int k = 0; k < barContainerCount; k++) {
View view = barContainerLinear.getChildAt(k);
if (view instanceof Bar) {
Bar bar = (Bar) view;
LayerDrawable layerDrawable = (LayerDrawable) bar.getProgressDrawable();
layerDrawable.mutate();
ScaleDrawable scaleDrawable = (ScaleDrawable) layerDrawable.getDrawable(1);
GradientDrawable progressLayer = (GradientDrawable) scaleDrawable.getDrawable();
if (progressLayer != null) {
if (mProgressColor > 0)
progressLayer.setColor(ContextCompat.getColor(mContext, mProgressColor));
else
progressLayer.setColor(ContextCompat.getColor(mContext, android.R.color.darker_gray));
}
} else {
TextView titleTxtView = (TextView) view;
if (mProgressDisableColor > 0)
titleTxtView.setTextColor(ContextCompat.getColor(mContext, mBarTitleColor));
else
titleTxtView.setTextColor(ContextCompat.getColor(mContext, android.R.color.darker_gray));
}
}
}
}
}
}
public FieldView(Context context, AttributeSet attrs, int myColor, Drawable d, boolean add_player) {
super(context, attrs);
this.myColor = myColor;
Drawable[] layers = new Drawable[3];
// field border
layers[0] = ContextCompat.getDrawable(context, R.drawable.field_color_border);
// field content
layers[1] = ContextCompat.getDrawable(context, R.drawable.field);
layers[2] = ContextCompat.getDrawable(context, R.drawable.figure4);
layersDrawable = new LayerDrawable(layers);
layersDrawable.mutate();
layersDrawable.setLayerInset(0, 0, 0, 0, 0);
//layersDrawable.setLayerInset(1, 0, 0, 0, 0);
//layersDrawable.setLayerInset(1, 15, 15, 15, 15);
layersDrawable.setLayerInset(1, 50, 50, 50, 50);
layersDrawable.setLayerInset(2, 200, 115, 200, 115);
//layersDrawable.setLayerInset(2, 10, 10, 10, 10);
//layersDrawable.setLayerInset(2, 400, 130, 400, 670);
this.setImageDrawable(layersDrawable);
if (d == null) {
// this.setBackground(d);
layersDrawable.getDrawable(0).setAlpha(0);
layersDrawable.getDrawable(1).setAlpha(0);
layersDrawable.getDrawable(2).setAlpha(0);
}
if (myColor != 0) {
if(myColor==Color.WHITE)
{
layersDrawable.getDrawable(0).setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
}
else
{
layersDrawable.getDrawable(0).setColorFilter(myColor, PorterDuff.Mode.MULTIPLY);
}
layersDrawable.getDrawable(1).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
layersDrawable.getDrawable(2).setAlpha(0);
}
this.add_player = add_player;
if (add_player)
{
layersDrawable.getDrawable(2).setAlpha(255);
}
}
private void clickBarOff(FrameLayout frameLayout) {
pins.get((int) frameLayout.getTag()).setVisibility(View.INVISIBLE);
isOldBarClicked = false;
int childCount = frameLayout.getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = frameLayout.getChildAt(i);
if (childView instanceof LinearLayout) {
LinearLayout linearLayout = (LinearLayout) childView;
Bar bar = (Bar) linearLayout.getChildAt(0);
TextView titleTxtView = (TextView) linearLayout.getChildAt(1);
LayerDrawable layerDrawable = (LayerDrawable) bar.getProgressDrawable();
layerDrawable.mutate();
ScaleDrawable scaleDrawable = (ScaleDrawable) layerDrawable.getDrawable(1);
GradientDrawable progressLayer = (GradientDrawable) scaleDrawable.getDrawable();
if (progressLayer != null) {
progressLayer.setColor(ContextCompat.getColor(mContext, mProgressColor));
}
titleTxtView.setTextColor(ContextCompat.getColor(mContext, mBarTitleColor));
}
}
}