How to use setOutlineProvider instead of setOutline in Lollipop

前端 未结 3 1522
日久生厌
日久生厌 2020-12-02 19:47

For the earlier L Preview there were some examples like shown below to add to your code in order to use an FAB (Floating Action Button).

But unfortunately I can\'t u

相关标签:
3条回答
  • 2020-12-02 20:02

    Just to complete the @ianhanniballake answer:

    Button fab = (Button) findViewById(R.id.fab);
    //Outline outline = new Outline();
    //outline.setOval(0, 0, size, size);
    //fab.setOutline(outline);  
    ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                // Or read size directly from the view's width/height
                int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
                outline.setOval(0, 0, size, size);
            }
        };
    fab.setOutlineProvider(viewOutlineProvider);
    
    0 讨论(0)
  • 2020-12-02 20:04

    Every View has an Outline object and a ViewOutlineProvider(V.O.P) object. As the name suggest viewoutlineprovider provides the outline to the view indirectly.

    What the view does is it passes its outline object to the V.O.P and the V.O.P updates it.

    so you have the view's Outline object in the getOutline method of the VOP as a parameter,simply update it.

    ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
        @Override
        public void getOutline(View view, Outline outline) {
            outline.setOval(0, 0, view.getWidth(), view.getHeight());
        }
    };
    fab.setOutlineProvider(viewOutlineProvider);
    
    0 讨论(0)
  • 2020-12-02 20:18

    Per customizing view shadows and outlines training:

    You can create oval and rectangular outlines with rounded corners using the methods in the Outline class. The default outline provider for views obtains the outline from the view's background. To prevent a view from casting a shadow, set its outline provider to null.

    So your ViewOutlineProvider just needs to call setOval(0, 0, size, size) on the outline parameter to getOutline():

    public void getOutline (View view, Outline outline) {
        // Or read size directly from the view's width/height
        int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
        outline.setOval(0, 0, size, size);
    }
    
    0 讨论(0)
提交回复
热议问题