下面列出了android.graphics.drawable.LayerDrawable#getDrawable() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Drawable fromDrawable(Drawable drawable, Resources r) {
if (drawable != null) {
if (drawable instanceof SelectableRoundedCornerDrawable) {
return drawable;
}
else if (drawable instanceof LayerDrawable) {
LayerDrawable ld = (LayerDrawable) drawable;
final int num = ld.getNumberOfLayers();
for (int i = 0; i < num; i++) {
Drawable d = ld.getDrawable(i);
ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d, r));
}
return ld;
}
Bitmap bm = drawableToBitmap(drawable);
if (bm != null) {
return new SelectableRoundedCornerDrawable(bm, r);
} else {
Log.w(TAG, "Failed to create bitmap from drawable!");
}
}
return drawable;
}
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;
}
@Nullable
private MaterialShapeDrawable getMaterialShapeDrawable(boolean getSurfaceColorStrokeDrawable) {
if (rippleDrawable != null && rippleDrawable.getNumberOfLayers() > 0) {
if (IS_LOLLIPOP) {
InsetDrawable insetDrawable = (InsetDrawable) rippleDrawable.getDrawable(0);
LayerDrawable layerDrawable = (LayerDrawable) insetDrawable.getDrawable();
return (MaterialShapeDrawable)
layerDrawable.getDrawable(getSurfaceColorStrokeDrawable ? 0 : 1);
} else {
return (MaterialShapeDrawable)
rippleDrawable.getDrawable(getSurfaceColorStrokeDrawable ? 0 : 1);
}
}
return null;
}
/**
* @param context context used to get resources
* @param resourceID drawable resourceID to a LayerDrawable
* @param fillColor fill color to apply to drawable
* @param strokeColor stroke color to apply to drawable
* @param strokePx width of stroke (pixels)
* @return a Bitmap of the drawable
*/
public static Bitmap layerDrawableToBitmap(Context context, int resourceID, int fillColor, int strokeColor, int strokePx)
{
Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), resourceID, null);
LayerDrawable layers = (LayerDrawable)drawable;
int w = 1, h = 1;
if (layers != null)
{
Drawable layer0 = layers.getDrawable(0);
w = layers.getIntrinsicWidth();
h = (layer0 != null ? layer0.getIntrinsicHeight() : layers.getIntrinsicHeight());
}
Drawable tinted = tintDrawable(layers, fillColor, strokeColor, strokePx);
return drawableToBitmap(context, tinted, w, h, true);
}
private void setCancelBackgroundColor(int alpha, int color)
{
LayerDrawable layerDrawable = (LayerDrawable) mCancelButton.getBackground();
Drawable d = layerDrawable.getDrawable(0);
if (d instanceof AnimatedCircleDrawable)
{
AnimatedCircleDrawable animatedCircleDrawable = (AnimatedCircleDrawable) d;
animatedCircleDrawable.setColor(color);
animatedCircleDrawable.setAlpha(alpha);
} else if (d instanceof ColorDrawable)
{
ColorDrawable colorDrawable = (ColorDrawable) d;
if (!ApiHelper.isLOrHigher())
{
colorDrawable.setColor(color);
}
colorDrawable.setAlpha(alpha);
}
}
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;
}
/**
* Switches between colors for rest timer and workout timer
* according to the boolean flag provided/
*
* @param guiFlip True for workout phase colors, false for rest phase colors
*/
private void setWorkoutGuiColors(boolean guiFlip) {
int textColor = guiFlip ? R.color.white : R.color.black;
int backgroundColor = guiFlip ? R.color.lightblue : R.color.white;
int progressBackgroundColor = guiFlip ? R.color.white : R.color.lightblue;
int buttonColor = guiFlip ? R.color.white : R.color.darkblue;
currentSetsInfo.setTextColor(getResources().getColor(textColor));
workoutTitle.setTextColor(getResources().getColor(textColor));
workoutTimer.setTextColor(getResources().getColor(textColor));
prevTimer.setColorFilter(getResources().getColor(buttonColor));
nextTimer.setColorFilter(getResources().getColor(buttonColor));
View view = findViewById(R.id.workout_content);
view.setBackgroundColor(getResources().getColor(backgroundColor));
LayerDrawable progressBarDrawable = (LayerDrawable) progressBar.getProgressDrawable();
Drawable backgroundDrawable = progressBarDrawable.getDrawable(0);
Drawable progressDrawable = progressBarDrawable.getDrawable(1);
progressDrawable.setColorFilter(ContextCompat.getColor(this, R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
backgroundDrawable.setColorFilter(ContextCompat.getColor(this, progressBackgroundColor), PorterDuff.Mode.SRC_IN);
//progressBar.setProgressBackgroundTintList(ColorStateList.valueOf(getResources().getColor(progressBackgroundColor)));
}
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;
}
public static Drawable fromDrawable(Drawable drawable, Resources r) {
if (drawable != null) {
if (drawable instanceof SelectableRoundedCornerDrawable) {
return drawable;
} else if (drawable instanceof LayerDrawable) {
LayerDrawable ld = (LayerDrawable) drawable;
final int num = ld.getNumberOfLayers();
for (int i = 0; i < num; i++) {
Drawable d = ld.getDrawable(i);
ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d, r));
}
return ld;
}
Bitmap bm = drawableToBitmap1(drawable);
if (bm != null) {
return new SelectableRoundedCornerDrawable(bm, r);
} else {
MyLg.w(TAG, "Failed to create bitmap from drawable!");
}
}
return drawable;
}
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;
}
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 void setMonthBackgroundSelected(int color) {
LayerDrawable layerDrawable = (LayerDrawable) ContextCompat.getDrawable(context, R.drawable.month_selected);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.getDrawable(1);
gradientDrawable.setColor(color);
layerDrawable.setDrawableByLayerId(1, gradientDrawable);
StateListDrawable states = new StateListDrawable();
states.addState(new int[]{android.R.attr.state_selected}, gradientDrawable);
states.addState(new int[]{android.R.attr.state_pressed}, gradientDrawable);
states.addState(new int[]{}, ContextCompat.getDrawable(context, R.drawable.month_default));
layoutMain.setBackground(states);
}
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);
} else {
Log.w(TAG, "Failed to create bitmap from drawable!");
}
}
return drawable;
}
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);
}
else
{
Log.w(TAG, "Failed to create bitmap from drawable!");
}
}
return drawable;
}
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);
} else {
Log.w(TAG, "Failed to create bitmap from drawable!");
}
}
return drawable;
}
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);
} else {
Log.w(TAG, "Failed to create bitmap from drawable!");
}
}
return drawable;
}
private void setMonthBackgroundSelected(int color) {
LayerDrawable layerDrawable = (LayerDrawable) ContextCompat.getDrawable(context, R.drawable.month_selected);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.getDrawable(1);
gradientDrawable.setColor(color);
layerDrawable.setDrawableByLayerId(1, gradientDrawable);
StateListDrawable states = new StateListDrawable();
states.addState(new int[]{android.R.attr.state_selected}, gradientDrawable);
states.addState(new int[]{android.R.attr.state_pressed}, gradientDrawable);
states.addState(new int[]{}, ContextCompat.getDrawable(context, R.drawable.month_default));
layoutMain.setBackground(states);
}
private void setCurSeekBarStyle() {
for (SeekBar seekBar : mSeekBars) {
//获取seekBar的layer-list drawable对象
LayerDrawable layerDrawable = (LayerDrawable) seekBar.getProgressDrawable();
//层次包括背景图和进度,所以进度直接设为1,获取并设置进度条背景
Drawable drawable = layerDrawable.getDrawable(1);
drawable.setColorFilter(mStrokeColors[mTheme], PorterDuff.Mode.SRC);
//获取thumb背景
Drawable thumb = seekBar.getThumb();
thumb.setColorFilter(mStrokeColors[mTheme], PorterDuff.Mode.SRC);
}
}
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 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));
}
}
}
}
}
}