下面列出了android.graphics.drawable.LayerDrawable#setLayerInset() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Drawable create(Context context) {
final GradientDrawable bubble = new GradientDrawable();
final int radius = context.getResources().getDimensionPixelSize(R.dimen.message_bubble_corner_radius);
final float[] radii = cornerBooleansToRadii(corners, radius);
bubble.setColor(color);
bubble.setCornerRadii(radii);
if (!hasShadow) {
return bubble;
} else {
final GradientDrawable shadow = new GradientDrawable();
final int distance = context.getResources().getDimensionPixelSize(R.dimen.message_bubble_shadow_distance);
shadow.setColor(shadowColor);
shadow.setCornerRadii(radii);
final LayerDrawable layers = new LayerDrawable(new Drawable[]{shadow, bubble});
layers.setLayerInset(1, 0, 0, 0, distance);
return layers;
}
}
protected LayerDrawable createCommonFace() {
if (resIcon != -1) {
LayerDrawable ly = afterProcessLayerDrawable(new LayerDrawable(
new Drawable[]{
createFillDrawable(),
getArrow(),
getIcon()
}));
ly.setLayerInset(2, icon_size_text_padding, 0, 0, 0);
return ly;
} else {
return afterProcessLayerDrawable(new LayerDrawable(
new Drawable[]{
createFillDrawable(),
getArrow()
}));
}
}
void updateBackground() {
float circleLeft = mShadowRadius;
float circleTop = mShadowRadius - mShadowOffset;
final RectF circleRect = new RectF(circleLeft, circleTop, circleLeft + mCircleSize, circleTop + mCircleSize);
LayerDrawable layerDrawable = new LayerDrawable(
new Drawable[] {
getResources().getDrawable(mSize == SIZE_NORMAL ? R.drawable.floating_action_button_fab_bg_normal : R.drawable.floating_action_button_fab_bg_mini),
createFillDrawable(circleRect),
createStrokesDrawable(circleRect),
getIconDrawable()
});
float iconOffset = (mCircleSize - getDimension(R.dimen.fab_icon_size)) / 2f;
int iconInsetHorizontal = (int) (mShadowRadius + iconOffset);
int iconInsetTop = (int) (circleTop + iconOffset);
int iconInsetBottom = (int) (mShadowRadius + mShadowOffset + iconOffset);
layerDrawable.setLayerInset(3, iconInsetHorizontal, iconInsetTop, iconInsetHorizontal, iconInsetBottom);
setBackgroundCompat(layerDrawable);
}
private Drawable createDrawable(int color) {
OvalShape ovalShape = new OvalShape();
ShapeDrawable shapeDrawable = new ShapeDrawable(ovalShape);
shapeDrawable.getPaint().setColor(color);
if (mShadow) {
LayerDrawable layerDrawable = new LayerDrawable(
new Drawable[]{getResources().getDrawable(R.drawable.floating_acition_button_shadow),
shapeDrawable});
int shadowSize = getDimension(
mType == TYPE_NORMAL ? R.dimen.fab_shadow_size : R.dimen.fab_mini_shadow_size);
layerDrawable.setLayerInset(1, shadowSize, shadowSize, shadowSize, shadowSize);
return layerDrawable;
} else {
return shapeDrawable;
}
}
void updateBackground() {
float circleLeft = mShadowRadius;
float circleTop = mShadowRadius - mShadowOffset;
final RectF circleRect = new RectF(circleLeft, circleTop, circleLeft + mCircleSize, circleTop + mCircleSize);
LayerDrawable layerDrawable = new LayerDrawable(
new Drawable[] {
new BitmapDrawable(getResources()),
createFillDrawable(circleRect),
new BitmapDrawable(getResources()),
getIconDrawable()
});
float iconOffset = (mCircleSize - getDimension(R.dimen.fab_icon_size)) / 2f;
int iconInsetHorizontal = (int) (mShadowRadius + iconOffset);
int iconInsetTop = (int) (circleTop + iconOffset);
int iconInsetBottom = (int) (mShadowRadius + mShadowOffset + iconOffset);
layerDrawable.setLayerInset(3, iconInsetHorizontal, iconInsetTop, iconInsetHorizontal, iconInsetBottom);
setBackgroundCompat(layerDrawable);
}
/**
* 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;
}
private LayerDrawable createDrawable(int radius, int topColor, int bottomColor) {
float[] outerRadius = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
//Top
RoundRectShape topRoundRect = new RoundRectShape(outerRadius, null, null);
ShapeDrawable topShapeDrawable = new ShapeDrawable(topRoundRect);
topShapeDrawable.getPaint().setColor(topColor);
//Bottom
RoundRectShape roundRectShape = new RoundRectShape(outerRadius, null, null);
ShapeDrawable bottomShapeDrawable = new ShapeDrawable(roundRectShape);
bottomShapeDrawable.getPaint().setColor(bottomColor);
//Create array
Drawable[] drawArray = {bottomShapeDrawable, topShapeDrawable};
LayerDrawable layerDrawable = new LayerDrawable(drawArray);
//Set shadow height
if (isShadowEnabled && topColor != Color.TRANSPARENT) {
//unpressed drawable
layerDrawable.setLayerInset(0, 0, 0, 0, 0); /*index, left, top, right, bottom*/
} else {
//pressed drawable
layerDrawable.setLayerInset(0, 0, mShadowHeight, 0, 0); /*index, left, top, right, bottom*/
}
layerDrawable.setLayerInset(1, 0, 0, 0, mShadowHeight); /*index, left, top, right, bottom*/
return layerDrawable;
}
private LayerDrawable makeClusterBackground() {
mColoredCircleBackground = new ShapeDrawable(new OvalShape());
ShapeDrawable outline = new ShapeDrawable(new OvalShape());
outline.getPaint().setColor(0x80ffffff); // Transparent white.
LayerDrawable background = new LayerDrawable(new Drawable[]{outline, mColoredCircleBackground});
int strokeWidth = (int) (mDensity * 3);
background.setLayerInset(1, strokeWidth, strokeWidth, strokeWidth, strokeWidth);
return background;
}
void updateBackground() {
final float strokeWidth = getDimension(R.dimen.fab_stroke_width);
final float halfStrokeWidth = strokeWidth / 2f;
LayerDrawable layerDrawable = new LayerDrawable(
new Drawable[] {
getResources().getDrawable(mSize == SIZE_NORMAL ? R.drawable.fab_bg_normal : R.drawable.fab_bg_mini),
createFillDrawable(strokeWidth),
createOuterStrokeDrawable(strokeWidth),
getIconDrawable()
});
int iconOffset = (int) (mCircleSize - getDimension(R.dimen.fab_icon_size)) / 2;
int circleInsetHorizontal = (int) (mShadowRadius);
int circleInsetTop = (int) (mShadowRadius - mShadowOffset);
int circleInsetBottom = (int) (mShadowRadius + mShadowOffset);
layerDrawable.setLayerInset(1,
circleInsetHorizontal,
circleInsetTop,
circleInsetHorizontal,
circleInsetBottom);
layerDrawable.setLayerInset(2,
(int) (circleInsetHorizontal - halfStrokeWidth),
(int) (circleInsetTop - halfStrokeWidth),
(int) (circleInsetHorizontal - halfStrokeWidth),
(int) (circleInsetBottom - halfStrokeWidth));
layerDrawable.setLayerInset(3,
circleInsetHorizontal + iconOffset,
circleInsetTop + iconOffset,
circleInsetHorizontal + iconOffset,
circleInsetBottom + iconOffset);
setBackgroundCompat(layerDrawable);
}
/**
* 得到唱盘图片
* 唱盘图片由空心圆盘及音乐专辑图片“合成”得到
*/
public Drawable getDiscDrawable(Bitmap bitmap) {
int discSize = (int) (mScreenWidth * DisplayUtil.SCALE_DISC_SIZE);
int musicPicSize = (int) (mScreenWidth * DisplayUtil.SCALE_MUSIC_PIC_SIZE);
Bitmap bitmapDisc = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R
.drawable.ic_disc), discSize, discSize, false);
Bitmap bitmapMusicPic = Bitmap.createScaledBitmap(bitmap, musicPicSize, musicPicSize, true);
BitmapDrawable discDrawable = new BitmapDrawable(bitmapDisc);
RoundedBitmapDrawable roundMusicDrawable = RoundedBitmapDrawableFactory.create
(getResources(), bitmapMusicPic);
//抗锯齿
discDrawable.setAntiAlias(true);
roundMusicDrawable.setAntiAlias(true);
Drawable[] drawables = new Drawable[2];
drawables[0] = roundMusicDrawable;
drawables[1] = discDrawable;
LayerDrawable layerDrawable = new LayerDrawable(drawables);
int musicPicMargin = (int) ((DisplayUtil.SCALE_DISC_SIZE - DisplayUtil
.SCALE_MUSIC_PIC_SIZE) * mScreenWidth / 2);
//调整专辑图片的四周边距,让其显示在正中
layerDrawable.setLayerInset(0, musicPicMargin, musicPicMargin, musicPicMargin,
musicPicMargin);
return layerDrawable;
}
void updateBackground() {
final float strokeWidth = getDimension(R.dimen.fab_stroke_width);
final float halfStrokeWidth = strokeWidth / 2f;
LayerDrawable layerDrawable = new LayerDrawable(
new Drawable[] {
getResources().getDrawable(mSize == SIZE_NORMAL ? R.drawable.fab_bg_normal : R.drawable.fab_bg_mini),
createFillDrawable(strokeWidth),
createOuterStrokeDrawable(strokeWidth),
getIconDrawable()
});
int iconOffset = (int) (mCircleSize - getDimension(R.dimen.fab_icon_size)) / 2;
int circleInsetHorizontal = (int) (mShadowRadius);
int circleInsetTop = (int) (mShadowRadius - mShadowOffset);
int circleInsetBottom = (int) (mShadowRadius + mShadowOffset);
layerDrawable.setLayerInset(1,
circleInsetHorizontal,
circleInsetTop,
circleInsetHorizontal,
circleInsetBottom);
layerDrawable.setLayerInset(2,
(int) (circleInsetHorizontal - halfStrokeWidth),
(int) (circleInsetTop - halfStrokeWidth),
(int) (circleInsetHorizontal - halfStrokeWidth),
(int) (circleInsetBottom - halfStrokeWidth));
layerDrawable.setLayerInset(3,
circleInsetHorizontal + iconOffset,
circleInsetTop + iconOffset,
circleInsetHorizontal + iconOffset,
circleInsetBottom + iconOffset);
setBackgroundCompat(layerDrawable);
}
private Drawable getFileDrawable(int fileResource) {
Drawable firstLayer = mContext.getResources().getDrawable(fileResource);
LayerDrawable drawable = new LayerDrawable(new Drawable[]{
mContext.getResources().getDrawable(R.drawable.fplib_circle),
firstLayer
});
drawable.setLayerInset(1, (int) iconPadding, (int) iconPadding,
(int) iconPadding, (int) iconPadding);
return drawable;
}
void updateBackground() {
final float strokeWidth = getDimension(R.dimen.fab_stroke_width);
final float halfStrokeWidth = strokeWidth / 2f;
LayerDrawable layerDrawable = new LayerDrawable(
new Drawable[] {
getResources().getDrawable(mSize == SIZE_NORMAL ? R.drawable.fab_bg_normal : R.drawable.fab_bg_mini),
createFillDrawable(strokeWidth),
createOuterStrokeDrawable(strokeWidth),
getIconDrawable()
});
int iconOffset = (int) (mCircleSize - getDimension(R.dimen.fab_icon_size)) / 2;
int circleInsetHorizontal = (int) (mShadowRadius);
int circleInsetTop = (int) (mShadowRadius - mShadowOffset);
int circleInsetBottom = (int) (mShadowRadius + mShadowOffset);
layerDrawable.setLayerInset(1,
circleInsetHorizontal,
circleInsetTop,
circleInsetHorizontal,
circleInsetBottom);
layerDrawable.setLayerInset(2,
(int) (circleInsetHorizontal - halfStrokeWidth),
(int) (circleInsetTop - halfStrokeWidth),
(int) (circleInsetHorizontal - halfStrokeWidth),
(int) (circleInsetBottom - halfStrokeWidth));
layerDrawable.setLayerInset(3,
circleInsetHorizontal + iconOffset,
circleInsetTop + iconOffset,
circleInsetHorizontal + iconOffset,
circleInsetBottom + iconOffset);
setBackgroundCompat(layerDrawable);
}
private LayerDrawable createDrawable(int radius, int topColor, int bottomColor) {
float[] outerRadius = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
//Top
RoundRectShape topRoundRect = new RoundRectShape(outerRadius, null, null);
ShapeDrawable topShapeDrawable = new ShapeDrawable(topRoundRect);
topShapeDrawable.getPaint().setColor(topColor);
//Bottom
RoundRectShape roundRectShape = new RoundRectShape(outerRadius, null, null);
ShapeDrawable bottomShapeDrawable = new ShapeDrawable(roundRectShape);
bottomShapeDrawable.getPaint().setColor(bottomColor);
//Create array
Drawable[] drawArray = {bottomShapeDrawable, topShapeDrawable};
LayerDrawable layerDrawable = new LayerDrawable(drawArray);
//Set shadow height
if (isShadowEnabled && topColor != Color.TRANSPARENT) {
//unpressed drawable
layerDrawable.setLayerInset(0, 0, 0, 0, 0); /*index, left, top, right, bottom*/
} else {
//pressed drawable
layerDrawable.setLayerInset(0, 0, mShadowHeight, 0, 0); /*index, left, top, right, bottom*/
}
layerDrawable.setLayerInset(1, 0, 0, 0, mShadowHeight); /*index, left, top, right, bottom*/
return layerDrawable;
}
private LayerDrawable makeClusterBackground() {
mColoredCircleBackground = new ShapeDrawable(new OvalShape());
ShapeDrawable outline = new ShapeDrawable(new OvalShape());
outline.getPaint().setColor(0x80ffffff); // Transparent white.
LayerDrawable background = new LayerDrawable(new Drawable[]{outline, mColoredCircleBackground});
int strokeWidth = (int) (mDensity * 3);
background.setLayerInset(1, strokeWidth, strokeWidth, strokeWidth, strokeWidth);
return background;
}
void showBadge(@NonNull Drawable badge) {
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{iconDrawable, badge});
layerDrawable.setLayerInset(
1,
iconDrawable.getIntrinsicWidth(),
0,
0,
iconDrawable.getIntrinsicHeight()
);
icon.setImageDrawable(layerDrawable);
}
private Drawable getFileDrawable(int fileResource) {
Drawable firstLayer = context.getResources().getDrawable(fileResource);
LayerDrawable drawable = new LayerDrawable(new Drawable[]{
context.getResources().getDrawable(R.drawable.fplib_circle),
firstLayer
});
drawable.setLayerInset(1, (int) iconPadding, (int) iconPadding,
(int) iconPadding, (int) iconPadding);
return drawable;
}
private LayerDrawable getLayerList(boolean isPressed) {
float[] radii = new float[8];
for (int i = 0; i < radii.length; i++) {
radii[i] = corners;
}
ShapeDrawable shapeShadow = new ShapeDrawable(new RectShape());
shapeShadow.getPaint().setColor(shadowColor);
shapeShadow.setShape(new RoundRectShape(radii, null, null));
ShapeDrawable shapeBackground = new ShapeDrawable(new RectShape());
shapeBackground.getPaint().setColor(backgroundColor);
shapeBackground.setShape(new RoundRectShape(radii, null, null));
LayerDrawable composite = new LayerDrawable(new Drawable[]{shapeShadow, shapeBackground});
if (isPressed) {
composite.setLayerInset(0, 0, (int) (normalHeight - pressedHeight), 0, 0);
composite.setLayerInset(1, 0, (int) (normalHeight - pressedHeight), 0, (int) pressedHeight);
} else {
composite.setLayerInset(0, 0, 0, 0, 0);
composite.setLayerInset(1, 0, 0, 0, (int) normalHeight);
}
return composite;
}
public Drawable getSingleDrawable(LayerDrawable layerDrawable) {
int resourceBitmapHeight = layerDrawable.getMinimumHeight(), resourceBitmapWidth = layerDrawable
.getMinimumWidth();
float widthInInches = 0.2f;
int widthInPixels = (int) (widthInInches * mContext.getResources()
.getDisplayMetrics().densityDpi);
int heightInPixels = (int) (widthInPixels * resourceBitmapHeight / resourceBitmapWidth);
int insetLeft = 10, insetTop = 10, insetRight = 10, insetBottom = 10;
layerDrawable.setLayerInset(1, insetLeft, insetTop, insetRight,
insetBottom);
Bitmap bitmap = Bitmap.createBitmap(widthInPixels, heightInPixels,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
layerDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
layerDrawable.draw(canvas);
BitmapDrawable bitmapDrawable = new BitmapDrawable(mContext.getResources(),
bitmap);
bitmapDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
return bitmapDrawable;
}
private Drawable getFileDrawable(int fileResource) {
Drawable firstLayer = mContext.getResources().getDrawable(fileResource);
LayerDrawable drawable = new LayerDrawable(new Drawable[]{
mContext.getResources().getDrawable(R.drawable.fplib_circle),
firstLayer
});
drawable.setLayerInset(1, (int) iconPadding, (int) iconPadding,
(int) iconPadding, (int) iconPadding);
return drawable;
}