下面列出了怎么用android.graphics.drawable.PaintDrawable的API类实例代码及写法,或者点击链接到github查看源代码。
public static Drawable drawable(final int[] colorBoxes, final float[] position, final GradientAngle gradientAngle) {
ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
AngleCoordinate ac = AngleCoordinate.getAngleCoordinate(gradientAngle, width, height);
LinearGradient linearGradient = new LinearGradient(ac.x1, ac.y1, ac.x2, ac.y2,
colorBoxes,
position,
Shader.TileMode.REPEAT);
return linearGradient;
}
};
PaintDrawable paint = new PaintDrawable();
paint.setShape(new RectShape());
paint.setShaderFactory(shaderFactory);
return paint;
}
/**
* Returns a suitable drawable for ActionBar with theme colors.
*
* @param theme selected theme
* @param dark boolean for choosing dark colors or primary colors
* @param borderBottom bottom border width
* @return drawable to be used in ActionBar
*/
public static Drawable getActionBarDrawable(Activity activity, int theme, boolean dark, float borderBottom) {
int[] colors = activity.getResources().getIntArray(theme);
int color1 = colors[2];
int color2 = colors[1];
if (dark) {
color1 = colors[1];
color2 = colors[0];
}
borderBottom = dipToPx(activity, borderBottom);
PaintDrawable front = new PaintDrawable(color1);
PaintDrawable bottom = new PaintDrawable(color2);
Drawable[] d = {bottom, front};
LayerDrawable drawable = new LayerDrawable(d);
drawable.setLayerInset(1, 0, 0, 0, (int) borderBottom);
return drawable;
}
/**
* Returns a suitable drawable for ActionBar with theme colors.
*
* @param theme selected theme
* @param dark boolean for choosing dark colors or primary colors
* @param borderBottom bottom border width
* @return drawable to be used in ActionBar
*/
public static Drawable getActionBarDrawable(Activity activity, int theme, boolean dark, float borderBottom) {
int[] colors = activity.getResources().getIntArray(theme);
int color1 = colors[2];
int color2 = colors[1];
if (dark) {
color1 = colors[1];
color2 = colors[0];
}
borderBottom = dipToPx(activity, borderBottom);
PaintDrawable front = new PaintDrawable(color1);
PaintDrawable bottom = new PaintDrawable(color2);
Drawable[] d = {bottom, front};
LayerDrawable drawable = new LayerDrawable(d);
drawable.setLayerInset(1, 0, 0, 0, (int) borderBottom);
return drawable;
}
public void setImgText(ImgTextEntity entity) {
final Context context = getContext();
TextView textView = new TextView(context);
@SuppressLint("RestrictedApi")
Typeface typeface = TypefaceCompat.createFromResourcesFontFile(context, getResources(), R.font.crazydailyicon, "", 0);
textView.setTypeface(typeface);
textView.setText(entity.text);
textView.setTextSize(SIZE * BUTTON_RATIO * TEXT_RATIO);
OvalShape shape = new OvalShape();
PaintDrawable shapeDrawable = new PaintDrawable(entity.color);
shapeDrawable.setShape(shape);
textView.setTextColor(Color.WHITE);
textView.setBackground(shapeDrawable);
textView.setGravity(Gravity.CENTER);
final int size = (int) (SIZE * BUTTON_RATIO);
LayoutParams params = new LayoutParams(size, size);
params.gravity = Gravity.END | Gravity.BOTTOM;
addView(textView, params);
mButtonView = textView;
mButtonView.setOnClickListener(v -> {
if (isOpen) {
close();
} else {
show();
}
});
}
/**
* Creates an approximated cubic gradient using a multi-stop linear gradient. See
* <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
* details.
*/
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {
// Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
int cacheKeyHash = baseColor;
cacheKeyHash = 31 * cacheKeyHash + numStops;
cacheKeyHash = 31 * cacheKeyHash + gravity;
Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
if (cachedGradient != null) {
return cachedGradient;
}
numStops = Math.max(numStops, 2);
PaintDrawable paintDrawable = new PaintDrawable();
paintDrawable.setShape(new RectShape());
final int[] stopColors = new int[numStops];
int red = Color.red(baseColor);
int green = Color.green(baseColor);
int blue = Color.blue(baseColor);
int alpha = Color.alpha(baseColor);
for (int i = 0; i < numStops; i++) {
float x = i * 1f / (numStops - 1);
float opacity = MathUtil.constrain(0, 1, (float) Math.pow(x, 3));
stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
}
final float x0, x1, y0, y1;
switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT: x0 = 1; x1 = 0; break;
case Gravity.RIGHT: x0 = 0; x1 = 1; break;
default: x0 = 0; x1 = 0; break;
}
switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
case Gravity.TOP: y0 = 1; y1 = 0; break;
case Gravity.BOTTOM: y0 = 0; y1 = 1; break;
default: y0 = 0; y1 = 0; break;
}
paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
return new LinearGradient(
width * x0,
height * y0,
width * x1,
height * y1,
stopColors, null,
Shader.TileMode.CLAMP);
}
});
cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
return paintDrawable;
}
/**
* Creates an approximated cubic gradient using a multi-stop linear gradient. See
* <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
* details.
*/
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {
// Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
int cacheKeyHash = baseColor;
cacheKeyHash = 31 * cacheKeyHash + numStops;
cacheKeyHash = 31 * cacheKeyHash + gravity;
Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
if (cachedGradient != null) {
return cachedGradient;
}
numStops = Math.max(numStops, 2);
PaintDrawable paintDrawable = new PaintDrawable();
paintDrawable.setShape(new RectShape());
final int[] stopColors = new int[numStops];
int red = Color.red(baseColor);
int green = Color.green(baseColor);
int blue = Color.blue(baseColor);
int alpha = Color.alpha(baseColor);
for (int i = 0; i < numStops; i++) {
float x = i * 1f / (numStops - 1);
float opacity = MathUtil.constrain(0, 1, (float) Math.pow(x, 3));
stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
}
final float x0, x1, y0, y1;
switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT: x0 = 1; x1 = 0; break;
case Gravity.RIGHT: x0 = 0; x1 = 1; break;
default: x0 = 0; x1 = 0; break;
}
switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
case Gravity.TOP: y0 = 1; y1 = 0; break;
case Gravity.BOTTOM: y0 = 0; y1 = 1; break;
default: y0 = 0; y1 = 0; break;
}
paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
return new LinearGradient(
width * x0,
height * y0,
width * x1,
height * y1,
stopColors, null,
Shader.TileMode.CLAMP);
}
});
cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
return paintDrawable;
}
/**
* Creates an approximated cubic gradient using a multi-stop linear gradient. See
* <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
* details.
*/
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {
// Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
int cacheKeyHash = baseColor;
cacheKeyHash = 31 * cacheKeyHash + numStops;
cacheKeyHash = 31 * cacheKeyHash + gravity;
Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
if (cachedGradient != null) {
return cachedGradient;
}
numStops = Math.max(numStops, 2);
PaintDrawable paintDrawable = new PaintDrawable();
paintDrawable.setShape(new RectShape());
final int[] stopColors = new int[numStops];
int red = Color.red(baseColor);
int green = Color.green(baseColor);
int blue = Color.blue(baseColor);
int alpha = Color.alpha(baseColor);
for (int i = 0; i < numStops; i++) {
float x = i * 1f / (numStops - 1);
/* float opacity = MathUtil.constrain(0, 1, (float) Math.pow(x, 3));
stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);*/
}
final float x0, x1, y0, y1;
switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT: x0 = 1; x1 = 0; break;
case Gravity.RIGHT: x0 = 0; x1 = 1; break;
default: x0 = 0; x1 = 0; break;
}
switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
case Gravity.TOP: y0 = 1; y1 = 0; break;
case Gravity.BOTTOM: y0 = 0; y1 = 1; break;
default: y0 = 0; y1 = 0; break;
}
paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient linearGradient = new LinearGradient(
width * x0,
height * y0,
width * x1,
height * y1,
stopColors, null,
Shader.TileMode.CLAMP);
return linearGradient;
}
});
cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
return paintDrawable;
}
/**
* Creates an approximated cubic gradient using a multi-stop linear gradient. See
* <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
* details.
*/
public static Drawable makeCubicGradientScrimDrawable(@ColorInt int baseColor,
int numStops,
int gravity) {
numStops = Math.max(numStops, 2);
PaintDrawable paintDrawable = new PaintDrawable();
paintDrawable.setShape(new RectShape());
final int[] stopColors = new int[numStops];
int alpha = Color.alpha(baseColor);
for (int i = 0; i < numStops; i++) {
float x = i * 1f / (numStops - 1);
float opacity = MathUtils.constrain(0, 1, (float) Math.pow(x, 3));
stopColors[i] = ColorUtils.modifyAlpha(baseColor, (int) (alpha * opacity));
}
final float x0, x1, y0, y1;
switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT:
x0 = 1;
x1 = 0;
break;
case Gravity.RIGHT:
x0 = 0;
x1 = 1;
break;
default:
x0 = 0;
x1 = 0;
break;
}
switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
case Gravity.TOP:
y0 = 1;
y1 = 0;
break;
case Gravity.BOTTOM:
y0 = 0;
y1 = 1;
break;
default:
y0 = 0;
y1 = 0;
break;
}
paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient linearGradient = new LinearGradient(
width * x0,
height * y0,
width * x1,
height * y1,
stopColors, null,
Shader.TileMode.CLAMP);
return linearGradient;
}
});
return paintDrawable;
}
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);
}
private void init(AttributeSet attrs) {
if (attributes == null)
attributes = new Attributes(this, getResources());
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.fl_FlatRadioButton);
// getting common attributes
int customTheme = a.getResourceId(R.styleable.fl_FlatRadioButton_fl_theme, Attributes.DEFAULT_THEME);
attributes.setThemeSilent(customTheme, getResources());
attributes.setFontFamily(a.getString(R.styleable.fl_FlatRadioButton_fl_fontFamily));
attributes.setFontWeight(a.getString(R.styleable.fl_FlatRadioButton_fl_fontWeight));
attributes.setFontExtension(a.getString(R.styleable.fl_FlatRadioButton_fl_fontExtension));
attributes.setSize(a.getDimensionPixelSize(R.styleable.fl_FlatRadioButton_fl_size, Attributes.DEFAULT_SIZE_PX));
attributes.setRadius(attributes.getSize() / 2);
attributes.setBorderWidth(a.getDimensionPixelSize(R.styleable.fl_FlatRadioButton_fl_borderWidth, Attributes.DEFAULT_BORDER_WIDTH_PX));
// getting view specific attributes
dotMargin = a.getDimensionPixelSize(R.styleable.fl_FlatRadioButton_fl_dotMargin, dotMargin);
a.recycle();
}
// creating unchecked-enabled state drawable
GradientDrawable uncheckedEnabled = new GradientDrawable();
uncheckedEnabled.setCornerRadius(attributes.getRadius());
uncheckedEnabled.setSize(attributes.getSize(), attributes.getSize());
uncheckedEnabled.setColor(Color.TRANSPARENT);
uncheckedEnabled.setStroke(attributes.getBorderWidth(), attributes.getColor(2));
// creating checked-enabled state drawable
GradientDrawable checkedOutside = new GradientDrawable();
checkedOutside.setCornerRadius(attributes.getSize());
checkedOutside.setSize(attributes.getSize(), attributes.getSize());
checkedOutside.setColor(Color.TRANSPARENT);
checkedOutside.setStroke(attributes.getBorderWidth(), attributes.getColor(2));
PaintDrawable checkedCore = new PaintDrawable(attributes.getColor(2));
checkedCore.setCornerRadius(attributes.getSize());
checkedCore.setIntrinsicHeight(attributes.getSize());
checkedCore.setIntrinsicWidth(attributes.getSize());
InsetDrawable checkedInside = new InsetDrawable(checkedCore,
attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin,
attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin);
Drawable[] checkedEnabledDrawable = {checkedOutside, checkedInside};
LayerDrawable checkedEnabled = new LayerDrawable(checkedEnabledDrawable);
// creating unchecked-enabled state drawable
GradientDrawable uncheckedDisabled = new GradientDrawable();
uncheckedDisabled.setCornerRadius(attributes.getRadius());
uncheckedDisabled.setSize(attributes.getSize(), attributes.getSize());
uncheckedDisabled.setColor(Color.TRANSPARENT);
uncheckedDisabled.setStroke(attributes.getBorderWidth(), attributes.getColor(3));
// creating checked-disabled state drawable
GradientDrawable checkedOutsideDisabled = new GradientDrawable();
checkedOutsideDisabled.setCornerRadius(attributes.getRadius());
checkedOutsideDisabled.setSize(attributes.getSize(), attributes.getSize());
checkedOutsideDisabled.setColor(Color.TRANSPARENT);
checkedOutsideDisabled.setStroke(attributes.getBorderWidth(), attributes.getColor(3));
PaintDrawable checkedCoreDisabled = new PaintDrawable(attributes.getColor(3));
checkedCoreDisabled.setCornerRadius(attributes.getRadius());
checkedCoreDisabled.setIntrinsicHeight(attributes.getSize());
checkedCoreDisabled.setIntrinsicWidth(attributes.getSize());
InsetDrawable checkedInsideDisabled = new InsetDrawable(checkedCoreDisabled,
attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin,
attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin);
Drawable[] checkedDisabledDrawable = {checkedOutsideDisabled, checkedInsideDisabled};
LayerDrawable checkedDisabled = new LayerDrawable(checkedDisabledDrawable);
StateListDrawable states = new StateListDrawable();
states.addState(new int[]{-android.R.attr.state_checked, android.R.attr.state_enabled}, uncheckedEnabled);
states.addState(new int[]{android.R.attr.state_checked, android.R.attr.state_enabled}, checkedEnabled);
states.addState(new int[]{-android.R.attr.state_checked, -android.R.attr.state_enabled}, uncheckedDisabled);
states.addState(new int[]{android.R.attr.state_checked, -android.R.attr.state_enabled}, checkedDisabled);
setButtonDrawable(states);
// setting padding for avoiding text to be appear on icon
setPadding(attributes.getSize() / 4 * 5, 0, 0, 0);
setTextColor(attributes.getColor(2));
// check for IDE preview render
if(!this.isInEditMode()) {
Typeface typeface = FlatUI.getFont(getContext(), attributes);
if (typeface != null) setTypeface(typeface);
}
}
private void init(AttributeSet attrs) {
if (attributes == null)
attributes = new Attributes(this, getResources());
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.fl_FlatCheckBox);
// getting common attributes
int customTheme = a.getResourceId(R.styleable.fl_FlatCheckBox_fl_theme, Attributes.DEFAULT_THEME);
attributes.setThemeSilent(customTheme, getResources());
attributes.setFontFamily(a.getString(R.styleable.fl_FlatCheckBox_fl_fontFamily));
attributes.setFontWeight(a.getString(R.styleable.fl_FlatCheckBox_fl_fontWeight));
attributes.setFontExtension(a.getString(R.styleable.fl_FlatCheckBox_fl_fontExtension));
attributes.setSize(a.getDimensionPixelSize(R.styleable.fl_FlatCheckBox_fl_size, Attributes.DEFAULT_SIZE_PX));
attributes.setRadius(a.getDimensionPixelSize(R.styleable.fl_FlatCheckBox_fl_cornerRadius, Attributes.DEFAULT_RADIUS_PX));
attributes.setBorderWidth(attributes.getSize() / 10);
// getting view specific attributes
dotMargin = a.getDimensionPixelSize(R.styleable.fl_FlatCheckBox_fl_dotMargin, dotMargin);
a.recycle();
}
// creating unchecked-enabled state drawable
GradientDrawable uncheckedEnabled = new GradientDrawable();
uncheckedEnabled.setCornerRadius(attributes.getRadius());
uncheckedEnabled.setSize(attributes.getSize(), attributes.getSize());
uncheckedEnabled.setColor(Color.TRANSPARENT);
uncheckedEnabled.setStroke(attributes.getBorderWidth(), attributes.getColor(2));
// creating checked-enabled state drawable
GradientDrawable checkedOutside = new GradientDrawable();
checkedOutside.setCornerRadius(attributes.getRadius());
checkedOutside.setSize(attributes.getSize(), attributes.getSize());
checkedOutside.setColor(Color.TRANSPARENT);
checkedOutside.setStroke(attributes.getBorderWidth(), attributes.getColor(2));
PaintDrawable checkedCore = new PaintDrawable(attributes.getColor(2));
checkedCore.setCornerRadius(attributes.getRadius());
checkedCore.setIntrinsicHeight(attributes.getSize());
checkedCore.setIntrinsicWidth(attributes.getSize());
InsetDrawable checkedInside = new InsetDrawable(checkedCore,
attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin,
attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin);
Drawable[] checkedEnabledDrawable = {checkedOutside, checkedInside};
LayerDrawable checkedEnabled = new LayerDrawable(checkedEnabledDrawable);
// creating unchecked-enabled state drawable
GradientDrawable uncheckedDisabled = new GradientDrawable();
uncheckedDisabled.setCornerRadius(attributes.getRadius());
uncheckedDisabled.setSize(attributes.getSize(), attributes.getSize());
uncheckedDisabled.setColor(Color.TRANSPARENT);
uncheckedDisabled.setStroke(attributes.getBorderWidth(), attributes.getColor(3));
// creating checked-disabled state drawable
GradientDrawable checkedOutsideDisabled = new GradientDrawable();
checkedOutsideDisabled.setCornerRadius(attributes.getRadius());
checkedOutsideDisabled.setSize(attributes.getSize(), attributes.getSize());
checkedOutsideDisabled.setColor(Color.TRANSPARENT);
checkedOutsideDisabled.setStroke(attributes.getBorderWidth(), attributes.getColor(3));
PaintDrawable checkedCoreDisabled = new PaintDrawable(attributes.getColor(3));
checkedCoreDisabled.setCornerRadius(attributes.getRadius());
checkedCoreDisabled.setIntrinsicHeight(attributes.getSize());
checkedCoreDisabled.setIntrinsicWidth(attributes.getSize());
InsetDrawable checkedInsideDisabled = new InsetDrawable(checkedCoreDisabled,
attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin,
attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin);
Drawable[] checkedDisabledDrawable = {checkedOutsideDisabled, checkedInsideDisabled};
LayerDrawable checkedDisabled = new LayerDrawable(checkedDisabledDrawable);
StateListDrawable states = new StateListDrawable();
states.addState(new int[]{-android.R.attr.state_checked, android.R.attr.state_enabled}, uncheckedEnabled);
states.addState(new int[]{android.R.attr.state_checked, android.R.attr.state_enabled}, checkedEnabled);
states.addState(new int[]{-android.R.attr.state_checked, -android.R.attr.state_enabled}, uncheckedDisabled);
states.addState(new int[]{android.R.attr.state_checked, -android.R.attr.state_enabled}, checkedDisabled);
setButtonDrawable(states);
// setting padding for avoiding text to appear on icon
setPadding(attributes.getSize() / 4 * 5, 0, 0, 0);
setTextColor(attributes.getColor(2));
// check for IDE preview render
if(!this.isInEditMode()) {
Typeface typeface = FlatUI.getFont(getContext(), attributes);
if (typeface != null) setTypeface(typeface);
}
}
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);
}
private void init(AttributeSet attrs) {
if (attributes == null)
attributes = new Attributes(this, getResources());
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.fl_FlatRadioButton);
// getting common attributes
int customTheme = a.getResourceId(R.styleable.fl_FlatRadioButton_fl_theme, Attributes.DEFAULT_THEME);
attributes.setThemeSilent(customTheme, getResources());
attributes.setFontFamily(a.getString(R.styleable.fl_FlatRadioButton_fl_fontFamily));
attributes.setFontWeight(a.getString(R.styleable.fl_FlatRadioButton_fl_fontWeight));
attributes.setFontExtension(a.getString(R.styleable.fl_FlatRadioButton_fl_fontExtension));
attributes.setSize(a.getDimensionPixelSize(R.styleable.fl_FlatRadioButton_fl_size, Attributes.DEFAULT_SIZE_PX));
attributes.setRadius(attributes.getSize() / 2);
attributes.setBorderWidth(a.getDimensionPixelSize(R.styleable.fl_FlatRadioButton_fl_borderWidth, Attributes.DEFAULT_BORDER_WIDTH_PX));
// getting view specific attributes
dotMargin = a.getDimensionPixelSize(R.styleable.fl_FlatRadioButton_fl_dotMargin, dotMargin);
a.recycle();
}
// creating unchecked-enabled state drawable
GradientDrawable uncheckedEnabled = new GradientDrawable();
uncheckedEnabled.setCornerRadius(attributes.getRadius());
uncheckedEnabled.setSize(attributes.getSize(), attributes.getSize());
uncheckedEnabled.setColor(Color.TRANSPARENT);
uncheckedEnabled.setStroke(attributes.getBorderWidth(), attributes.getColor(2));
// creating checked-enabled state drawable
GradientDrawable checkedOutside = new GradientDrawable();
checkedOutside.setCornerRadius(attributes.getSize());
checkedOutside.setSize(attributes.getSize(), attributes.getSize());
checkedOutside.setColor(Color.TRANSPARENT);
checkedOutside.setStroke(attributes.getBorderWidth(), attributes.getColor(2));
PaintDrawable checkedCore = new PaintDrawable(attributes.getColor(2));
checkedCore.setCornerRadius(attributes.getSize());
checkedCore.setIntrinsicHeight(attributes.getSize());
checkedCore.setIntrinsicWidth(attributes.getSize());
InsetDrawable checkedInside = new InsetDrawable(checkedCore,
attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin,
attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin);
Drawable[] checkedEnabledDrawable = {checkedOutside, checkedInside};
LayerDrawable checkedEnabled = new LayerDrawable(checkedEnabledDrawable);
// creating unchecked-enabled state drawable
GradientDrawable uncheckedDisabled = new GradientDrawable();
uncheckedDisabled.setCornerRadius(attributes.getRadius());
uncheckedDisabled.setSize(attributes.getSize(), attributes.getSize());
uncheckedDisabled.setColor(Color.TRANSPARENT);
uncheckedDisabled.setStroke(attributes.getBorderWidth(), attributes.getColor(3));
// creating checked-disabled state drawable
GradientDrawable checkedOutsideDisabled = new GradientDrawable();
checkedOutsideDisabled.setCornerRadius(attributes.getRadius());
checkedOutsideDisabled.setSize(attributes.getSize(), attributes.getSize());
checkedOutsideDisabled.setColor(Color.TRANSPARENT);
checkedOutsideDisabled.setStroke(attributes.getBorderWidth(), attributes.getColor(3));
PaintDrawable checkedCoreDisabled = new PaintDrawable(attributes.getColor(3));
checkedCoreDisabled.setCornerRadius(attributes.getRadius());
checkedCoreDisabled.setIntrinsicHeight(attributes.getSize());
checkedCoreDisabled.setIntrinsicWidth(attributes.getSize());
InsetDrawable checkedInsideDisabled = new InsetDrawable(checkedCoreDisabled,
attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin,
attributes.getBorderWidth() + dotMargin, attributes.getBorderWidth() + dotMargin);
Drawable[] checkedDisabledDrawable = {checkedOutsideDisabled, checkedInsideDisabled};
LayerDrawable checkedDisabled = new LayerDrawable(checkedDisabledDrawable);
StateListDrawable states = new StateListDrawable();
states.addState(new int[]{-android.R.attr.state_checked, android.R.attr.state_enabled}, uncheckedEnabled);
states.addState(new int[]{android.R.attr.state_checked, android.R.attr.state_enabled}, checkedEnabled);
states.addState(new int[]{-android.R.attr.state_checked, -android.R.attr.state_enabled}, uncheckedDisabled);
states.addState(new int[]{android.R.attr.state_checked, -android.R.attr.state_enabled}, checkedDisabled);
setButtonDrawable(states);
// setting padding for avoiding text to be appear on icon
setPadding(attributes.getSize() / 4 * 5, 0, 0, 0);
setTextColor(attributes.getColor(2));
// check for IDE preview render
if(!this.isInEditMode()) {
Typeface typeface = FlatUI.getFont(getContext(), attributes);
if (typeface != null) setTypeface(typeface);
}
}