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

其它

直接什么都不做的時(shí)候,沒有圓角,沒有分割線.
設(shè)置內(nèi)容item為3列,但是不滿3列,會導(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ù)測下一個(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,否則不對弄兜,比如第一個(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只會跨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);
    }

測距代碼 測距就是給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,會影響空間
            }

        }

        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,會影響空間
            }

        }

        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,否則不對语稠,比如第一個(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只會跨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)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市搭综,隨后出現(xiàn)的幾起案子垢箕,更是在濱河造成了極大的恐慌,老刑警劉巖兑巾,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件条获,死亡現(xiàn)場離奇詭異,居然都是意外死亡蒋歌,警方通過查閱死者的電腦和手機(jī)帅掘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進(jìn)店門委煤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人修档,你說我怎么就攤上這事碧绞。” “怎么了吱窝?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵讥邻,是天一觀的道長。 經(jīng)常有香客問我院峡,道長兴使,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任照激,我火速辦了婚禮鲫惶,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘实抡。我一直安慰自己欠母,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布吆寨。 她就那樣靜靜地躺著赏淌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪啄清。 梳的紋絲不亂的頭發(fā)上六水,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天,我揣著相機(jī)與錄音辣卒,去河邊找鬼掷贾。 笑死,一個(gè)胖子當(dāng)著我的面吹牛荣茫,可吹牛的內(nèi)容都是我干的想帅。 我是一名探鬼主播,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼啡莉,長吁一口氣:“原來是場噩夢啊……” “哼港准!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起咧欣,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤浅缸,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后魄咕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體衩椒,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了毛萌。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片梢什。...
    茶點(diǎn)故事閱讀 39,932評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖朝聋,靈堂內(nèi)的尸體忽然破棺而出嗡午,到底是詐尸還是另有隱情,我是刑警寧澤冀痕,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布荔睹,位于F島的核電站,受9級特大地震影響言蛇,放射性物質(zhì)發(fā)生泄漏僻他。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一腊尚、第九天 我趴在偏房一處隱蔽的房頂上張望吨拗。 院中可真熱鬧,春花似錦婿斥、人聲如沸劝篷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽娇妓。三九已至,卻和暖如春活鹰,著一層夾襖步出監(jiān)牢的瞬間哈恰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工志群, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留着绷,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓锌云,卻偏偏與公主長得像荠医,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子宾抓,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內(nèi)容