[原創(chuàng)]recyclerview實(shí)現(xiàn)多行分組 給分組塊加圓角.

如果用多個(gè)recyclerview 或者里面多套個(gè)adapter當(dāng)我沒說,這里的實(shí)現(xiàn)方式是通過.借助griviewmanager和item

效果圖是要達(dá)到這樣的.當(dāng)然這也是我最后搞完的圖.

29a5f9a7d0ecebe84ed456ae9ff84eb.jpg

需求:

標(biāo)題作為1行 內(nèi)容為3列任意行.
標(biāo)題內(nèi)容分組 給標(biāo)題內(nèi)容包裹區(qū)域加圓角.
只有內(nèi)容無標(biāo)題的分組 就給內(nèi)容區(qū)域加圓角

遇到的問題

1.當(dāng)內(nèi)容未滿3列,顏色為recyclerview顏色或背景色.
2.邊距寫的不對(duì),導(dǎo)致內(nèi)容被扭曲,某個(gè)item出現(xiàn)背景色而且未和其他行對(duì)其.
3.滑動(dòng)的時(shí)候發(fā)現(xiàn)我繪制的顏色掉了(是我粗心用的i而不是適配器位置導(dǎo)致)
4.判斷末尾行
如果是末尾行直接填充白色圓角
5.如果沒有標(biāo)題,直接給內(nèi)容加上左 和上右圓角.
6.最底部的邊距沒得.

其它

直接什么都不做的時(shí)候,沒有圓角,沒有分割線.
設(shè)置內(nèi)容item為3列,但是不滿3列,會(huì)導(dǎo)致出現(xiàn)背景顏色,有些朋友可能直接設(shè)置recyclerview背景了,但是你怕是不要處理圓角了??

如何實(shí)現(xiàn)?

  1. 使用GridLayoutManager
    2.給GridLayoutManager 設(shè)置setSpanSizeLookup 如果是標(biāo)題就合并 getSpanSize 表示合并的尺寸,如果是內(nèi)容是3列,那么這里返回3.否則返回1 ,
  2. 設(shè)置ItemDecoration 用getItemOffsets 控制間隔 ,
  3. 設(shè)置ItemDecoration 用public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int spanSize = layoutManager.getSpanSizeLookup().getSpanSize(childAdapterPosition);

主要用于判斷當(dāng)前組是不是0,最后一組我用的另外的方法,組0 index=0, 代表是頭部,如果是標(biāo)題就繪制整個(gè),如果內(nèi)容 index=0也繪制 ,額好像不需要用到spanGroupIndex我記得用到過得

int spanGroupIndex = layoutManager.getSpanSizeLookup().getSpanGroupIndex(childAdapterPosition, spanCount);
  int spanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(childAdapterPosition, spanCount); //比如第一個(gè)是標(biāo)題 ,第二列是 3格子,標(biāo)題的永遠(yuǎn)是0 ,分3列的則是0 12 這樣的循環(huán)。groupindex就是 每一列的完畢  就算一組。

用于判斷是否是最后一行

 int itemCount = parent.getLayoutManager().getItemCount();

用于預(yù)測(cè)下一個(gè)是不是標(biāo)題,是標(biāo)題那么現(xiàn)在就應(yīng)該開始繪制下半身的圓角,以及如果內(nèi)容item列不滿足總列數(shù),就繪制彌補(bǔ)空白處.

int lastspanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(itemCount-1, spanCount); //比如第一個(gè)是標(biāo)題 ,第二列是 3格子,標(biāo)題的永遠(yuǎn)是0 ,分3列的則是0 12 這樣的循環(huán)。groupindex就是 每一列的完畢  就算一組。

下面是具體寫完之后的代碼.

繪制空缺塊和圓角塊.
下圖紅色代表填充的空缺快,最后一組的填充則是綠色 , 頭部是藍(lán)色圓角 , 非最后一組底部圓角填充了黃色,我這樣做只是為了調(diào)試,弄完之后我就把顏色改成白色了..
經(jīng)常用到的判斷
判斷當(dāng)前是跨了幾行,用于判斷是否是標(biāo)題

GridLayoutManager layoutManager = new GridLayoutManager(recyclerView.getContext(), spanCount, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);

        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int type = recyclerView.getAdapter().getItemViewType(position);
                if (position >= recyclerView.getAdapter().getItemCount()) {
                    return NOT_FOUND_SPAN_SIZE;
                } else if (type == TYPE_MENU_GROUP_NAME) {
                    return MENU_TITLE_SPAN_SIZE;
                } else if (type == TYPE_GROUP_DATA) {
                    return 1;
                } else {
                    return NOT_FOUND_SPAN_SIZE;//這是 decoration調(diào)用的。
                }
            }

            @Override
            public int getSpanIndex(int position, int spanCount) {
                int spanIndex = super.getSpanIndex(position, spanCount);
                Log.w(TAG, "spanIndex" + spanIndex + ",pos" + position + ",spanCount:" + spanCount);
                return spanIndex;
            }

            @Override
            public int getSpanGroupIndex(int adapterPosition, int spanCount) {
                int spanGroupIndex = super.getSpanGroupIndex(adapterPosition, spanCount);
                Log.w(TAG, "spanGroupIndex" + spanGroupIndex + ",adapterPosition" + adapterPosition + ",spanCount:" + spanCount);
                return spanGroupIndex;
            }
        });

繪制圓角和空缺快設(shè)置代碼

  for (int ix = 0; ix < childCount; ix++) {
            final View child = parent.getChildAt(ix);

            int position = parent.getChildAdapterPosition(child);

            GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
            int spanCount = layoutManager.getSpanCount();//表示整個(gè)列表分多少列
            int spanSize = layoutManager.getSpanSizeLookup().getSpanSize(position);//表示當(dāng)前格所占有的格子數(shù)
            int spanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(position, spanCount); //這里參數(shù)是spanCount,不是spanSize,否則不對(duì),比如第一個(gè)是標(biāo)題 ,第二列是 3格子,標(biāo)題的永遠(yuǎn)是0 ,分3列的則是0 12 這樣的循環(huán)。groupindex就是 每一列的完畢  就算一組。


            if (BuildConfig.DEBUG) {
//                MyAppListAdapter adapter = (MyAppListAdapter) parent.getAdapter();
//                SubMenuItemI subMenuItemI = adapter.getData().get(position);
//                int spanSizeNext = layoutManager.getSpanSizeLookup().getSpanSize(position + 1);
            }

            if (spanSize != MENU_TITLE_SPAN_SIZE) {//普通item只會(huì)跨1個(gè)。 除非是菜單
            int spanGroupIndex = layoutManager.getSpanSizeLookup().getSpanGroupIndex(position, spanCount);
                int spanSizeNext = layoutManager.getSpanSizeLookup().getSpanSize(position + 1);
                if (spanIndex < MENU_TITLE_SPAN_SIZE - 1) {// MENU_TITLE_SPAN_SIZE 等于new GridManager里面的Count,這里用合并的數(shù)來判斷也可以做,是否屬于最后一個(gè)格子,但是最后一個(gè)格子又是否鋪滿整個(gè)屏幕是最后一個(gè)但是沒有剛好沒鋪滿,就把它給鋪滿。
                    if (spanSizeNext == NOT_FOUND_SPAN_SIZE || spanSizeNext == MENU_TITLE_SPAN_SIZE) {//如果下一個(gè)不存在那么是最后一個(gè),或者下一個(gè)是標(biāo)題了。
                        drawable = generateDrawable(-1);//補(bǔ)空缺
                        boundAndDraw(c, child.getRight(), right, drawable, child.getTop(), child.getBottom());//這里沒有任何圓角
                        if (BuildConfig.DEBUG) {
            /*                MyAppListAdapter adapter = (MyAppListAdapter) parent.getAdapter();
                            SubMenuItemI subMenuItemI = adapter.getData().get(position);
                            Log.w(TAG, subMenuItemI + ",為最后一個(gè)前后面超出!spanIndex:" + spanIndex + ",spanSize:" + spanSize + ",height:" + child.getHeight());
                            */
                        }

                    }
                }

                if (spanSizeNext == NOT_FOUND_SPAN_SIZE || spanSizeNext == MENU_TITLE_SPAN_SIZE) {// 剛好鋪滿的那種 鋪滿還需要判斷是否下一行是否是標(biāo)題.
                    //左下角圓角.
                    drawable = generateDrawable(5);//,spanSizeNext == NOT_FOUND_SPAN_SIZE ?Color.GREEN:Color.YELLOW);
                    int top = child.getTop() + child.getHeight();
                    int bottom = (int) (top + radius);
//                            Log.w(TAG,"LEFT:"+left+",top:"+top+",right:"+right+",bottom:"+bottom);
                    boundAndDraw(c, left, right, drawable, top, bottom);
                }
                if( spanGroupIndex==0&&position==0){//沒有頭部 的頂
                    drawable = generateDrawable(4);
                    int top = (int) (child.getTop() - radius);
                    int bottom = child.getTop();
                    boundAndDraw(c, left, right, drawable, top, bottom);
                }

            } else {//標(biāo)題頭
                drawable = generateDrawable(4);
                int top = (int) (child.getTop() - radius);
                int bottom = child.getTop();
                boundAndDraw(c, left, right, drawable, top, bottom);
            }
        }
    }

    private void debugDrwa(Canvas c, int left, int right, View child) {
        //以下計(jì)算主要用來確定繪制的位置
        final int top1 = child.getBottom() + 1;
        ColorDrawable drawable1 = new ColorDrawable(Color.BLUE);
        final int bottom = top1 + drawable1.getIntrinsicHeight();
        boundAndDraw(c, left, right, drawable1, top1, bottom);
    }

