• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-9 06:50:59 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap