Android-Custom View要严格保持在布局的自定义边界内
创始人
2024-08-19 17:00:37
0次

要实现Android-Custom View严格保持在布局的自定义边界内,可以使用以下解决方法:

  1. 在Custom View的构造函数中,使用setWillNotDraw(false)来确保Custom View会被绘制。
public class CustomView extends View {
    public CustomView(Context context) {
        super(context);
        setWillNotDraw(false);
    }
}
  1. 在Custom View的onMeasure()方法中,根据布局的宽度和高度来确定Custom View的实际大小,并确保它不超出布局的边界。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int desiredWidth = ...; // 自定义宽度
    int desiredHeight = ...; // 自定义高度

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    int width, height;

    if (widthMode == MeasureSpec.EXACTLY) {
        width = widthSize;
    } else if (widthMode == MeasureSpec.AT_MOST) {
        width = Math.min(desiredWidth, widthSize);
    } else {
        width = desiredWidth;
    }

    if (heightMode == MeasureSpec.EXACTLY) {
        height = heightSize;
    } else if (heightMode == MeasureSpec.AT_MOST) {
        height = Math.min(desiredHeight, heightSize);
    } else {
        height = desiredHeight;
    }

    setMeasuredDimension(width, height);
}
  1. 在Custom View的onLayout()方法中,根据布局的边界,计算Custom View的实际位置。
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    int width = right - left;
    int height = bottom - top;

    // 计算Custom View的实际位置
    int customViewLeft = ...;
    int customViewTop = ...;
    int customViewRight = customViewLeft + width;
    int customViewBottom = customViewTop + height;

    layout(customViewLeft, customViewTop, customViewRight, customViewBottom);
}
  1. 在Custom View的onDraw()方法中,绘制Custom View的内容。
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // 在Custom View内部绘制内容
    // ...
}

通过以上步骤,Custom View就能够严格保持在布局的自定义边界内了。

相关内容

热门资讯

玩家实测辅助!元宝娱乐辅助透视... 玩家实测辅助!元宝娱乐辅助透视挂,紫金之巅是有外挂,竟然竟然有挂1、下载好元宝娱乐脚本下载之后点击打...
我来教教你开挂!微乐双扣辅助透... 我来教教你开挂!微乐双扣辅助透视挂,么么四川麻将是真的有外挂,其实有挂方针1、每一步都需要思考,不同...
有玩家发现辅助!beautyb... 有玩家发现辅助!beautybox辅助透视挂,南星游戏确实有外挂,原来有挂透明挂进入游戏-大厅左侧-...
每日必备透视挂!新大圣辅助透视... 每日必备透视挂!新大圣辅助透视挂,蜂娱棋牌2是有外挂,本来有挂技术1)蜂娱棋牌2有没有挂:进一步探索...
目前辅助!边锋运城麻将辅助透视... 目前辅助!边锋运城麻将辅助透视挂,情怀信阳是有外挂,竟然真是有挂1、玩家可以在边锋运城麻将线上大神俱...
发现玩家透视挂!博锐十三水辅助... 发现玩家透视挂!博锐十三水辅助透视挂,边锋老友棋牌真的有外挂,一直了解有挂1.边锋老友棋牌 选牌创建...
记者发布辅助!天天微友辅助透视... 记者发布辅助!天天微友辅助透视挂,KELE是有外挂,确实有挂方略运天天微友辅助工具,进入游戏界面。进...
为了进一步辅助!哈灵斗地主辅助... 为了进一步辅助!哈灵斗地主辅助透视挂,蜀乐麻将真的有外挂,果然有挂讲解哈灵斗地主脚本下载中分为三种模...
玩家必备科普辅助!新版玄龙辅助... 玩家必备科普辅助!新版玄龙辅助透视挂,星空娱乐真的有外挂,确实确实有挂1、超多福利:超高返利,海量正...
一分钟带你了解开挂!圣盛晃晃麻... 一分钟带你了解开挂!圣盛晃晃麻将辅助透视挂,情怀宜春真的有外挂,都是了解有挂1、任何圣盛晃晃麻将透视...