ShadeViewGroup


/**
 * @Description: 類作用描述
 * @Author: hukui
 * @Date: 2020/9/7 17:20
 */

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.mylibrary.api.R;


/**
 * 嘗試一個(gè)自定義陰影View
 */
public class ShadeViewGroup extends ViewGroup {
    private float deltaLength;
    private float cornerRadius;
    private Paint mShadowPaint;
    private boolean drawShadow;
    private Context mContext;
    private int shadowColor;
    private float shadowRadius;
    private float dx;
    private float dy;

    public ShadeViewGroup(Context context) {
        this(context, null);
    }

    public ShadeViewGroup(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShadeViewGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView(attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        View child = getChildAt(0);
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        int childMeasureWidth = child.getMeasuredWidth();
        int childMeasureHeight = child.getMeasuredHeight();
        child.layout((measuredWidth - childMeasureWidth) / 2, (measuredHeight - childMeasureHeight) / 2, (measuredWidth + childMeasureWidth) / 2, (measuredHeight + childMeasureHeight) / 2);
    }


    /**
     * 初始化信息變量
     */
    private void initView(AttributeSet attrs) {
        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.ShadeViewGroup);
        shadowColor = a.getColor(R.styleable.ShadeViewGroup_containerShadowColor, Color.WHITE);
        shadowRadius = a.getDimension(R.styleable.ShadeViewGroup_containerShadowRadius, 0);
        dx = a.getDimension(R.styleable.ShadeViewGroup_deltaX, 0);
        dy = a.getDimension(R.styleable.ShadeViewGroup_deltaY, 0);
        deltaLength = a.getDimension(R.styleable.ShadeViewGroup_containerDeltaLength, 0);
        cornerRadius = a.getDimension(R.styleable.ShadeViewGroup_containerCornerRadius, 0);

        drawShadow = a.getBoolean(R.styleable.ShadeViewGroup_enable, true);
        a.recycle();
        initShadowPaint();
    }

    public void setShadowColor(int shadowColor) {
        this.shadowColor = shadowColor;
        initShadowPaint();
        invalidate();
    }

    private void initShadowPaint() {
        mShadowPaint = new Paint();
        mShadowPaint.setStyle(Paint.Style.FILL);
        mShadowPaint.setAntiAlias(true);
        mShadowPaint.setColor(shadowColor);
        mShadowPaint.setShadowLayer(shadowRadius, dx, dy, shadowColor);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (getChildCount() != 1) {
            throw new IllegalStateException("子View只能有一個(gè)");
        }
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        View child = getChildAt(0);
        LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
        int childBottomMargin = (int) (Math.max(deltaLength, layoutParams.bottomMargin) + 1);
        int childLeftMargin = (int) (Math.max(deltaLength, layoutParams.leftMargin) + 1);
        int childRightMargin = (int) (Math.max(deltaLength, layoutParams.rightMargin) + 1);
        int childTopMargin = (int) (Math.max(deltaLength, layoutParams.topMargin) + 1);
        int widthMeasureSpecMode;
        int widthMeasureSpecSize;
        int heightMeasureSpecMode;
        int heightMeasureSpecSize;
        if (widthMode == MeasureSpec.UNSPECIFIED) {
            widthMeasureSpecMode = MeasureSpec.UNSPECIFIED;
            widthMeasureSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        } else {
            if (layoutParams.width == LayoutParams.MATCH_PARENT) {
                widthMeasureSpecMode = MeasureSpec.EXACTLY;
                widthMeasureSpecSize = measuredWidth - childLeftMargin - childRightMargin;
            } else if (LayoutParams.WRAP_CONTENT == layoutParams.width) {
                widthMeasureSpecMode = MeasureSpec.AT_MOST;
                widthMeasureSpecSize = measuredWidth - childLeftMargin - childRightMargin;
            } else {
                widthMeasureSpecMode = MeasureSpec.EXACTLY;
                widthMeasureSpecSize = layoutParams.width;
            }
        }
        if (heightMode == MeasureSpec.UNSPECIFIED) {
            heightMeasureSpecMode = MeasureSpec.UNSPECIFIED;
            heightMeasureSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        } else {
            if (layoutParams.height == LayoutParams.MATCH_PARENT) {
                heightMeasureSpecMode = MeasureSpec.EXACTLY;
                heightMeasureSpecSize = measuredHeight - childBottomMargin - childTopMargin;
            } else if (LayoutParams.WRAP_CONTENT == layoutParams.height) {
                heightMeasureSpecMode = MeasureSpec.AT_MOST;
                heightMeasureSpecSize = measuredHeight - childBottomMargin - childTopMargin;
            } else {
                heightMeasureSpecMode = MeasureSpec.EXACTLY;
                heightMeasureSpecSize = layoutParams.height;
            }
        }
        measureChild(child, MeasureSpec.makeMeasureSpec(widthMeasureSpecSize, widthMeasureSpecMode), MeasureSpec.makeMeasureSpec(heightMeasureSpecSize, heightMeasureSpecMode));
        int parentWidthMeasureSpec = MeasureSpec.getMode(widthMeasureSpec);
        int parentHeightMeasureSpec = MeasureSpec.getMode(heightMeasureSpec);
        int height = measuredHeight;
        int width = measuredWidth;
        int childHeight = child.getMeasuredHeight();
        int childWidth = child.getMeasuredWidth();
        if (parentHeightMeasureSpec == MeasureSpec.AT_MOST) {
            height = childHeight + childTopMargin + childBottomMargin;
        }
        if (parentWidthMeasureSpec == MeasureSpec.AT_MOST) {
            width = childWidth + childRightMargin + childLeftMargin;
        }
        if (width < childWidth + 2 * deltaLength) {
            width = (int) (childWidth + 2 * deltaLength);
        }
        if (height < childHeight + 2 * deltaLength) {
            height = (int) (childHeight + 2 * deltaLength);
        }
        if (height != measuredHeight || width != measuredWidth) {
            setMeasuredDimension(width, height);
        }
    }