/**
     * /外矩形 0左上、1右上、2 右下、3左下的圓角半 4 上 左和右邊 5 ,下 左右。
     *
     * @param type
     * @return
     */
    private GradientDrawable generateDrawable(int type, int color) {
        this.color = color;
        return generateDrawable(type);
    }

測(cè)距代碼 測(cè)距就是給view留空間,不留空間就繪制在一坨了.

   int childAdapterPosition = parent.getChildAdapterPosition(view);
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        int spanCount = layoutManager.getSpanCount(); //構(gòu)造時(shí)傳遞的參數(shù).
        //DefaultSpanSizeLookup
        int spanSize = layoutManager.getSpanSizeLookup().getSpanSize(childAdapterPosition);
        int spanGroupIndex = layoutManager.getSpanSizeLookup().getSpanGroupIndex(childAdapterPosition, spanCount);

        if (spanSize == MENU_TITLE_SPAN_SIZE) { //標(biāo)題才跨 3個(gè)。
//            if (needTop) {
            if (spanGroupIndex == 0) {
                outRect.top = (int) (getSpace() + radius);
            } else {
            outRect.top = (int) (getSpace() + (radius*2));
            }
//            }
        } else  {
            outRect.top = 0;
            outRect.left = 0;
            outRect.right = 0;
            if( spanGroupIndex==0){//沒有頭部 的頂
                outRect.top = (int) (getSpace()  + radius);
            }else{
                outRect.top = 0;
            }
            int spanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(childAdapterPosition, spanCount); //比如第一個(gè)是標(biāo)題 ,第二列是 3格子,標(biāo)題的永遠(yuǎn)是0 ,分3列的則是0 12 這樣的循環(huán)。groupindex就是 每一列的完畢  就算一組。
            int itemCount = parent.getLayoutManager().getItemCount();
            int lastspanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(itemCount-1, spanCount); //比如第一個(gè)是標(biāo)題 ,第二列是 3格子,標(biāo)題的永遠(yuǎn)是0 ,分3列的則是0 12 這樣的循環(huán)。groupindex就是 每一列的完畢  就算一組。


            if (childAdapterPosition>=itemCount-(lastspanIndex+1)) {//最后一行
                outRect.bottom= (int) (getSpace()+(radius/2));
                if(lastspanIndex==spanIndex&&(1+spanIndex!=MENU_TITLE_SPAN_SIZE)){
//                        outRect.right=30;//=parent.getRight()-parent.getPaddingRight();//寫不寫其實(shí)都一樣,因?yàn)椴焕L制右邊?不,應(yīng)該說都鋪滿屏幕了,不怕控件沒給夠
                                    }//不能加right,會(huì)影響空間
            }

        }

        if (BuildConfig.DEBUG) {
//            int spanGroupIndex =;//1 23格子 或者 1列 都算一組,
            int spanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(childAdapterPosition, spanCount); //比如第一個(gè)是標(biāo)題 ,第二列是 3格子,標(biāo)題的永遠(yuǎn)是0 ,分3列的則是0 12 這樣的循環(huán)。groupindex就是 每一列的完畢  就算一組。


     /*       SubMenuItemI subMenuItemI = ((MyAppListAdapter) parent.getAdapter()).getData().get(childAdapterPosition);
            String title = subMenuItemI.getTitle();*/
            Log.w(TAG, "Decoration SpanIndex:" + spanIndex + ",groupindex:" + spanGroupIndex + ",spanSize:" + spanSize + ",model:" + "title" + ",adapterposition:" + childAdapterPosition);

        }

