use of android.graphics.drawable.LayerDrawable in project android_frameworks_base by AOSPA.
the class BatteryMeterDrawable method checkBatteryMeterDrawableValid.
private void checkBatteryMeterDrawableValid(Resources res, int style) {
final int resId = getBatteryDrawableResourceForStyle(style);
final Drawable batteryDrawable;
try {
batteryDrawable = mContext.getDrawable(resId);
} catch (Resources.NotFoundException e) {
throw new BatteryMeterDrawableException(res.getResourceName(resId) + " is an " + "invalid drawable", e);
}
// Check that the drawable is a LayerDrawable
if (!(batteryDrawable instanceof LayerDrawable)) {
throw new BatteryMeterDrawableException("Expected a LayerDrawable but received a " + batteryDrawable.getClass().getSimpleName());
}
final LayerDrawable layerDrawable = (LayerDrawable) batteryDrawable;
final Drawable frame = layerDrawable.findDrawableByLayerId(R.id.battery_frame);
final Drawable level = layerDrawable.findDrawableByLayerId(R.id.battery_fill);
// Now, check that the required layers exist and are of the correct type
if (frame == null) {
throw new BatteryMeterDrawableException("Missing battery_frame drawble");
}
if (level != null) {
// Check that the level drawable is an AnimatedVectorDrawable
if (!(level instanceof AnimatedVectorDrawable)) {
throw new BatteryMeterDrawableException("Expected a AnimatedVectorDrawable " + "but received a " + level.getClass().getSimpleName());
}
// Make sure we can stop-motion animate the level drawable
try {
StopMotionVectorDrawable smvd = new StopMotionVectorDrawable(level);
smvd.setCurrentFraction(0.5f);
} catch (Exception e) {
throw new BatteryMeterDrawableException("Unable to perform stop motion on " + "battery_fill drawable", e);
}
} else {
throw new BatteryMeterDrawableException("Missing battery_fill drawable");
}
}
use of android.graphics.drawable.LayerDrawable in project android_frameworks_base by crdroidandroid.
the class ProgressBar method tileify.
/**
* Converts a drawable to a tiled version of itself. It will recursively
* traverse layer and state list drawables.
*/
private Drawable tileify(Drawable drawable, boolean clip) {
if (drawable instanceof LayerDrawable) {
final LayerDrawable orig = (LayerDrawable) drawable;
final int N = orig.getNumberOfLayers();
final Drawable[] outDrawables = new Drawable[N];
for (int i = 0; i < N; i++) {
final int id = orig.getId(i);
outDrawables[i] = tileify(orig.getDrawable(i), (id == R.id.progress || id == R.id.secondaryProgress));
}
final LayerDrawable clone = new LayerDrawable(outDrawables);
for (int i = 0; i < N; i++) {
clone.setId(i, orig.getId(i));
clone.setLayerGravity(i, orig.getLayerGravity(i));
clone.setLayerWidth(i, orig.getLayerWidth(i));
clone.setLayerHeight(i, orig.getLayerHeight(i));
clone.setLayerInsetLeft(i, orig.getLayerInsetLeft(i));
clone.setLayerInsetRight(i, orig.getLayerInsetRight(i));
clone.setLayerInsetTop(i, orig.getLayerInsetTop(i));
clone.setLayerInsetBottom(i, orig.getLayerInsetBottom(i));
clone.setLayerInsetStart(i, orig.getLayerInsetStart(i));
clone.setLayerInsetEnd(i, orig.getLayerInsetEnd(i));
}
return clone;
}
if (drawable instanceof StateListDrawable) {
final StateListDrawable in = (StateListDrawable) drawable;
final StateListDrawable out = new StateListDrawable();
final int N = in.getStateCount();
for (int i = 0; i < N; i++) {
out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
}
return out;
}
if (drawable instanceof BitmapDrawable) {
final Drawable.ConstantState cs = drawable.getConstantState();
final BitmapDrawable clone = (BitmapDrawable) cs.newDrawable(getResources());
clone.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
if (mSampleWidth <= 0) {
mSampleWidth = clone.getIntrinsicWidth();
}
if (clip) {
return new ClipDrawable(clone, Gravity.LEFT, ClipDrawable.HORIZONTAL);
} else {
return clone;
}
}
return drawable;
}
use of android.graphics.drawable.LayerDrawable in project JustAndroid by chinaltz.
the class SegmentedGroup method updateBackground.
private void updateBackground(View view) {
if (!isInEditMode()) {
int checked = mLayoutSelector.getSelected();
int unchecked = mLayoutSelector.getUnselected();
//Set text color
ColorStateList colorStateList = new ColorStateList(new int[][] { { -android.R.attr.state_checked }, { android.R.attr.state_checked } }, new int[] { mTintColor, mCheckedTextColor });
((Button) view).setTextColor(colorStateList);
//Redraw with tint color
Drawable checkedDrawable = resources.getDrawable(checked).mutate();
Drawable uncheckedDrawable = resources.getDrawable(unchecked).mutate();
((GradientDrawable) checkedDrawable).setColor(mTintColor);
((GradientDrawable) checkedDrawable).setStroke(mMarginDp, mTintColor);
((GradientDrawable) uncheckedDrawable).setStroke(mMarginDp, mTintColor);
((GradientDrawable) uncheckedDrawable).setColor(mUnCheckedTintColor);
//Set proper radius
((GradientDrawable) checkedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));
((GradientDrawable) uncheckedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));
GradientDrawable maskDrawable = (GradientDrawable) resources.getDrawable(unchecked).mutate();
maskDrawable.setStroke(mMarginDp, mTintColor);
maskDrawable.setColor(mUnCheckedTintColor);
maskDrawable.setCornerRadii(mLayoutSelector.getChildRadii(view));
int maskColor = Color.argb(50, Color.red(mTintColor), Color.green(mTintColor), Color.blue(mTintColor));
maskDrawable.setColor(maskColor);
LayerDrawable pressedDrawable = new LayerDrawable(new Drawable[] { uncheckedDrawable, maskDrawable });
Drawable[] drawables = { uncheckedDrawable, checkedDrawable };
TransitionDrawable transitionDrawable = new TransitionDrawable(drawables);
if (((RadioButton) view).isChecked()) {
transitionDrawable.reverseTransition(0);
}
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] { -android.R.attr.state_checked, android.R.attr.state_pressed }, pressedDrawable);
stateListDrawable.addState(StateSet.WILD_CARD, transitionDrawable);
mDrawableMap.put(view.getId(), transitionDrawable);
//Set button background
if (Build.VERSION.SDK_INT >= 16) {
view.setBackground(stateListDrawable);
} else {
view.setBackgroundDrawable(stateListDrawable);
}
super.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
TransitionDrawable current = mDrawableMap.get(checkedId);
current.reverseTransition(200);
if (mLastCheckId != 0) {
TransitionDrawable last = mDrawableMap.get(mLastCheckId);
if (last != null)
last.reverseTransition(200);
}
mLastCheckId = checkedId;
if (mCheckedChangeListener != null) {
mCheckedChangeListener.onCheckedChanged(group, checkedId);
}
}
});
}
}
use of android.graphics.drawable.LayerDrawable in project little-bear-dictionary by daimajia.
the class ProgressBar method tileify.
private Drawable tileify(Drawable drawable, boolean clip) {
if (drawable instanceof LayerDrawable) {
LayerDrawable background = (LayerDrawable) drawable;
final int N = background.getNumberOfLayers();
Drawable[] outDrawables = new Drawable[N];
for (int i = 0; i < N; i++) {
int id = background.getId(i);
outDrawables[i] = tileify(background.getDrawable(i), id == R.id.progress || id == R.id.secondaryProgress);
}
LayerDrawable newBg = new LayerDrawable(outDrawables);
for (int i = 0; i < N; i++) {
newBg.setId(i, background.getId(i));
}
return newBg;
} else if (drawable instanceof StateListDrawable) {
StateListDrawable in = (StateListDrawable) drawable;
StateListDrawable out = new StateListDrawable();
int numStates = ReflectHelper.invoke(in, "getStateCount", int.class);
for (int i = 0; i < numStates; i++) {
out.addState(ReflectHelper.invoke(in, "getStateSet", int[].class, i), tileify(ReflectHelper.invoke(in, "getStateDrawable", Drawable.class, i), clip));
}
return out;
} else if (drawable instanceof BitmapDrawable) {
final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
if (mSampleTile == null) {
mSampleTile = tileBitmap;
}
final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
shapeDrawable.getPaint().setShader(bitmapShader);
return clip ? new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL) : shapeDrawable;
}
return drawable;
}
use of android.graphics.drawable.LayerDrawable in project little-bear-dictionary by daimajia.
the class ProgressBar method doRefreshProgress.
private synchronized void doRefreshProgress(int id, int progress, boolean fromUser, boolean callBackToApp) {
float scale = mMax > 0 ? (float) progress / (float) mMax : 0;
final Drawable d = mCurrentDrawable;
if (d != null) {
Drawable progressDrawable = null;
if (d instanceof LayerDrawable) {
progressDrawable = ((LayerDrawable) d).findDrawableByLayerId(id);
}
final int level = (int) (scale * ProgressBar.MAX_LEVEL);
(progressDrawable != null ? progressDrawable : d).setLevel(level);
} else {
invalidate();
}
if (callBackToApp && id == R.id.progress) {
onProgressRefresh(scale, fromUser);
}
}
Aggregations