使用一个角和两个切割边绘制背景形状-Android
我想画一个形状作为背景。该形状有一个角和两个切削刃使用一个角和两个切割边绘制背景形状-Android,android,shape,Android,Shape,我想画一个形状作为背景。该形状有一个角和两个切削刃 这是我想要的形状的大致示意图,一个圆角和两个用直线连接的角。我正在用和画它。你能帮个忙吗?我会说使用photoshop获得正确的外观并将其用作png可绘制的我会说使用photoshop获得正确的外观并将其用作png可绘制的在ShapedRavables中没有像你所建议的那样切割正方形角落的工具。有一个“半径”组件 您可以尝试创建多个图像,并使用LayeredList Drawable将它们堆叠在彼此的顶部,但这可能很复杂,并且肯定会导致over
这是我想要的形状的大致示意图,一个圆角和两个用直线连接的角。我正在用和画它。你能帮个忙吗?我会说使用photoshop获得正确的外观并将其用作png可绘制的我会说使用photoshop获得正确的外观并将其用作png可绘制的在ShapedRavables中没有像你所建议的那样切割正方形角落的工具。有一个“半径”组件 您可以尝试创建多个图像,并使用LayeredList Drawable将它们堆叠在彼此的顶部,但这可能很复杂,并且肯定会导致overdraw,即绘制性能差
您的另一种选择是使用Paint API创建您想要的任何图像,然后可以将其缓存并使用。ShapeDrawables中没有像您建议的那样用于切割正方形的角的工具。有一个“半径”组件
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>
您可以尝试创建多个图像,并使用LayeredList Drawable将它们堆叠在彼此的顶部,但这可能很复杂,并且肯定会导致overdraw,即绘制性能差
您的另一种选择是使用Paint API创建您想要的任何图像,然后可以缓存这些图像,然后使用它们。根据UDI的回答,9块位图可能是最简单的,但是如果您想在代码中实现这一点,请创建自定义形状:
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>
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中的自定义形状作为背景可绘制形状:
view.setBackground(new ShapeDrawable(new WeirdShape()));
看起来像:
根据UDI的答案,9块位图可能是最简单的,但如果您想在代码中实现,请创建一个自定义形状:
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中的自定义形状作为背景可绘制形状:
view.setBackground(new ShapeDrawable(new WeirdShape()));
看起来像:
谢谢你的回复。我必须将此形状与其他形状集成,并且我已经为它们编写了代码。这就是为什么我不想使用png。谢谢你的回复。我必须将此形状与其他形状集成,并且我已经为它们编写了代码。这就是为什么我想避免使用png。我有两个视图,一个是背景视图,另一个是圆形视图。我想剪切前景视图的一个角,这样背景视图就可以从那个角看到。我在我的视图上应用了你的代码。我如何设置背景颜色来塑造你创建的形状?我如何增加剪切的长度角点?将Paint.Style.STROKE更改为Paint.Style.FILL_和_STROKE将填充图像,并且角点常量设置角点的长度。如果这不符合您的要求,您将不得不发布一个新问题。我有两个视图,一个是背景视图,另一个是圆形视图。我希望剪切前景视图的一个角,以便从该角可以看到背景视图。我在视图上应用了您的代码。我如何设置背景颜色来塑造您创建的形状?我如何增加切割角的长度?将Paint.Style.STROKE更改为Paint.Style.FILL_和_STROKE将填充图像,并且角常数设置角的长度。如果这不符合你的要求,你就必须提出一个新的问题。这会使你的问题变得更加全面。OP希望顶部的拐角是锐利的切口,而不是圆角。这会使所有的拐角都圆角。OP希望顶角是锐利的切割,而不是圆角。