OGeek|极客世界-中国程序员成长平台

标题: android - 可以在绘制 View 之前在 View 上调用 setClipBounds 吗? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-9 06:50
标题: android - 可以在绘制 View 之前在 View 上调用 setClipBounds 吗?

我正在构建一个自定义复合控件。此复合控件的组件之一是自定义按钮。我的 ShiftingTabButton 是我的复合控件类中的一个嵌套类。

我需要将 ShiftingTabButton 的 clipBounds 设置为比第一次绘制时的实际高度略短。

在如下所示的 ShiftingTabButton 的构造函数中,我设置了一些必要的参数,然后调用 measure() 模式 UNSPECIFIED ,以确定新 View 的预期大小。当我在 Debug模式下运行它时,我可以看到 width = 160 和 height = 64 对于我的 Nexus 7。

到目前为止一切顺利。

但是当我尝试使用修改后的 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!
}



Best Answer-推荐答案


您可以轻松地复制实现以支持旧版本:

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;
    }
}

关于android - 可以在绘制 View 之前在 View 上调用 setClipBounds 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18724976/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://www.ogeek.cn/) Powered by Discuz! X3.4