最終源碼


public class MenuDecoration extends SpacesItemDecoration {
    private static final int NOT_FOUND_SPAN_SIZE = -1;
    private static int MENU_TITLE_SPAN_SIZE = 3;
    private final int shadowSize=2;
    private float radius = 30;
    private int color = Color.RED;
    private boolean needTop;
    public static final int TYPE_GROUP_DATA = 1;
    public static final int TYPE_MENU_GROUP_NAME = 0;

    /**
     * @param recyclerView
     * @param spanCount
     * @param needLeft
     * @param needRight
     * @param isneddTop
     */
    public MenuDecoration(RecyclerView recyclerView, int spanCount, boolean needLeft, boolean needRight, boolean isneddTop, int space, float radius, int color) {

        super((int) (space));
        this.radius = radius;
        this.color = color;
        GridLayoutManager layoutManager = new GridLayoutManager(recyclerView.getContext(), spanCount, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);

        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int type = recyclerView.getAdapter().getItemViewType(position);
                if (position >= recyclerView.getAdapter().getItemCount()) {
                    return NOT_FOUND_SPAN_SIZE;
                } else if (type == TYPE_MENU_GROUP_NAME) {
                    return MENU_TITLE_SPAN_SIZE;
                } else if (type == TYPE_GROUP_DATA) {
                    return 1;
                } else {
                    return NOT_FOUND_SPAN_SIZE;//這是 decoration調(diào)用的。
                }
            }

            @Override
            public int getSpanIndex(int position, int spanCount) {
                int spanIndex = super.getSpanIndex(position, spanCount);
                Log.w(TAG, "spanIndex" + spanIndex + ",pos" + position + ",spanCount:" + spanCount);
                return spanIndex;
            }

            @Override
            public int getSpanGroupIndex(int adapterPosition, int spanCount) {
                int spanGroupIndex = super.getSpanGroupIndex(adapterPosition, spanCount);
                Log.w(TAG, "spanGroupIndex" + spanGroupIndex + ",adapterPosition" + adapterPosition + ",spanCount:" + spanCount);
                return spanGroupIndex;
            }
        });
        /**
         * 頂部 下面實(shí)現(xiàn)了
         */
        this.needTop = isneddTop;
        MENU_TITLE_SPAN_SIZE = spanCount;
        recyclerView.setPadding(needLeft ? getSpace()  : 0, isneddTop ? getSpace() / 2 : 0, needRight ? getSpace() : 0, getSpace() / 2);
    }

    private static final String TAG = "Gridlayout";

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

