绘制背景形状与一个角落和两个切割边缘 - Android

时间:2014-09-15 13:53:46

标签: android shape

我想绘制一个形状以将其设置为背景。形状有一个角和两个切削刃。

shape

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

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

ShapeDrawables中没有任何工具可以像你提议的那样切割正方形的角落。有一个半径'成分

您可以尝试创建多个图像并将它们堆叠在一起(使用LayeredList Drawable),但这可能很复杂,并且肯定会导致过度绘制(即绘图性能不佳)。

您的另一种选择是使用Paint API创建您想要的任何图像,然后可以对其进行缓存和使用。

答案 2 :(得分:0)

put this in drawable like rounded_edittext.xml -->

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
     <solid android:color="#FFFFFF"/>
        <corners
         android:bottomRightRadius="0dp"
         android:bottomLeftRadius="15dp"
      android:topLeftRadius="10dp"
      android:topRightRadius="5dp"/>
    </shape>


call drawable as edittext background
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <EditText  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:padding="5dip"
        android:background="@drawable/rounded_edittext" />
    </LinearLayout>

答案 3 :(得分:-1)

我会说使用photoshop来获得正确的方面并将其用作png drawable