在Android中绘制自定义可绘制形状
我想在xml
布局中绘制一个自定义形状,如下所示:
I want to draw a custom shape in xml
layout like below :
我试图在XML
中做一些代码,但是我不明白如何为阴影的轮廓赋予阴影效果.
I tried to do some code in XML
but i do not understand how to give shadow effect to outline of this shadow.
我做了什么:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="20dp"
android:height="20dp"
android:gravity="top|left">
<rotate
android:fromDegrees="45"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="45">
<shape android:shape="rectangle">
<corners android:bottomRightRadius="2dp"/>
<gradient android:type="linear" android:angle="270" android:startColor="#ffffff" android:endColor="#ffffff"/>
</shape>
</rotate>
</item>
<item
android:width="120dp"
android:height="36dp"
android:bottom="6dp">
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="@color/color_white"
android:startColor="@color/color_white"
android:type="linear" />
<corners android:radius="6dp" />
</shape>
</item>
</layer-list>
我得到的结果是:
有人可以建议我如何给阴影上轮廓,如上述图片所示吗?或任何其他解决方案,例如9补丁映像.我该如何绘制9个补丁图像?
Can anyone suggest me how can i give shadow to outline as in the mentioned above picture? OR any other solution like 9 patch image. How can i draw such 9 patch image ?
更好的方法不要使用 layer-list
Even better way Don't create whole shape using layer-list
- 使用它的主要缺点是您需要给出
shape
的固定hight
和width
- The main disadvantage using that is you need to give fixed
hight
andwidth
ofshape
使用简单的方法只需使用 layer-list
创建顶部箭头,然后使用以下示例
Use Simple way just create top arrow using layer-list
and than use as below sample
尝试一下
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="10dp"
android:src="@drawable/test" />
<LinearLayout
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text=" Lorem ipsum dolor sit amet, consectetur adipiscing elit"
android:textColor="@color/colorAccent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text=" Lorem ipsum dolor sit amet, consectetur adipiscing elit"
android:textColor="@android:color/black" />
</LinearLayout>
</LinearLayout>
@ drawable/test"
@drawable/test"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:pivotX="-40%"
android:pivotY="87%"
android:toDegrees="45">
<shape android:shape="rectangle">
<stroke
android:width="10dp"
android:color="@android:color/white" />
<solid android:color="@android:color/white" />
</shape>
</rotate>
</item>
</layer-list>
输出