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

标签 android shapes

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

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/

相关文章:

在 C 中创建一个网格形状(为什么要多创建一条线?)

android - 与 android :drawableBottom attribute. 结合使用时不显示可绘制形状

javascript - Angular ionic 真实简单 Controller 不工作

安卓线性布局

android - 限制从 Firebase 获取数据以执行拉动刷新和加载更多功能

android - Okhttp 在使用 volley 时显示泄漏警告

algorithm - 处理板上形状的选择

CSS淡出没有图像的水平规则/线条样式的div效果

安卓布局设计

java - 如何在android studio项目中包含共享库?