下面列出了怎么用android.graphics.drawable.ScaleDrawable的API类实例代码及写法,或者点击链接到github查看源代码。
public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
if (drawable instanceof DrawableContainer) {
// If we have a DrawableContainer, let's traverse it's child array
final Drawable.ConstantState state = drawable.getConstantState();
if (state instanceof DrawableContainer.DrawableContainerState) {
final DrawableContainer.DrawableContainerState containerState =
(DrawableContainer.DrawableContainerState) state;
for (final Drawable child : containerState.getChildren()) {
if (!canSafelyMutateDrawable(child)) {
return false;
}
}
}
} else if (drawable instanceof DrawableWrapper) {
return canSafelyMutateDrawable(
Objects.requireNonNull(((DrawableWrapper) drawable).getDrawable()));
} else if (drawable instanceof ScaleDrawable) {
return canSafelyMutateDrawable(Objects.requireNonNull(((ScaleDrawable) drawable).getDrawable()));
}
return true;
}
private void initDrawables(@NonNull Context context)
{
majorTick = ContextCompat.getDrawable(context, R.drawable.ic_tick);
minorTick = ContextCompat.getDrawable(context, R.drawable.ic_tick);
centerTick = ContextCompat.getDrawable(context, R.drawable.ic_tick_center);
initTick(majorTick, true);
initTick(minorTick, false);
initTick(centerTick, true, centerTickColor);
Drawable background = ContextCompat.getDrawable(context, R.drawable.seekbar_background);
SuntimesUtils.tintDrawable(background, trackColor, trackColor, 0);
Drawable secondaryProgress = new ColorDrawable(Color.TRANSPARENT);
Drawable primaryProgress = new ScaleDrawable(new ColorDrawable(Color.TRANSPARENT), Gravity.START, 1, -1);
LayerDrawable progressDrawable = new LayerDrawable(new Drawable[] { background, secondaryProgress, primaryProgress });
progressDrawable.setId(0, android.R.id.background);
progressDrawable.setId(1, android.R.id.secondaryProgress);
progressDrawable.setId(2, android.R.id.progress);
Rect bounds = getProgressDrawable().getBounds();
setProgressDrawable(progressDrawable);
getProgressDrawable().setBounds(bounds);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
// test transition
View v = findViewById(R.id.test_transition);
TransitionDrawable drawable = (TransitionDrawable) v.getBackground();
drawable.startTransition(1000);
// test scale
View testScale = findViewById(R.id.test_scale);
ScaleDrawable testScaleDrawable = (ScaleDrawable) testScale.getBackground();
testScaleDrawable.setLevel(10);
// test clip
ImageView testClip = (ImageView) findViewById(R.id.test_clip);
ClipDrawable testClipDrawable = (ClipDrawable) testClip.getDrawable();
testClipDrawable.setLevel(8000);
// test custom drawable
View testCustomDrawable = findViewById(R.id.test_custom_drawable);
CustomDrawable customDrawable = new CustomDrawable(Color.parseColor("#0ac39e"));
testCustomDrawable.setBackgroundDrawable(customDrawable);
}
}
@Override
public Drawable parse(ParseRequest request) {
final Context ctx = request.context();
final AttributeSet attrs = request.attrs();
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.Folivora_Scale);
ScaleDrawable sd = new ScaleDrawable(
getDrawable(ctx, a, attrs, R.styleable.Folivora_Scale_scaleDrawable),
a.getInt(R.styleable.Folivora_Scale_scaleGravity, Gravity.START),
a.getFloat(R.styleable.Folivora_Scale_scaleWidth, -1F),
a.getFloat(R.styleable.Folivora_Scale_scaleHeight, -1F)
);
sd.setLevel(a.getInt(R.styleable.Folivora_Scale_scaleLevel, 1));
a.recycle();
return sd;
}
private void setupDrawables() {
parts = new ArrayList<>();
parts.add(ContextCompat.getDrawable(getContext(), R.drawable.carriage_part1));
parts.add(ContextCompat.getDrawable(getContext(), R.drawable.carriage_part2));
parts.add(ContextCompat.getDrawable(getContext(), R.drawable.carriage_part3));
carriageOffset = (int) getContext().getResources().getDimension(R.dimen.carriage_offset);
pageOffset = (int) getContext().getResources().getDimension(R.dimen.page_offset);
offset = (int) getContext().getResources().getDimension(R.dimen.offset);
button = ContextCompat.getDrawable(getContext(), R.drawable.button);
buttonPressed = ContextCompat.getDrawable(getContext(), R.drawable.button_pressed);
page = new ScaleDrawable(ContextCompat.getDrawable(getContext(), R.drawable.page),
TOP, -1, 1);
page.setLevel(10000);
pageBack =
new ScaleDrawable(ContextCompat.getDrawable(getContext(), R.drawable.page_revers),
TOP, -1, 1);
pageBack.setLevel(0);
keyboard = ContextCompat.getDrawable(getContext(), R.drawable.keyboard_bg);
typewriter = ContextCompat.getDrawable(getContext(), R.drawable.machine);
space = ContextCompat.getDrawable(getContext(), R.drawable.space);
spacePressed = ContextCompat.getDrawable(getContext(), R.drawable.space_pressed);
letter = ContextCompat.getDrawable(getContext(), R.drawable.letter);
}
public void setEventsIcon(@DrawableRes int drawableRes) {
Drawable drawable = ContextCompat.getDrawable(getContext(), drawableRes);
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, width / 2, height / 2);
ScaleDrawable sd = new ScaleDrawable(drawable, Gravity.CENTER, 0.6f, 0.6f);
sd.setLevel(8000);
ViewHelper.tintDrawable(drawable, ViewHelper.getTertiaryTextColor(getContext()));
setCompoundDrawablesWithIntrinsicBounds(sd, null, null, null);
}
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 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();
}
}
/**
* Some drawable implementations have problems with mutation. This method returns false if
* there is a known issue in the given drawable's implementation.
*/
public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
if (Build.VERSION.SDK_INT < 15 && drawable instanceof InsetDrawable) {
return false;
} else if (Build.VERSION.SDK_INT < 15 && drawable instanceof GradientDrawable) {
// GradientDrawable has a bug pre-ICS which results in mutate() resulting
// in loss of color
return false;
} else if (Build.VERSION.SDK_INT < 17 && drawable instanceof LayerDrawable) {
return false;
}
if (drawable instanceof DrawableContainer) {
// If we have a DrawableContainer, let's traverse it's child array
final Drawable.ConstantState state = drawable.getConstantState();
if (state instanceof DrawableContainer.DrawableContainerState) {
final DrawableContainer.DrawableContainerState containerState =
(DrawableContainer.DrawableContainerState) state;
for (final Drawable child : containerState.getChildren()) {
if (!canSafelyMutateDrawable(child)) {
return false;
}
}
}
} else if (SkinCompatVersionUtils.isV4DrawableWrapper(drawable)) {
return canSafelyMutateDrawable(SkinCompatVersionUtils.getV4DrawableWrapperWrappedDrawable(drawable));
} else if (SkinCompatVersionUtils.isV4WrappedDrawable(drawable)) {
return canSafelyMutateDrawable(SkinCompatVersionUtils.getV4WrappedDrawableWrappedDrawable(drawable));
} else if (SkinCompatVersionUtils.isV7DrawableWrapper(drawable)) {
return canSafelyMutateDrawable(SkinCompatVersionUtils.getV7DrawableWrapperWrappedDrawable(drawable));
} else if (drawable instanceof ScaleDrawable) {
Drawable scaleDrawable = ((ScaleDrawable) drawable).getDrawable();
if (scaleDrawable != null) {
return canSafelyMutateDrawable(scaleDrawable);
}
}
return true;
}
/**
* Some drawable implementations have problems with mutation. This method returns false if
* there is a known issue in the given drawable's implementation.
*/
public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
if (Build.VERSION.SDK_INT < 15 && drawable instanceof InsetDrawable) {
return false;
} else if (Build.VERSION.SDK_INT < 15 && drawable instanceof GradientDrawable) {
// GradientDrawable has a bug pre-ICS which results in mutate() resulting
// in loss of color
return false;
} else if (Build.VERSION.SDK_INT < 17 && drawable instanceof LayerDrawable) {
return false;
}
if (drawable instanceof DrawableContainer) {
// If we have a DrawableContainer, let's traverse it's child array
final Drawable.ConstantState state = drawable.getConstantState();
if (state instanceof DrawableContainer.DrawableContainerState) {
final DrawableContainer.DrawableContainerState containerState =
(DrawableContainer.DrawableContainerState) state;
for (final Drawable child : containerState.getChildren()) {
if (!canSafelyMutateDrawable(child)) {
return false;
}
}
}
} else if (SkinCompatVersionUtils.isV4DrawableWrapper(drawable)) {
return canSafelyMutateDrawable(SkinCompatVersionUtils.getV4DrawableWrapperWrappedDrawable(drawable));
} else if (SkinCompatVersionUtils.isV4WrappedDrawable(drawable)) {
return canSafelyMutateDrawable(SkinCompatVersionUtils.getV4WrappedDrawableWrappedDrawable(drawable));
} else if (drawable instanceof DrawableWrapper) {
return canSafelyMutateDrawable(((DrawableWrapper) drawable).getWrappedDrawable());
} else if (drawable instanceof ScaleDrawable) {
return canSafelyMutateDrawable(((ScaleDrawable) drawable).getDrawable());
}
return true;
}
/***
* Method called from APIs below 21 to setup Progress Color
*/
private void setupProgressColor() {
//load up the drawable and apply color
LayerDrawable ld = (LayerDrawable) getProgressDrawable();
ScaleDrawable shape = (ScaleDrawable) (ld.findDrawableByLayerId(android.R.id.progress));
shape.setColorFilter(mProgressColor, PorterDuff.Mode.SRC_IN);
//set the background to transparent
NinePatchDrawable ninePatchDrawable = (NinePatchDrawable) (ld.findDrawableByLayerId(android.R.id.background));
ninePatchDrawable.setColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_IN);
}
@Override
public void setEnabled(final boolean enabled) {
mIsEnabled = enabled;
triggerMethodOnceViewIsDisplayed(this, new Callable<Void>() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public Void call() throws Exception {
if (!lollipopAndAbove()) {
gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.OVAL);
gradientDrawable.setSize(mOriginalThumbHeight / 3, mOriginalThumbHeight / 3);
gradientDrawable.setColor(mIsEnabled ? mThumbColor : Color.LTGRAY);
gradientDrawable.setDither(true);
gradientDrawable.setAlpha(mThumbAlpha);
setThumb(gradientDrawable);
//load up the drawable and apply color
LayerDrawable ld = (LayerDrawable) getProgressDrawable();
ScaleDrawable shape = (ScaleDrawable) (ld.findDrawableByLayerId(android.R.id.progress));
shape.setColorFilter(mIsEnabled ? mProgressColor : Color.LTGRAY, PorterDuff.Mode.SRC_IN);
//set the background to transparent
NinePatchDrawable ninePatchDrawable = (NinePatchDrawable) (ld.findDrawableByLayerId(android.R.id.background));
ninePatchDrawable.setColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_IN);
//background
//load up the drawable and apply color
SeekBarBackgroundDrawable seekBarBackgroundDrawable = new SeekBarBackgroundDrawable(getContext(),
mIsEnabled ? mProgressBackgroundColor : Color.LTGRAY, mActualBackgroundColor, getPaddingLeft(), getPaddingRight());
if (belowJellybean())
setBackgroundDrawable(seekBarBackgroundDrawable);
else
setBackground(seekBarBackgroundDrawable);
}
SeekBarCompat.super.setEnabled(enabled);
return null;
}
});
}
private Drawable tileifyProgressDrawable(Drawable wrapped) {
if (wrapped instanceof LayerDrawable) {
LayerDrawable drawable = (LayerDrawable) wrapped;
final int N = drawable.getNumberOfLayers();
Drawable[] outDrawables = new Drawable[N];
for (int i = 0; i < N; i++) {
final int id = drawable.getId(i);
Drawable childDrawable = drawable.getDrawable(i);
if (id == android.R.id.background) {
outDrawables[i] = new NumberBGDrawable(childDrawable);
} else if (id == android.R.id.progress) {
if (childDrawable instanceof ScaleDrawable) {
outDrawables[i] = tileifyScaleDrawable((ScaleDrawable) childDrawable);
} else if (childDrawable instanceof ClipDrawable) {
outDrawables[i] = tileifyClipDrawable((ClipDrawable) childDrawable);
} else {
outDrawables[i] = childDrawable;
}
} else {
outDrawables[i] = childDrawable;
}
}
LayerDrawable newDrawable = new NumberLayerDrawable(outDrawables);
return newDrawable;
}
return wrapped;
}
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));
}
}
}
}
}
}
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));
}
}
}