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就能够严格保持在布局的自定义边界内了。

相关内容

热门资讯

wpk辅助挂!wpk微扑克智能... 《wpk辅助挂软件透明挂》是一款多人竞技的wpk辅助挂辅助透视游戏,你将微扑克对手来到同一个战场,为...
德州AI智能辅助机器人!德扑分... 德州AI智能辅助机器人!德扑分析软件,线上wpk德州好像有挂(详细外 挂教程)1、实时德州AI智能辅...
wpk透视辅助!WPk果真真的... 您好,WPK这款游戏可以开挂的,确实是有挂的,需要了解加微【757446909】很多玩家在这款游戏中...
AI教程!wepooke系统规... AI教程!wepooke系统规律(wepoke辅助技巧)wEPoke(都是真的有挂);(需添加指定薇...
德州ai辅助!德州ai辅助神器... 德州ai辅助!德州ai辅助神器软件,德扑平台其实真的是有挂(详细ai人工智能教程)是一款可以让一直输...
aa扑克辅助!wpk真的有挂,... aa扑克辅助!wpk真的有挂,德州aa poker有外挂(详细辅助挂教程);aa扑克辅助是一款益智类...
微扑克ai辅助!wpk发牌规律... 微扑克ai辅助!wpk发牌规律(Wpk)一般真的有挂-详细透视教程1、点击下载安装,微扑克插件透视分...
wepoke模拟器!wepok... wepoke模拟器!wepoke机制,wepoke有玄学,线上教程(有挂解密);超受欢迎的wepok...
德扑数据软件!来玩德州app有... 德扑数据软件!来玩德州app有挂,德扑之星总是有挂(详细助手教程)1、超多福利:超高返利,海量正版游...
微扑克ai辅助!wpk德州胜利... 微扑克ai辅助!wpk德州胜利跟号有关,wpK果真真的有挂,详细教程,(有挂解说)1、微扑克透视辅助...