下面列出了android.graphics.drawable.LayerDrawable#findDrawableByLayerId() 实例代码,或者点击链接到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);
}
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();
}
}
/**
* 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);
}
public void changeBackground() {
if (eventCheck) {
setBackgroundResource(R.drawable.background_checkbox);
LayerDrawable layer = (LayerDrawable) getBackground();
GradientDrawable shape = (GradientDrawable) layer
.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(backgroundColor);
} else {
setBackgroundResource(R.drawable.background_switch_ball_uncheck);
}
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dummy, container, false);
Bundle bdl = getArguments();
mMainLayout = (FrameLayout) v.findViewById(R.id.main_layout);
LayerDrawable bgDrawable = (LayerDrawable) mMainLayout.getBackground();
GradientDrawable shape = (GradientDrawable) bgDrawable.findDrawableByLayerId(R.id.background_shape);
shape.setColor(bdl.getInt(EXTRA_COLOR));
return v;
}
public void setBackgroundColor(int color){
this.backgroundColor = color;
if(isEnabled())
beforeBackground = backgroundColor;
LayerDrawable layer = (LayerDrawable) progressView.getBackground();
GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(color);
super.setBackgroundColor(makePressColor());
}
public void changeBackground() {
if (check) {
setBackgroundResource(R.drawable.background_checkbox_check);
LayerDrawable layer = (LayerDrawable) getBackground();
GradientDrawable shape = (GradientDrawable) layer
.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(backgroundColor);
} else {
setBackgroundResource(R.drawable.background_checkbox_uncheck);
}
}
@ReactProp(name = "maximumTrackTintColor", customType = "Color")
public void setMaximumTrackTintColor(ReactSlider view, Integer color) {
LayerDrawable drawable = (LayerDrawable) view.getProgressDrawable().getCurrent();
Drawable background = drawable.findDrawableByLayerId(android.R.id.background);
if (color == null) {
background.clearColorFilter();
} else {
background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
}
public void setBackgroundColor(int color){
this.backgroundColor = color;
if(isEnabled())
beforeBackground = backgroundColor;
LayerDrawable layer = (LayerDrawable) progressView.getBackground();
GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(color);
super.setBackgroundColor(makePressColor());
}
public void setBackgroundColor(int color){
this.backgroundColor = color;
if(isEnabled())
beforeBackground = backgroundColor;
LayerDrawable layer = (LayerDrawable) getBackground();
GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(backgroundColor);
}
public ButtonFloat(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundResource(R.drawable.background_button_float);
sizeRadius = 28;
setDefaultProperties();
icon = new ImageView(context);
if(drawableIcon != null) {
try {
icon.setBackground(drawableIcon);
} catch (NoSuchMethodError e) {
icon.setBackgroundDrawable(drawableIcon);
}
}
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(Utils.dpToPx(sizeIcon, getResources()),Utils.dpToPx(sizeIcon, getResources()));
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
icon.setLayoutParams(params);
addView(icon);
LayerDrawable layer = (LayerDrawable) getBackground();
GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(backgroundColor);
post(new Runnable() {
@Override
public void run() {
showPosition = ViewHelper.getY(ButtonFloat.this) - Utils.dpToPx(24, getResources());
hidePosition = ViewHelper.getY(ButtonFloat.this) + getHeight() * 3;
ViewHelper.setY(ButtonFloat.this, hidePosition);
}
});
}
private void updateProgressDrawable() {
LayerDrawable progressBarDrawable = (LayerDrawable) getProgressDrawable();
Drawable backgroundDrawable = progressBarDrawable.findDrawableByLayerId(android.R.id.background);
Drawable progressDrawable = progressBarDrawable.findDrawableByLayerId(android.R.id.progress);
TintUtil.tintDrawable(backgroundDrawable, mProgressBackgroundColor);
TintUtil.tintDrawable(progressDrawable, mProgressColor);
}
public void setBackgroundColor(int color) {
this.backgroundColor = color;
if (isEnabled())
beforeBackground = backgroundColor;
try {
LayerDrawable layer = (LayerDrawable) getBackground();
GradientDrawable shape = (GradientDrawable) layer
.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(backgroundColor);
rippleColor = makePressColor();
} catch (Exception ex) {
// Without bacground
}
}
private void setSeekBarBg(){
try {
int progressColor = CustomAttrValueUtil.getAttrColorValue(R.attr.colorPrimary,R.color.colorAccent,this);
LayerDrawable layerDrawable = (LayerDrawable) seekBar.getProgressDrawable();
ScaleDrawable scaleDrawable = (ScaleDrawable)layerDrawable.findDrawableByLayerId(android.R.id.progress);
GradientDrawable drawable = (GradientDrawable) scaleDrawable.getDrawable();
drawable.setColor(progressColor);
}catch (Exception e){
e.printStackTrace();
}
}
private void changeBackgroundColor(int color) {
LayerDrawable layer = (LayerDrawable) getBackground();
GradientDrawable shape = (GradientDrawable) layer
.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(color);
}
private void setSmallBackground(View v){
v.setBackgroundResource(R.drawable.background_small_indicator);
LayerDrawable layer = (LayerDrawable) v.getBackground();
GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(colorIndicator);
}
private void setProgressBarColor(int dark) {
LayerDrawable ld = (LayerDrawable) progressSlider.getProgressDrawable();
ClipDrawable clipDrawable = (ClipDrawable) ld.findDrawableByLayerId(android.R.id.progress);
clipDrawable.setColorFilter(dark, PorterDuff.Mode.SRC_IN);
}
public void changeBackground() {
setBackgroundResource(R.drawable.background_checkbox);
LayerDrawable layer = (LayerDrawable) getBackground();
GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(backgroundColor);
}
void init(AttributeSet attrs, int defStyle) {
inflate(getContext(), R.layout.sv_slide_view, this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackground(ContextCompat.getDrawable(getContext(), R.drawable.sv_view_bg));
} else {
setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.drawable.sv_view_bg));
}
slideTextView = findViewById(R.id.sv_text);
slider = findViewById(R.id.sv_slider);
slider.setOnSeekBarChangeListener(this);
slideBackground = getBackground();
buttonLayers = (LayerDrawable) slider.getThumb();
buttonBackground = buttonLayers.findDrawableByLayerId(R.id.buttonBackground);
TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.SlideView,
defStyle, defStyle);
int strokeColor;
float slideTextSize = spToPx(16, getContext());
String slideText;
boolean reverseSlide;
ColorStateList sliderTextColor;
try {
animateSlideText = a.getBoolean(R.styleable.SlideView_sv_animateSlideText, true);
reverseSlide = a.getBoolean(R.styleable.SlideView_sv_reverseSlide, false);
strokeColor = a.getColor(R.styleable.SlideView_sv_strokeColor, ContextCompat.
getColor(getContext(), R.color.sv_stroke_color_default));
slideText = a.getString(R.styleable.SlideView_sv_slideText);
sliderTextColor = a.getColorStateList(R.styleable.SlideView_sv_slideTextColor);
slideTextSize = a.getDimension(R.styleable.SlideView_sv_slideTextSize, slideTextSize);
slideTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, slideTextSize);
setText(slideText);
setTextColor(sliderTextColor == null ? slideTextView.getTextColors() : sliderTextColor);
int buttonImageId = a.getResourceId(R.styleable.SlideView_sv_buttonImage, R.drawable.sv_ic_chevron_double_right);
setButtonImage(ContextCompat.getDrawable(getContext(), buttonImageId));
setButtonImageDisabled(ContextCompat.getDrawable(getContext(), a.getResourceId
(R.styleable.SlideView_sv_buttonImageDisabled, buttonImageId)));
setButtonBackgroundColor(a.getColorStateList(R.styleable.SlideView_sv_buttonBackgroundColor));
setSlideBackgroundColor(a.getColorStateList(R.styleable.SlideView_sv_slideBackgroundColor));
if (a.hasValue(R.styleable.SlideView_sv_strokeColor)) {
Util.setDrawableStroke(slideBackground, strokeColor);
}
if (reverseSlide) {
slider.setRotation(180);
LayoutParams params = ((LayoutParams) slideTextView.getLayoutParams());
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
params.addRule(RelativeLayout.ALIGN_PARENT_END, 0);
params.addRule(RelativeLayout.ALIGN_PARENT_START);
}
slideTextView.setLayoutParams(params);
}
} finally {
a.recycle();
}
}
public void setBackgroundColor(int color){
this.backgroundColor = color;
LayerDrawable layer = (LayerDrawable) getBackground();
GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(backgroundColor);
}