我想绘制一个带有渐变背景的图像。我已经创建了一个可绘制的位图资源,如下所示
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/bg"
android:tileMode="repeat"
android:gravity="bottom"
/>
如您所知,定义 tileMode 时会忽略重力。我尝试创建我自己的继承自 BitmapDrawable 的类,但我无法成功。 请帮我应用两个属性:tileMode 和 gravity。
最佳答案
我没看清楚问题。这里的目标是什么?
是否在 tileMode 中使用渐变图像(如 .png .jpg)?
还是使用透明图标并使其背景渐变?
另外,为什么在使用 tileMode 时需要重力? tileMode 表示用同一图像平铺位图的整个区域。因为它将对整个图像执行此操作,所以没有必要在该图像上施加重力,因为所有图像都将被填充。
如果目标是在屏幕的某个部分使用此可绘制位图,请将其添加到您的布局中:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/mydrawablebitmap"
android:id="@+id/imageview1" />
您可以将其放入您的 layout.xml 并设置您自己的宽度和高度,并根据需要对齐。
如果目标是使用图像并在此处制作其背景是您的处理方式:
在您的可绘制文件夹中创建一个 gradient.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#E0E0E0"
android:endColor="#FFFFFF"
android:angle="-90"/>
</shape>
然后将此 ImageView 添加到您的布局中:
<ImageView
android:id="@+id/myimagewithgradientbackground"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="@drawable/gradient"
android:src="@drawable/foregroundimage" />
如果你给我一些关于这个问题的更多细节,我会继续并相应地编辑我的答案。
关于android - 绘制底部对齐的平铺位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12801189/