Android custom shape

后端 未结 4 538
小鲜肉
小鲜肉 2021-01-14 11:04

I know it is possible to make a shape looking something like this: \"enter

But I don\'

4条回答
  •  灰色年华
    2021-01-14 12:02

    Oh look at that, I was wrong - gradients are not a problem:

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.LinearGradient;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.graphics.Shader;
    import android.view.View;
    
    public class ButtonShadow extends View {
    
        public ButtonShadow(Context context)
        {
            super(context);
        }
    
        @Override
        public void onDraw(Canvas canvas)
        {
            RectF space = new RectF(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());
    
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    
            paint.setShader(new LinearGradient(0, getWidth(), 0, 0, Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));
    
            canvas.drawArc(space, 180, 360, true, paint);
    
            Rect rect = new Rect(this.getLeft(),this.getTop() + (this.getHeight() / 2),this.getRight(),this.getBottom());
            canvas.drawRect(rect, paint);
        }
    }
    

    For more on gradient fills look here: How to fill a Path in Android with a linear gradient?

提交回复
热议问题