//        int itemCount = parent.getLayoutManager().getItemCount();
        int childAdapterPosition = parent.getChildAdapterPosition(view);
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        int spanCount = layoutManager.getSpanCount(); //構(gòu)造時(shí)傳遞的參數(shù).
        //DefaultSpanSizeLookup
        int spanSize = layoutManager.getSpanSizeLookup().getSpanSize(childAdapterPosition);
        int spanGroupIndex = layoutManager.getSpanSizeLookup().getSpanGroupIndex(childAdapterPosition, spanCount);

        if (spanSize == MENU_TITLE_SPAN_SIZE) { //標(biāo)題才跨 3個(gè)。
//            if (needTop) {
            if (spanGroupIndex == 0) {
                outRect.top = (int) (getSpace() + radius);
            } else {
            outRect.top = (int) (getSpace() + (radius*2));
            }
//            }
        } else  {
            outRect.top = 0;
            outRect.left = 0;
            outRect.right = 0;
            if( spanGroupIndex==0){//沒有頭部 的頂
                outRect.top = (int) (getSpace()  + radius);
            }else{
                outRect.top = 0;
            }
            int spanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(childAdapterPosition, spanCount); //比如第一個(gè)是標(biāo)題 ,第二列是 3格子,標(biāo)題的永遠(yuǎn)是0 ,分3列的則是0 12 這樣的循環(huán)。groupindex就是 每一列的完畢  就算一組。
            int itemCount = parent.getLayoutManager().getItemCount();
            int lastspanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(itemCount-1, spanCount); //比如第一個(gè)是標(biāo)題 ,第二列是 3格子,標(biāo)題的永遠(yuǎn)是0 ,分3列的則是0 12 這樣的循環(huán)。groupindex就是 每一列的完畢  就算一組。


            if (childAdapterPosition>=itemCount-(lastspanIndex+1)) {//最后一行
                outRect.bottom= (int) (getSpace()+(radius/2));
                if(lastspanIndex==spanIndex&&(1+spanIndex!=MENU_TITLE_SPAN_SIZE)){
//                        outRect.right=30;//=parent.getRight()-parent.getPaddingRight();//寫不寫其實(shí)都一樣,因?yàn)椴焕L制右邊?不,應(yīng)該說都鋪滿屏幕了,不怕控件沒給夠
                                    }//不能加right,會(huì)影響空間
            }

        }

        if (BuildConfig.DEBUG) {
//            int spanGroupIndex =;//1 23格子 或者 1列 都算一組,
            int spanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(childAdapterPosition, spanCount); //比如第一個(gè)是標(biāo)題 ,第二列是 3格子,標(biāo)題的永遠(yuǎn)是0 ,分3列的則是0 12 這樣的循環(huán)。groupindex就是 每一列的完畢  就算一組。
     /*       SubMenuItemI subMenuItemI = ((MyAppListAdapter) parent.getAdapter()).getData().get(childAdapterPosition);
            String title = subMenuItemI.getTitle();*/
            Log.w(TAG, "Decoration SpanIndex:" + spanIndex + ",groupindex:" + spanGroupIndex + ",spanSize:" + spanSize + ",model:" + "title" + ",adapterposition:" + childAdapterPosition);

        }
    }


    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
