我正在创建自己的浮动操作按钮。为此,我使用的是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);
}
}
告诉我如何使影子清晰正确,就像支持库中给出的浮动操作按钮一样。
答案 0 :(得分:0)
您可以在GitHub查看此fab库here。该库允许您在工厂设置涟漪效应颜色。
<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" />