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。
    --------------------- 

  • 相关阅读:
    等式
    Lemon 评测软件用法
    同花顺
    浅谈二分图的最大匹配和二分图的KM算法
    LCT总结
    5.30模拟赛
    树上斜率优化
    5.22 noip模拟赛
    KMP,HASH,Trie,AC自动机
    splay总结
  • 原文地址:https://www.cnblogs.com/ly570/p/11284707.html
Copyright © 2011-2022 走看看