Gradient Radius as percentage of screen size

前端 未结 5 1849
南方客
南方客 2020-12-08 13:56

I\'m trying to create a shape drawable with radial gradient background, with radius that will adjust to the screen size (take a look at the relevant documentation).

5条回答
  •  难免孤独
    2020-12-08 14:01

    I ended up creating a custom View with following overriden onDraw method:

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    
        int maxSize = Math.max(getHeight(), getWidth());
        RadialGradient gradient = new RadialGradient(
                getWidth()/2,
                getHeight()/2,
                maxSize, 
                new int[] {Color.RED, Color.BLACK},
                new float[] {0, 1}, 
                android.graphics.Shader.TileMode.CLAMP);
    
        paint.setDither(true);
        paint.setShader(gradient);
    
        canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
    }
    

    In your layout just add the View:

    
    

    Of course it would be possible to create attributes for the View, eg. color, percentage to allow customization via XML.

提交回复
热议问题