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

The following examples show how to use android.graphics.drawable.LayerDrawable#setDrawableByLayerId() . 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: BottomBar.java    From Camera2 with Apache License 2.0 6 votes vote down vote up
private LayerDrawable applyCircleDrawableToShutterBackground(LayerDrawable shutterBackground)
{
    // the background for video has a circle_item drawable placeholder
    // that gets replaced by an AnimatedCircleDrawable for the cool
    // shrink-down-to-a-circle effect
    // all other modes need not do this replace
    Drawable d = shutterBackground.findDrawableByLayerId(R.id.circle_item);
    if (d != null)
    {
        Drawable animatedCircleDrawable =
                new AnimatedCircleDrawable((int) mCircleRadius);
        shutterBackground
                .setDrawableByLayerId(R.id.circle_item, animatedCircleDrawable);
        animatedCircleDrawable.setLevel(DRAWABLE_MAX_LEVEL);
    }

    return shutterBackground;
}
 
Example 2
Source File: RoundedDrawable.java    From android-common-utils with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: RoundedDrawable.java    From Loop with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: RoundedDrawable.java    From ClipCircleHeadLikeQQ with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: BaseActivity.java    From RetailStore with Apache License 2.0 6 votes vote down vote up
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);
    }
 
Example 6
Source File: SelectableRoundedImageView.java    From sealtalk-android with MIT License 6 votes vote down vote up
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;
        }
 
Example 7
Source File: RoundedDrawable.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 8
Source File: SelectableRoundedImageView.java    From Expert-Android-Programming with MIT License 6 votes vote down vote up
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;
}
 
Example 9
Source File: RoundedDrawable.java    From DMusic with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 10
Source File: RoundedDrawable.java    From Android with MIT License 6 votes vote down vote up
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;
}
 
Example 11
Source File: SelectableRoundedImageView.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
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;
        }
 
Example 12
Source File: RoundedDrawable.java    From Common with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 13
Source File: MonthAdapter.java    From RackMonthPicker with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: RoundedDrawable.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 15
Source File: MaterialProgressBar.java    From zhizhihu with Apache License 2.0 5 votes vote down vote up
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));
    }
}
 
Example 16
Source File: MonthAdapter.java    From RackMonthPicker with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 17
Source File: RoundedDrawable.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 18
Source File: RoundedDrawable.java    From MyBlogDemo with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 19
Source File: FlatSeekBar.java    From GreenDamFileExploere with Apache License 2.0 4 votes vote down vote up
private void init(AttributeSet attrs) {

		if (attributes == null)
			attributes = new Attributes(this, getResources());

		if (attrs != null) {
			TypedArray a = getContext().obtainStyledAttributes(attrs,
					R.styleable.fl_FlatSeekBar);

			// getting common attributes
			int customTheme = a.getResourceId(
					R.styleable.fl_FlatSeekBar_fl_theme,
					Attributes.DEFAULT_THEME);
			attributes.setThemeSilent(customTheme, getResources());

			attributes.setSize(a.getDimensionPixelSize(
					R.styleable.fl_FlatSeekBar_fl_size,
					Attributes.DEFAULT_SIZE_PX));

			a.recycle();
		}

		// setting thumb
		PaintDrawable thumb = new PaintDrawable(attributes.getColor(0));
		thumb.setCornerRadius(attributes.getSize() * 9 / 8);
		thumb.setIntrinsicWidth(attributes.getSize() * 9 / 4);
		thumb.setIntrinsicHeight(attributes.getSize() * 9 / 4);
		setThumb(thumb);

		// progress
		PaintDrawable progress = new PaintDrawable(attributes.getColor(1));
		progress.setCornerRadius(attributes.getSize());
		progress.setIntrinsicHeight(attributes.getSize());
		progress.setIntrinsicWidth(attributes.getSize());
		progress.setDither(true);
		ClipDrawable progressClip = new ClipDrawable(progress, Gravity.LEFT,
				ClipDrawable.HORIZONTAL);

		// secondary progress
		PaintDrawable secondary = new PaintDrawable(attributes.getColor(2));
		secondary.setCornerRadius(attributes.getSize());
		secondary.setIntrinsicHeight(attributes.getSize());
		ClipDrawable secondaryProgressClip = new ClipDrawable(secondary,
				Gravity.LEFT, ClipDrawable.HORIZONTAL);

		// background
		PaintDrawable background = new PaintDrawable(attributes.getColor(3));
		background.setCornerRadius(attributes.getSize());
		background.setIntrinsicHeight(attributes.getSize());

		// applying drawable
		LayerDrawable ld = (LayerDrawable) getProgressDrawable();
		ld.setDrawableByLayerId(android.R.id.background, background);
		ld.setDrawableByLayerId(android.R.id.progress, progressClip);
		ld.setDrawableByLayerId(android.R.id.secondaryProgress,
				secondaryProgressClip);
	}
 
Example 20
Source File: FlatSeekBar.java    From FamilyChat with Apache License 2.0 4 votes vote down vote up
private void init(AttributeSet attrs) {

        if (attributes == null)
            attributes = new Attributes(this, getResources());

        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, com.cengalabs.flatui.R.styleable.fl_FlatSeekBar);

            // getting common attributes
            int customTheme = a.getResourceId(com.cengalabs.flatui.R.styleable.fl_FlatSeekBar_fl_theme, Attributes.DEFAULT_THEME);
            attributes.setThemeSilent(customTheme, getResources());

            attributes.setSize(a.getDimensionPixelSize(com.cengalabs.flatui.R.styleable.fl_FlatSeekBar_fl_size, Attributes.DEFAULT_SIZE_PX));

            a.recycle();
        }

        // setting thumb
        PaintDrawable thumb = new PaintDrawable(attributes.getColor(0));
        thumb.setCornerRadius(attributes.getSize() * 9 / 8);
        thumb.setIntrinsicWidth(attributes.getSize() * 9 / 4);
        thumb.setIntrinsicHeight(attributes.getSize() * 9 / 4);
        setThumb(thumb);

        // progress
        PaintDrawable progress = new PaintDrawable(attributes.getColor(1));
        progress.setCornerRadius(attributes.getSize());
        progress.setIntrinsicHeight(attributes.getSize());
        progress.setIntrinsicWidth(attributes.getSize());
        progress.setDither(true);
        ClipDrawable progressClip = new ClipDrawable(progress, Gravity.LEFT, ClipDrawable.HORIZONTAL);

        // secondary progress
        PaintDrawable secondary = new PaintDrawable(attributes.getColor(2));
        secondary.setCornerRadius(attributes.getSize());
        secondary.setIntrinsicHeight(attributes.getSize());
        ClipDrawable secondaryProgressClip = new ClipDrawable(secondary, Gravity.LEFT, ClipDrawable.HORIZONTAL);

        // background
        PaintDrawable background = new PaintDrawable(attributes.getColor(3));
        background.setCornerRadius(attributes.getSize());
        background.setIntrinsicHeight(attributes.getSize());

        // applying drawable
        LayerDrawable ld = (LayerDrawable) getProgressDrawable();
        ld.setDrawableByLayerId(R.id.background, background);
        ld.setDrawableByLayerId(R.id.progress, progressClip);
        ld.setDrawableByLayerId(R.id.secondaryProgress, secondaryProgressClip);
    }