drawRoundRect每个角的不同半径

时间:2016-07-03 08:05:32

标签: android

我想绘制一个不同角半径的正方形,例如顶部半径为“25px”,底部不圆。

我当前的code(所有角落都有相同的半径)

c.drawRoundRect(
tmpDrawHalf, // rect
cornersRadius, // rx
cornersRadius, // ry
paint // Paint
);

如何实现?

编辑: 我的目标: Image

2 个答案:

答案 0 :(得分:0)

您的 activity_main.xml 只需定义您的视图。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.softeng.drawshape.MainActivity">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/recta"/>


</RelativeLayout>

现在在我的案例中创建可绘制文件名recta.xml,并将其设置为ImageView的背景。

<强> recta.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <padding
        android:top="3dp"
        android:left="8dp"
        android:right="8dp"
        android:bottom="3dp"/>

    <solid android:color="#E6121A" />

    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="25dp"
        android:topRightRadius="25dp" />

</shape>

输出

enter image description here

答案 1 :(得分:0)

您可以使用drawable来制作所需的形状。 然后在视图中绘制形状。

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
            android:shape="rectangle">
            <solid android:color="#FF0000" />
            <corners android:topLeftRadius="25dp" android:topRightRadius="25dp"  />
        </shape>

然后在您的onDraw方法中。您甚至可以使用此方法通过使用选择器drawable为压缩,禁用等添加不同的状态。

Drawable d = ContextCompat.getDrawable(context, R.drawable.***)
d.setBounds(left, top, right, bottom); 
d.draw(canvas);