android - setClipBounds 可以在 View 绘制之前调用吗?

标签 android android-custom-view

我正在构建自定义复合控件。此复合控件的组件之一是自定义按钮。我的 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!
}

最佳答案

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

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 - setClipBounds 可以在 View 绘制之前调用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18724976/

相关文章:

android - 在某些 subview 前面绘制

java - 如何更改 Activity 上的绘制线颜色

android - 通过调试或发布过滤 androidDependency

Android:自定义 View 缩放/捏合

android - 如何同时拖动两个 View

Android Java.DateUtils 如何改变日期格式?

android - 带有自定义 View 的 ActionBar 选项卡未居中

android - 如何移动 Canvas 上绘制的 Path 对象?

java - URL 连接超时后导致应用程序崩溃

android - 如何在android中显示垂直线