自定义阴影LinearLayout,自带padding

先上东西,请看这个popupwindow,

image.png

实际上它是有阴影的


image.png
注意我的高和宽都加上了阴影的宽度240+7+3  长度341+7+3

<com.yinke.demon.commonres.view.ShawdeLinearLayout  
  android:layout_width="250dp"
    android:layout_height="351dp"
    android:background="@color/public_color_transparent"
    app:public_bg_color="@color/withe"
    app:public_effect="7dp"
    app:public_nobottom="false"
    app:public_noleft="false"
    app:public_noright="false"
    app:public_offset_x="3dp"
    app:public_offset_y="3dp"
    app:public_radius="0dp"
    app:public_shaw_color="#33000000">
</com.yinke.demon.commonres.view.ShawdeLinearLayout>

下面是我布局代码,下面如果,有控件需要阴影效果,可以改成相应控件再用,

package com.yinke.demon.commonres.view;

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.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;

import com.yinke.demon.commonres.R;


/**
 * @author Drchen
 * <p>
 * 自写带阴影的ConstraintLayout
 * <p>
 * 必须设置backGround 不然不进onDraw(),
 */

public class ShawdeLinearLayout extends LinearLayout {
    /**
     * 画笔,这里设置阴影
     */
    private Paint paint2;
    /**
     * 背景颜色
     */
    private int bg_color;
    /**
     * 阴影颜色
     */
    private int shawder_color;
    /**
     * 圆角
     */
    private int radius;
    /**
     * 阴影斜度x
     */
    private int offset_X;
    /**
     * 阴影斜度y
     */
    private int offset_Y;
    /**
     * 阴影宽度
     */
    private int effect;
    /**
     * 布局宽
     */
    private int with;
    /**
     * 布局高
     */
    private int higth;

    /**
     * 减去阴影后,布局实际背景绘制区域,left
     */
    private int left;
    /**
     * 减去阴影后,布局实际背景绘制区域,top
     */
    private int top;
    /**
     * 减去阴影后,布局实际背景绘制区域,right
     */
    private int right;
    /**
     * 减去阴影后,布局实际背景绘制区域,bottom
     */
    private int bottom;
    /**
     * 四边是否需要绘制
     */
    private boolean noleft, notop, noright, nobottom;


    private String TAG = "ShawderConstrainLayout";


    public ShawdeLinearLayout(Context context) {
        this(context, null);


    }

    public ShawdeLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attrs) {


        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.PublicShawderLayout);
        /**
         * 本项目的radio 不做百分比, 都用dp
         */
        radius = typedArray.getDimensionPixelSize(R.styleable.PublicShawderLayout_public_radius, 50);

        bg_color = typedArray.getColor(R.styleable.PublicShawderLayout_public_bg_color, Color.BLUE);
        shawder_color = typedArray.getColor(R.styleable.PublicShawderLayout_public_shaw_color, Color.BLACK);
        //百分比适配
        effect = typedArray.getDimensionPixelSize(R.styleable.PublicShawderLayout_public_effect, 20);

        offset_X = typedArray.getDimensionPixelOffset(R.styleable.PublicShawderLayout_public_offset_x, 20);
        offset_Y = typedArray.getDimensionPixelOffset(R.styleable.PublicShawderLayout_public_offset_y, 20);
        notop = typedArray.getBoolean(R.styleable.PublicShawderLayout_public_notop, false);
        noleft = typedArray.getBoolean(R.styleable.PublicShawderLayout_public_noleft, false);
        noright = typedArray.getBoolean(R.styleable.PublicShawderLayout_public_noright, false);
        nobottom = typedArray.getBoolean(R.styleable.PublicShawderLayout_public_nobottom, false);
        typedArray.recycle();

        paint2 = new Paint();
        paint2.setAntiAlias(true);
        // 设定颜色
        paint2.setColor(bg_color);
        // 设定阴影(柔边, X 轴位移, Y 轴位移, 阴影颜色)
//            paint2.setShadowLayer(5, 3, 3, );
        paint2.setShadowLayer(effect, offset_X, offset_Y, shawder_color);
        //这个要开,不然没有阴影
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        int padd_top;
        int padd_left;
        int padd_right;
        int padd_bottom;
        if (noleft) {
            padd_left = 0;
        } else {
            padd_left = offset_X + effect ;
        }
        if (notop) {
            padd_top = 0;
        } else {
            padd_top = offset_Y + effect ;
        }
        if (noright) {
            padd_right = 0;
        } else {
            padd_right = offset_Y +effect ;
        }

        if (nobottom) {
            padd_bottom = 0;
        } else {
            padd_bottom = offset_Y + effect ;
        }

        setPadding(padd_left, padd_top, padd_right, padd_bottom);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.with = w;
        this.higth = h;
        if (noleft) {
            left = 0;
        } else {
            left = offset_X + effect ;
        }
        if (notop) {
            top = 0;
        } else {
            top = offset_Y + effect ;
        }
        if (noright) {
            right = with;
        } else {
            right = with - offset_Y - effect ;
        }
        if (nobottom) {
            bottom = higth;
        } else {
            bottom = higth - offset_Y - effect ;
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas); //去掉减少不必要的绘制

        RectF rectF = new RectF(left, top, right, bottom);
        canvas.drawRoundRect(rectF, radius, radius, paint2);

    }


}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 7,413评论 0 17
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,737评论 1 32
  • 一、简历准备 1、个人技能 (1)自定义控件、UI设计、常用动画特效 自定义控件 ①为什么要自定义控件? Andr...
    lucas777阅读 5,398评论 2 54
  • 翻译停下来一个多月。其实翻译挺重要的。是进入阅读写作的必需阶段。所有从今天开始恢复翻译。并且要认真的对待。之前只是...
    123逍遥游阅读 325评论 0 2
  • 个人信用信息服务平台官网:https://ipcrs.pbccrc.org.cn/ 官方图文介绍:https://...
    量子波阅读 959评论 0 0

友情链接更多精彩内容