从图像创建 XML
Create an XML from an Image
您好,我有以下图片作为背景:
我想知道是否可以创建一个 xml 文件来替换它,所以我的想法是使用从某个位置到顶部和其余位置的渐变只是纯色(黑色)......这可能吗?提前致谢!
编辑:
首先感谢您的帮助。更具体地说我的问题,用 xml 文件替换此图像的目的是使 https://github.com/christophesmet/android_maskable_layout
的 MaskableFrameLayout 更快
所以我的代码示例如下:
<com.christophesmet.android.views.maskableframelayout.MaskFrameLayout
android:id="@+id/lytMask"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/gradient_mask"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recAlphaMask"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
我想在列表滚动时屏蔽它的顶部,这个例子工作正常,但如果我使用图像,它的速度很慢,性能很差,也许如果我用 xml 会更快。
假设您的根视图是 LinearLayout
,您可以将背景设置为 @drawable/gradient_background
,其中 gradient_background.xml
看起来像:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#474946"
android:endColor="#181818"
android:centerY="0.1"/>
<corners android:radius="5dp" />
</shape>
其中startColor
是起始颜色,endColor
是结束颜色,centerY
是颜色合并的位置(取值在0到1.0之间)
您可以尝试其中的任何一个,也可以在线生成您自己的。有一个在线生成渐变的工具:- http://angrytools.com/gradient/
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:type="linear"
android:centerX="11%"
android:startColor="#FF8C8C8C"
android:centerColor="#FF8C8C8C"
android:endColor="#FF000000"
android:angle="270"/>
</shape>
-------------------------------------------------
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:type="radial"
android:centerX="50%"
android:centerY="50%"
android:startColor="#FF8C8C8C"
android:centerColor="#FF8C8C8C"
android:endColor="#FF000000"
android:gradientRadius="11"/>
</shape>
下面是我用来生成上面的设置的屏幕截图 xml
您好,我有以下图片作为背景:
编辑:
首先感谢您的帮助。更具体地说我的问题,用 xml 文件替换此图像的目的是使 https://github.com/christophesmet/android_maskable_layout
的 MaskableFrameLayout 更快所以我的代码示例如下:
<com.christophesmet.android.views.maskableframelayout.MaskFrameLayout
android:id="@+id/lytMask"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/gradient_mask"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recAlphaMask"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
我想在列表滚动时屏蔽它的顶部,这个例子工作正常,但如果我使用图像,它的速度很慢,性能很差,也许如果我用 xml 会更快。
假设您的根视图是 LinearLayout
,您可以将背景设置为 @drawable/gradient_background
,其中 gradient_background.xml
看起来像:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#474946"
android:endColor="#181818"
android:centerY="0.1"/>
<corners android:radius="5dp" />
</shape>
其中startColor
是起始颜色,endColor
是结束颜色,centerY
是颜色合并的位置(取值在0到1.0之间)
您可以尝试其中的任何一个,也可以在线生成您自己的。有一个在线生成渐变的工具:- http://angrytools.com/gradient/
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:type="linear"
android:centerX="11%"
android:startColor="#FF8C8C8C"
android:centerColor="#FF8C8C8C"
android:endColor="#FF000000"
android:angle="270"/>
</shape>
-------------------------------------------------
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:type="radial"
android:centerX="50%"
android:centerY="50%"
android:startColor="#FF8C8C8C"
android:centerColor="#FF8C8C8C"
android:endColor="#FF000000"
android:gradientRadius="11"/>
</shape>
下面是我用来生成上面的设置的屏幕截图 xml