//    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        doDraw(c, parent);
    }

    private void doDraw(Canvas c, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();
        Drawable drawable;
        for (int ix = 0; ix < childCount; ix++) {
            final View child = parent.getChildAt(ix);
/*
            if (true) {


                debugDrwa(c, left, right, child);
                continue;
            }*/
            int position = parent.getChildAdapterPosition(child);
//                parent.getChildLayoutPosition()
            GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
            int spanCount = layoutManager.getSpanCount();//表示整個(gè)列表分多少列
            int spanSize = layoutManager.getSpanSizeLookup().getSpanSize(position);//表示當(dāng)前格所占有的格子數(shù)
            int spanIndex = layoutManager.getSpanSizeLookup().getSpanIndex(position, spanCount); //這里參數(shù)是spanCount,不是spanSize,否則不對(duì),比如第一個(gè)是標(biāo)題 ,第二列是 3格子,標(biāo)題的永遠(yuǎn)是0 ,分3列的則是0 12 這樣的循環(huán)。groupindex就是 每一列的完畢  就算一組。


            if (BuildConfig.DEBUG) {
//                MyAppListAdapter adapter = (MyAppListAdapter) parent.getAdapter();
//                SubMenuItemI subMenuItemI = adapter.getData().get(position);
//                int spanSizeNext = layoutManager.getSpanSizeLookup().getSpanSize(position + 1);
            }

            if (spanSize != MENU_TITLE_SPAN_SIZE) {//普通item只會(huì)跨1個(gè)。 除非是菜單
            int spanGroupIndex = layoutManager.getSpanSizeLookup().getSpanGroupIndex(position, spanCount);
                int spanSizeNext = layoutManager.getSpanSizeLookup().getSpanSize(position + 1);
                if (spanIndex < MENU_TITLE_SPAN_SIZE - 1) {// MENU_TITLE_SPAN_SIZE 等于new GridManager里面的Count,這里用合并的數(shù)來判斷也可以做,是否屬于最后一個(gè)格子,但是最后一個(gè)格子又是否鋪滿整個(gè)屏幕是最后一個(gè)但是沒有剛好沒鋪滿,就把它給鋪滿。
                    if (spanSizeNext == NOT_FOUND_SPAN_SIZE || spanSizeNext == MENU_TITLE_SPAN_SIZE) {//如果下一個(gè)不存在那么是最后一個(gè),或者下一個(gè)是標(biāo)題了。
                        drawable = generateDrawable(-1);//補(bǔ)空缺
                        boundAndDraw(c, child.getRight(), right, drawable, child.getTop(), child.getBottom());//這里沒有任何圓角
                        if (BuildConfig.DEBUG) {
            /*                MyAppListAdapter adapter = (MyAppListAdapter) parent.getAdapter();
                            SubMenuItemI subMenuItemI = adapter.getData().get(position);
                            Log.w(TAG, subMenuItemI + ",為最后一個(gè)前后面超出!spanIndex:" + spanIndex + ",spanSize:" + spanSize + ",height:" + child.getHeight());
                            */
                        }

                    }
                }

                if (spanSizeNext == NOT_FOUND_SPAN_SIZE || spanSizeNext == MENU_TITLE_SPAN_SIZE) {// 剛好鋪滿的那種 鋪滿還需要判斷是否下一行是否是標(biāo)題.
                    //左下角圓角.
                    drawable = generateDrawable(5);//,spanSizeNext == NOT_FOUND_SPAN_SIZE ?Color.GREEN:Color.YELLOW);
                    int top = child.getTop() + child.getHeight();
                    int bottom = (int) (top + radius);
//                            Log.w(TAG,"LEFT:"+left+",top:"+top+",right:"+right+",bottom:"+bottom);
                    boundAndDraw(c, left, right, drawable, top, bottom);
                }
                if( spanGroupIndex==0&&position==0){//沒有頭部 的頂
                    drawable = generateDrawable(4);
                    int top = (int) (child.getTop() - radius);
                    int bottom = child.getTop();
                    boundAndDraw(c, left, right, drawable, top, bottom);
                }

            } else {//標(biāo)題頭
                drawable = generateDrawable(4);
                int top = (int) (child.getTop() - radius);
                int bottom = child.getTop();
                boundAndDraw(c, left, right, drawable, top, bottom);
            }
        }
    }

    private void debugDrwa(Canvas c, int left, int right, View child) {
        //以下計(jì)算主要用來確定繪制的位置
        final int top1 = child.getBottom() + 1;
        ColorDrawable drawable1 = new ColorDrawable(Color.BLUE);
        final int bottom = top1 + drawable1.getIntrinsicHeight();
        boundAndDraw(c, left, right, drawable1, top1, bottom);
    }

    private void boundAndDraw(Canvas c, int left, int right, Drawable drawable, int top, int bottom) {
//        Log.w(TAG,"DrawCall LEFT:"+left+",righ:"+right+",top:"+top+",bottom:"+bottom);
        drawable.setBounds(left, top, right, bottom);
//                                drawable.setBounds(left, (int) (child.getTop()+child.getHeight()-radius), left + child.getWidth(), child.getBottom());
        drawable.draw(c);
    }

    /**
     * /外矩形 0左上、1右上、2 右下、3左下的圓角半 4 上 左和右邊 5 ,下 左右。
     *
     * @param type
     * @return
     */
    private Drawable generateDrawable(int type, int color) {
        this.color = color;
        return generateDrawable(type);
    }

    private Drawable generateDrawable(int type) {

        GradientDrawable gd = new GradientDrawable();
        //        color=color|0xff000000;

        gd.setColor(color);
        switch (type) {
            case 0:
                gd.setCornerRadii(new float[]{radius, radius, 0, 0, 0, 0, 0, 0});
                break;
            case 1:
                gd.setCornerRadii(new float[]{0, 0, radius, radius, 0, 0, 0, 0});
                break;
            case 2:
                gd.setCornerRadii(new float[]{0, 0, 0, 0, radius, radius, 0, 0});
                break;
            case 3:
                gd.setCornerRadii(new float[]{0, 0, 0, 0, 0, 0, radius, radius});
                break;
            case 4:
                gd.setCornerRadii(new float[]{radius, radius, radius, radius, 0, 0, 0, 0});
                break;
            case 5:
                gd.setCornerRadii(new float[]{0, 0, 0, 0, radius, radius, radius, radius});
                break;
            case 6:
                gd.setCornerRadii(new float[]{radius, radius, radius, radius, radius, radius, radius, radius});
                break;
            default:
                gd.setCornerRadii(new float[]{0, 0, 0, 0, 0, 0, 0, 0});
                break;
        }
/*
        Drawable[] drawables=new Drawable[2];
        GradientDrawable gdshadow = new GradientDrawable();

        //#ffdb8f  #ffdb8f
        gdshadow.setColor(Color.parseColor("#00ff00"));
        drawables[0]=gdshadow;
        drawables[1]=gd;
        LayerDrawable layerDrawable=new LayerDrawable(drawables);

*/

        return gd;
    }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容