我有一个png的按钮可抽屉。但它的图像绘制有梯度,当应用9patch时看起来很糟糕(梯度和9patch不兼容)。我想用形状来做这件事。但是我不能用android图形来绘画,因为它对我来说很难理解。
你能帮我画一个有形状的图像吗?
它包含一个边框渐变,里面有一个圆角橙色矩形和120°阴影
发布于 2016-12-29 22:49:46
这就是你要的
创建您的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c8c0c0"
android:orientation="vertical">
<LinearLayout
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerInParent="true"
android:background="@drawable/my_rectangle">
</LinearLayout>
</RelativeLayout>
在可绘制文件夹中创建my_rectangle.xml文件
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#000000"
android:startColor="#FFFFFF" />
<corners android:radius="10dp" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#e95d11" />
<corners android:radius="10dp" />
<size
android:width="50dp"
android:height="50dp" />
</shape>
</item>
</layer-list>
结果
备注
干杯
发布于 2019-10-24 14:38:29
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:angle="180"
android:endColor="@color/transperent"
android:startColor="@color/quantum_orange" />
<corners android:radius="5dp" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@color/white" />
<corners android:radius="5dp" />
<size
android:width="50dp"
android:height="50dp" />
</shape>
</item>
发布于 2019-05-13 22:00:16
虽然公认的答案是正确的,但最好使用9-patch。参见Creating & Using 9-patch images in Android、https://developer.android.com/studio/write/draw9patch、How do I put a border around an Android textview?。也许SVG-images能帮上忙,但我还没有成功。同样在被接受的答案中,阴影可以每45度旋转一次,例如,你可以尝试315度而不是270度。
您可以使用9-patch工具。'Draw9patch‘工具从sdk/tools
文件夹中消失了,但它可能在这里:https://androidstudio.io/downloads/tools/download-the-latest-version-of-draw9patch.jar.html。
Simple nine-patch generator.
res
文件夹中。9.png
图像用作阴影。9-patch
比使用渐变和CardView
的XML shadows更容易使用。在Android中的阴影是可怕的。
https://stackoverflow.com/questions/41330107
复制相似问题