gpt4 book ai didi

android - 用一个角和两个切割边绘制背景形状 - Android

转载 作者:行者123 更新时间:2023-11-29 15:14:39 26 4
gpt4 key购买 nike

我想画一个形状作为背景。该形状有一个角和两个切削刃。

shape

这是我想要的形状的粗略图,一个圆角和两个用直线连接的角。我正在使用并绘制它。你能帮忙吗?

最佳答案

9 补丁位图(根据 UDI 的回答)可能是最简单的,但如果您想在代码中执行此操作,请创建自定义形状:

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.shapes.Shape;
import android.graphics.RectF;

public class WeirdShape extends Shape {
private static final int COLOUR = Color.RED;
private static final float STROKE_WIDTH = 1.0f;
private static final float CORNER = 10.0f;

private final Paint border = new Paint();
private final Path path;

public WeirdShape() {
path = new Path();

border.setColor (COLOUR);
border.setStyle (Paint.Style.STROKE);
border.setStrokeWidth(STROKE_WIDTH);
border.setAntiAlias (true);
border.setDither (true);
border.setStrokeJoin (Paint.Join.ROUND);
border.setStrokeCap (Paint.Cap.ROUND);
}

@Override
protected void onResize(float width, float height) {
super.onResize(width, height);

float dx = STROKE_WIDTH/2.0f;
float dy = STROKE_WIDTH/2.0f;
float x = dx;
float y = dy;
float w = width - dx;
float h = height - dy;

RectF arc = new RectF(x,h-2*CORNER,x+2*CORNER,h);

path.reset();
path.moveTo(x + CORNER,y);
path.lineTo(w - CORNER,y);
path.lineTo(w,y + CORNER);
path.lineTo(w, h);
path.lineTo(x + CORNER,h);
path.arcTo (arc,90.0f,90.0f);
path.lineTo(dx,h - CORNER);
path.lineTo(dx,y + CORNER);
path.close();
}

@Override
public void draw(Canvas canvas, Paint paint) {
canvas.drawPath(path,border);
}
}

然后使用ShapeDrawable中的自定义Shape作为背景Drawable:

view.setBackground(new ShapeDrawable(new WeirdShape()));

看起来像:

enter image description here

关于android - 用一个角和两个切割边绘制背景形状 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25849729/

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