绘制背景形状有一个角落,两个切削刃 - 机器人 [英] Drawing background shape with one corner and two cutting edges - Android

查看:197
本文介绍了绘制背景形状有一个角落,两个切削刃 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想画的形状将其设置为背景。形状具有一个角部和两个切削刃。

I want to draw a shape to set it as background. the shape has one corner and two cutting edges.

下面是欲与一个圆角和两个弯道加入与直线形状的粗糙图。我使用,并绘制它。你能在这方面的帮助?

Here is the rough diagram of the shape I want with one round corner and two corners joined with straight line. I am using and to draw it. Could you help on this ?

推荐答案

一个9补丁位图(按UDI的答案)可能是最容易的,但如果你想做到这一点在code,创建一个自定义形状

A 9-patch bitmap (as per UDI's answer) is probably the easiest, but if you want to do it in code, create a custom Shape:

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为背景绘制对象:

and then use the custom Shape in a ShapeDrawable as the background Drawable:

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

它看起来像:

Which looks something like:

这篇关于绘制背景形状有一个角落,两个切削刃 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