    static class LayoutParams extends MarginLayoutParams {

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(MarginLayoutParams source) {
            super(source);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }
    }

    @Override
    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new LayoutParams(p);
    }

    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void dispatchDraw(Canvas canvas) {
        if (drawShadow) {
            if (getLayerType() != LAYER_TYPE_SOFTWARE) {
                setLayerType(LAYER_TYPE_SOFTWARE, null);
            }
            View child = getChildAt(0);
            int left = child.getLeft();
            int top = child.getTop();
            int right = child.getRight();
            int bottom = child.getBottom();
            RectF rectF = new RectF(left, top, right, bottom);
            canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, mShadowPaint);
        }
        super.dispatchDraw(canvas);
    }
}
    <declare-styleable name="ShadeViewGroup">
        <attr name="containerShadowColor" format="color" />
        <attr name="containerShadowRadius" format="dimension" />
        <attr name="containerDeltaLength" format="dimension" />
        <attr name="containerCornerRadius" format="dimension" />
        <attr name="deltaX" format="dimension" />
        <attr name="deltaY" format="dimension" />
        <attr name="enable" format="boolean" />
    </declare-styleable>

簡單用法

      <com.mylibrary.api.widget.ShadeViewGroup
                android:id="@+id/shopCar_EditLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginStart="-6dp"
                android:layout_marginEnd="-6dp"
                android:layout_marginBottom="-6dp"
                android:visibility="gone"
                app:containerCornerRadius="@dimen/radius2"
                app:containerDeltaLength="@dimen/shardRadius"
                app:containerShadowColor="@color/shadowColor"
                app:containerShadowRadius="@dimen/shardRadius"></com.mylibrary.api.widget.ShadeViewGroup>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市磅氨,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌棕所,老刑警劉巖旅择,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡秘案,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門潦匈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來阱高,“玉大人,你說我怎么就攤上這事茬缩〕嗑” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵凰锡,是天一觀的道長未舟。 經(jīng)常有香客問我,道長掂为,這世上最難降的妖魔是什么裕膀? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮勇哗,結(jié)果婚禮上昼扛,老公的妹妹穿的比我還像新娘。我一直安慰自己欲诺,他們只是感情好抄谐,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著瞧栗,像睡著了一般斯稳。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上迹恐,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天挣惰,我揣著相機(jī)與錄音,去河邊找鬼。 笑死憎茂,一個(gè)胖子當(dāng)著我的面吹牛珍语,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播竖幔,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼板乙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了拳氢?” 一聲冷哼從身側(cè)響起募逞,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎馋评,沒想到半個(gè)月后放接,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡留特,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年纠脾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蜕青。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡苟蹈,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出右核,到底是詐尸還是另有隱情慧脱,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布贺喝,位于F島的核電站磷瘤,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏搜变。R本人自食惡果不足惜采缚,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望挠他。 院中可真熱鬧扳抽,春花似錦、人聲如沸殖侵。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拢军。三九已至楞陷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間茉唉,已是汗流浹背固蛾。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國打工结执, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人艾凯。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓献幔,卻偏偏與公主長得像,于是被迫代替她去往敵國和親趾诗。 傳聞我的和親對象是個(gè)殘疾皇子蜡感,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

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

  • 原題目在這里[https://www.nowcoder.com/discuss/645211?channel=-1...
    錦岳閱讀 875評(píng)論 3 14
  • 這些題目是網(wǎng)友去百度、小米恃泪、樂視郑兴、美團(tuán)、58贝乎、獵豹杈笔、360、新浪糕非、搜狐等一線互聯(lián)網(wǎng)公司面試被問到的題目。熟悉本文中...
    Android劉東閱讀 2,866評(píng)論 0 16
  • 1.四大組件是什么 答: Activity; Service; Broadcast reciver; Conten...
    zekers閱讀 1,069評(píng)論 0 1
  • 第七章 吳普球榆、陸璣朽肥、朱熹和李時(shí)珍的澤蘭論考辨 原創(chuàng) 2018-04-06 李正宣 漢字正解 (這篇文章是筆者《先秦...
    蓉城泰伯閱讀 831評(píng)論 0 0
  • 第七章 吳普、陸璣持钉、朱熹和李時(shí)珍的澤蘭論考辨 原創(chuàng) 2018-04-06 李正宣 漢字正解 (這篇文章是筆者《先秦...
    蓉城泰伯閱讀 1,139評(píng)論 0 0