代码之家  ›  专栏  ›  技术社区  ›  BekaKK

创建半圆背景,可使用透明

  •  0
  • BekaKK  · 技术社区  · 7 年前

    enter image description here

    我写了代码,几乎可以正常工作

    public class GetsugaDrawable extends Drawable {
    
    private final Context context;
    private final float radiusScale = 1.2f;
    private final float yOffset = 0.3f;
    
    private final int colorLower = Color.RED;
    private final int colorUpper = Color.BLACK;
    
    private final Paint upperPaint = new Paint();
    
    public GetsugaDrawable(Context c) {
        context = c;
        upperPaint.setColor(colorUpper);
        upperPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    }
    
    @Override
    public void draw(@NonNull Canvas canvas) {
        final Rect bounds = new Rect(getBounds());
        canvas.drawColor(colorLower, PorterDuff.Mode.SRC);
        final float radius = radiusScale * bounds.height();
        final int x = bounds.centerX();
        final float y = (bounds.centerY() - bounds.height() * yOffset) - radius;
        canvas.drawCircle(x, y, radius, upperPaint);
    }
    
    @Override
    public void setAlpha(int alpha) {
        // ignored TODO impl.
    }
    
    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        // ignored TODO impl.
    }
    
    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
    }
    

    enter image description here

    我试着改变

        private final int colorUpper = Color.BLACK;
    

    具有

        private final int colorUpper = Color.TRANSPARENT;
    

    enter image description here

    我做错了什么?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Ravi Kant    7 年前

    添加upperPaint。setXfermode(新的PorterDuffXfermode(PorterDuff.Mode.CLEAR));

    public class GetsugaDrawable extends Drawable {
    private final Context context;
    private final float radiusScale = 1.2f;
    private final float yOffset = 0.3f;
    
    private final int colorLower = Color.RED;
    private final int colorUpper = Color.BLACK;
    
    private final Paint upperPaint = new Paint();
    
    public GetsugaDrawable(Context c) {
        context = c;
        upperPaint.setAntiAlias(true);
        upperPaint.setColor(colorUpper);
        upperPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));      
    }
    
    @Override
    public void draw(@NonNull Canvas canvas) {
        final Rect bounds = new Rect(getBounds());
        canvas.drawColor(colorLower);
        final float radius = radiusScale * bounds.height();
        final int x = bounds.centerX();
        final float y = (bounds.centerY() - bounds.height() * yOffset) - radius;
        canvas.drawCircle(x, y, radius, upperPaint);
    }
    
    @Override
    public void setAlpha(int alpha) {
    
    }
    
    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
    
    }
    
    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }}
    

    不要忘记观看。setLayerType(View.LAYER\u TYPE\u HARDWARE,null);在目标视图上

        2
  •  0
  •   amuyu    7 年前

    你可以改变 draw 作用

    @Override
    public void draw(@NonNull Canvas canvas) {
        final Rect bounds = new Rect(getBounds());
        final float radius = radiusScale * bounds.height();
        final int x = bounds.centerX();
        final float y = (bounds.centerY() - bounds.height() * yOffset) - radius;
        Path path = new Path();
        path.addCircle(x, y, radius, CW);
        canvas.clipPath(path, Region.Op.DIFFERENCE);
        canvas.drawColor(colorLower);
    }