浮动操作按钮的阴影
Drop shadow for Floating Action Button
我正在创建自己的浮动操作按钮。为此,我使用了 RippleDrawable(由我创建)和 ShadowDrawable(也是由我创建)。我面临的问题是我的 ShadowDrawbale 没有为相应的按钮投射完美的阴影。我的ShadowDrawable的代码如下:
public class ShadowDrawable extends Drawable {
private Context context;
private float floatMaximumRadius = 0;
private int intCenterX = 0, intCenterY = 0, intShadowAlpha,intShadowColor;
private Paint paintShadow;
public ShadowDrawable(Context context) {
this.context = context;
paintShadow = new Paint(Paint.ANTI_ALIAS_FLAG);
paintShadow.setStyle(Paint.Style.FILL);
}
@Override
public void draw(Canvas canvas) {
paintShadow.setShadowLayer(dpToPx(4f), dpToPx(1f), dpToPx(3f), intShadowColor);
paintShadow.setAlpha(intShadowAlpha);
paintShadow.setColor(intShadowColor);
canvas.drawCircle(intCenterX, intCenterY, floatMaximumRadius, paintShadow);
}
@Override
public int getOpacity() {
return 0;
}
@Override
protected void onBoundsChange(Rect rectBounds) {
super.onBoundsChange(rectBounds);
intCenterX = rectBounds.centerX();
intCenterY = rectBounds.centerY();
floatMaximumRadius = (Math.min(rectBounds.width(), rectBounds.height()) / 2) - 4;
}
@Override
public void setAlpha(int alpha) {
setShadowAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
}
public void setShadowAlpha(int intShadowAlpha) {
this.intShadowAlpha = intShadowAlpha;
invalidateSelf();
}
public void setShadowColor(int intShadowColor) {
this.intShadowColor = intShadowColor;
invalidateSelf();
}
private int dpToPx(float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return Math.round(dp * scale);
}
}
告诉我如何使阴影像支持库中提供的浮动操作按钮一样清晰和适当。
您可以在 here GitHub 处查看这个 fab 库。该库允许您在 fab 上设置波纹效果颜色。
<com.melnykov.fab.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="@drawable/ic_action_content_new"
fab:fab_colorNormal="@color/primary"
fab:fab_colorPressed="@color/primary_pressed"
fab:fab_colorRipple="@color/ripple" />
我正在创建自己的浮动操作按钮。为此,我使用了 RippleDrawable(由我创建)和 ShadowDrawable(也是由我创建)。我面临的问题是我的 ShadowDrawbale 没有为相应的按钮投射完美的阴影。我的ShadowDrawable的代码如下:
public class ShadowDrawable extends Drawable {
private Context context;
private float floatMaximumRadius = 0;
private int intCenterX = 0, intCenterY = 0, intShadowAlpha,intShadowColor;
private Paint paintShadow;
public ShadowDrawable(Context context) {
this.context = context;
paintShadow = new Paint(Paint.ANTI_ALIAS_FLAG);
paintShadow.setStyle(Paint.Style.FILL);
}
@Override
public void draw(Canvas canvas) {
paintShadow.setShadowLayer(dpToPx(4f), dpToPx(1f), dpToPx(3f), intShadowColor);
paintShadow.setAlpha(intShadowAlpha);
paintShadow.setColor(intShadowColor);
canvas.drawCircle(intCenterX, intCenterY, floatMaximumRadius, paintShadow);
}
@Override
public int getOpacity() {
return 0;
}
@Override
protected void onBoundsChange(Rect rectBounds) {
super.onBoundsChange(rectBounds);
intCenterX = rectBounds.centerX();
intCenterY = rectBounds.centerY();
floatMaximumRadius = (Math.min(rectBounds.width(), rectBounds.height()) / 2) - 4;
}
@Override
public void setAlpha(int alpha) {
setShadowAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
}
public void setShadowAlpha(int intShadowAlpha) {
this.intShadowAlpha = intShadowAlpha;
invalidateSelf();
}
public void setShadowColor(int intShadowColor) {
this.intShadowColor = intShadowColor;
invalidateSelf();
}
private int dpToPx(float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return Math.round(dp * scale);
}
}
告诉我如何使阴影像支持库中提供的浮动操作按钮一样清晰和适当。
您可以在 here GitHub 处查看这个 fab 库。该库允许您在 fab 上设置波纹效果颜色。
<com.melnykov.fab.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="@drawable/ic_action_content_new"
fab:fab_colorNormal="@color/primary"
fab:fab_colorPressed="@color/primary_pressed"
fab:fab_colorRipple="@color/ripple" />