2016-07-03 33 views
0

我想繪製一個帶有不同角半徑的正方形,例如頂部有一個「25px」半徑,底部不是圓角。drawRoundRect每個角的不同半徑

我現在code(所有的角落裏有相同的半徑)

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

如何實現這一目標?

編輯: 我的目標: Image

+1

使用'android.graphics.Path' – pskink

+0

您還可以使用圖層列表繪製,使形狀,只是畫在您的視圖中繪製對象。 – Ali

+0

@Ali圖層列表是否可繪製?做什麼的?如果你想使用drawable,那麼只需使用'GradientDrawable' /'PaintDrawable'及其方法'setCornerRadii' – pskink

回答

0

您可以使用一個可繪製,使你想要的形狀。 然後在視圖中繪製形狀。

<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); 
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