Android填充与边距性能

时间:2014-12-19 19:06:51

标签: android

我正在做一些属性动画,想知道哪个更快 - 更新边距或填充。两者都通过扩展Property<T, V>来更新。

public static final Property<View, Integer> LINEAR_TOP_MARGIN_PROPERTY = new Property<View, Integer>(Integer.class, "") {
    @Override
    public Integer get(View view) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
        return params.topMargin;
    }

    @Override
    public void set(View view, Integer value) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
        params.topMargin = value;
        view.requestLayout();
    }
};

public static final Property<View, Integer> PADDING_BOTTOM_PROPERTY = new Property<View, Integer>(Integer.class, "") {
    @Override
    public Integer get(View view) {
        return view.getPaddingBottom();
    }

    @Override
    public void set(View object, Integer value) {
        object.setPadding(
                object.getPaddingLeft(),
                object.getPaddingTop(),
                object.getPaddingRight(),
                value
        );
    }
};

Padding看起来更灵活,因为它适用于任何布局而无需进行广泛的LayoutParams投射和检查。但是,并非所有视图都具有相同的填充行为。

0 个答案:

没有答案