gpt4 book ai didi

android - ShapeDrawable 椭圆形(描边样式)的边缘被切割 (Android Studio)

转载 作者:行者123 更新时间:2023-11-30 00:30:38 26 4
gpt4 key购买 nike

我是 Android Studio 的新手,目前我正在尝试以编程方式创建随机颜色、笔划宽度和大小的椭圆形(不是实心,只是笔划):

        long randomSeedm = System.currentTimeMillis();
Random random = new Random(randomSeedm);
int[] color = {R.color.red, R.color.black, .....};

int ovalWidth = 20 + random.nextInt(100);
int ovalHeight = 20 + random.nextInt(100);

ShapeDrawable shape = new ShapeDrawable(new OvalShape());
shape.setIntrinsicWidth (ovalWidth);
shape.setIntrinsicHeight (ovalHeight);
shape.getPaint().setColor(getResources().getColor(color[random.nextInt(6)]));
shape.getPaint().setStyle(Paint.Style.STROKE);
shape.getPaint().setStrokeWidth(2+random.nextInt(15));
shape.getPaint().setAntiAlias(true);

ImageView oval = new ImageView(this);
oval.setImageDrawable(shape);

在我将 ImageView 放入覆盖整个屏幕的 FrameLayout 之后:

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ovalWidth, ovalHeight);
left = random.nextInt(frameLayoutWidth - ovalWidth);
top = random.nextInt(frameLayoutHeight - ovalHeight);
params.leftMargin = left;
params.topMargin = top;
params.gravity = Gravity.TOP + Gravity.LEFT;

frameLayoutWholeScreen.addView(oval, params);

结果非常令人满意(从我初学者的角度来看),唯一的问题是椭圆的边缘被部分切割了。以下是一些示例:

示例 1

[1]

示例 2

[2]

有什么方法可以避免削减?

最佳答案

在制作frame布局参数的时候需要加上stroke的值。

    int strokeSize = 2 + random.nextInt(15);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ovalWidth + strokeSize, ovalHeight + strokeSize);
left = random.nextInt(frameLayoutWidth - (ovalWidth + strokeSize));
top = random.nextInt(frameLayoutHeight - (ovalHeight + strokeSize));
params.leftMargin = left;
params.topMargin = top;
params.gravity = Gravity.TOP + Gravity.LEFT;
frameLayoutWholeScreen.addView(oval, params);

关于android - ShapeDrawable 椭圆形(描边样式)的边缘被切割 (Android Studio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44462844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com