How do launchers change the shape of an adaptive icon, including removal of background?

后端 未结 6 2064
难免孤独
难免孤独 2020-12-24 12:42

Background

Starting from Android O, apps can have adaptive icons, which are 2 layers of drawables: foreground and a background. The background is a mask that gets

6条回答
  •  無奈伤痛
    2020-12-24 13:16

    Launchers have much less restrictions than applications, so they may use other approaches, but one solution has been nicely showcased in Nick Butcher's Adaptive Icon Playground.

    The class you're probably interested in is the Adaptive Icon View which renders adapted versions of the icon by creating a raster of each layer with the background as a canvas bitmap and then drawing those layers as rounded rectangles to implement the clipping.

    The linked repo will be much more informative and includes examples of how to transform the layer for movement effects etc., but here is the basic pseudo-code for 'adapting an icon' in an image view:

    setIcon() {
        //erase canvas first...
        canvas.setBitmap(background)
        drawable.setBounds(0, 0, layerSize, layerSize)
        drawable.draw(canvas)
    }
    
    onDraw() {
        //draw shadow first if needed...
        canvas.drawRoundRect(..., cornerRadius, backgroundPaint)
        canvas.drawRoundRect(..., cornerRadius, foregroundPaint)
    }
    

提交回复
热议问题