zoukankan      html  css  js  c++  java
  • 重写LayoutParams,读取子View自定义属性

    在EasyConstraintLayout内部定义一个静态类LayoutParams继承ConstraintLayout.LayoutParams,然后在构造方法中读取上面自定义的属性。我们通过裁剪的方式实现圆角效果,因此还有要获取子view的位置和大小。

    static class LayoutParams extends ConstraintLayout.LayoutParams
    implements EasyLayoutParams{
    private LayoutParamsData data;
    public LayoutParams(Context c, AttributeSet attrs) {
    super(c, attrs);
    data = new LayoutParamsData(c, attrs);
    }
    @Override
    public LayoutParamsData getData() {
    return data;
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public interface EasyLayoutParams {
    LayoutParamsData getData();
    }
    1
    2
    3
    public class LayoutParamsData {
    int radius;
    int shadowColor;
    int shadowDx;
    int shadowDy;
    int shadowEvaluation;

    public LayoutParamsData(Context context, AttributeSet attrs) {
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EasyLayout);
    radius = a.getDimensionPixelOffset(R.styleable.EasyLayout_layout_radius, 0);
    shadowDx = a.getDimensionPixelOffset(R.styleable.EasyLayout_layout_shadowDx, 0);
    shadowDy = a.getDimensionPixelOffset(R.styleable.EasyLayout_layout_shadowDy, 0);
    shadowColor = a.getColor(R.styleable.EasyLayout_layout_shadowColor, 0x99999999);
    shadowEvaluation = a.getDimensionPixelOffset(R.styleable.EasyLayout_layout_shadowEvaluation, 0);
    a.recycle();
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    圆角和阴影实现原理
    因为我们是通过父布局控制子view的圆角和阴影行为,所以我们重写drawChild来实现,drawChild之前,先通过paint的ShadowLayer属性把子View的阴影先画上,这个阴影需要裁剪掉子view自身的大小位置。然后再画子view,并且裁剪圆角部分,最终实现圆角阴影效果。裁剪起初我们想到的是通过canvas的clipPath方法实现,但是发现会有很大的锯齿。所以改用paint的xfermode来裁剪阴影和子view。
    --------------------- 

  • 相关阅读:
    java中 Hex(十六进制)和byte[]相互转换
    byte[]和File相互转换
    Stringboot MultiPartFile 转 File
    mysql8.0+运行报错The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. 解决办法
    idea激活
    mysql8安装成功过后navicat连接失败
    AJAX的使用
    单文件上传和多文件上传实例
    监听器的使用
    SQL分页技术
  • 原文地址:https://www.cnblogs.com/ly570/p/11284707.html
Copyright © 2011-2022 走看看