在视图绘制之前可以调用setClipBounds吗?

3
我正在构建一个自定义复合控件。这个复合控件的一个组件是定制的按钮。我的ShiftingTabButton是我复合控件类中的嵌套类。
我需要在第一次绘制时将ShiftingTabButton的clipBounds设置为略短于实际高度。
在下面展示的ShiftingTabButton的构造函数中,我设置了一些必要的参数,然后使用UNSPECIFIED模式调用measure()来确定新视图的预期大小。当我以调试模式运行时,可以看到对于我的Nexus 7,width=160和height=64。
目前一切正常。
但是当我尝试使用修改后的clipBounds调用setClipBounds()时,应用程序会崩溃。
我尝试在构造函数中定义一个useShorterClipBounds标志,将clipBounds的设置移动到onDraw()方法中并使其依赖于标志检查,但我仍然遇到相同的崩溃。
public ShiftingTabButton(Context context, String string) {
    super(context);

    // parameter setup
    this.setText(string);
    this.setBackground(getResources().getDrawable(R.drawable.tab_unselected));
    this.setId(generateChildViewId());
    this.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
    this.setPadding(dpToPx(7), 0, dpToPx(7), 0);

    // measure the view     
    int widthMeasureSpec = MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, MeasureSpec.UNSPECIFIED);
    int heightMeasureSpec = MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, MeasureSpec.UNSPECIFIED);
    this.measure(widthMeasureSpec, heightMeasureSpec);
    int width = this.getMeasuredWidth();
    int height = this.getMeasuredHeight();

    // adjust the clipBounds        
    Rect clipBounds = new Rect();
    clipBounds.set(0, 0, width, height - dpToPx(16));
    this.setClipBounds(clipBounds); // !App crashes executing this line of code!
}
2个回答

8

你可以很容易地将实现复制一份来支持更早版本:

private Rect mClipBounds;

@Override
public void draw(Canvas canvas) {
    // Clip bounds implementation for JB_MR1 and older
    // Does not save the canvas, because the View implementation also doesn't...
    if (mClipBounds != null) {
        canvas.clipRect(mClipBounds);
    }
    super.draw(canvas);
}

@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void setClipBounds(Rect clipBounds) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        super.setClipBounds(clipBounds);
        return;
    }

    if (clipBounds != null) {
        if (clipBounds.equals(mClipBounds)) {
            return;
        }
        if (mClipBounds == null) {
            invalidate();
            mClipBounds = new Rect(clipBounds);
        } else {
            invalidate(Math.min(mClipBounds.left, clipBounds.left),
                    Math.min(mClipBounds.top, clipBounds.top),
                    Math.max(mClipBounds.right, clipBounds.right),
                    Math.max(mClipBounds.bottom, clipBounds.bottom));
            mClipBounds.set(clipBounds);
        }
    } else {
        if (mClipBounds != null) {
            invalidate();
            mClipBounds = null;
        }
    }
}

@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public Rect getClipBounds() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        return super.getClipBounds();
    } else {
        return (mClipBounds != null) ? new Rect(mClipBounds) : null;
    }
}

好主意...从源代码中获取新方法的实现。我喜欢它。 - Sound Conception

2

我发现setClipBounds()方法只在SDK的4.3 r2.1版本中添加,而Nexus 7运行的是4.2.2版本,因此找不到该方法。

相反地,在onDraw()方法中应直接在画布上进行裁剪。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接