Java 不同dpi的径向梯度
我正在创建一个径向渐变,如:Java 不同dpi的径向梯度,java,android,gradient,Java,Android,Gradient,我正在创建一个径向渐变,如: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#7faacaFF" android:endColor="#cfe1edFF"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#7faacaFF"
android:endColor="#cfe1edFF"
android:gradientRadius="326"
android:type="radial"/>
</shape>
但在gradientRadius字段中,使用sp和dip等值返回一个错误。
有没有一种方法可以指定mdpi的半径和比例,并在其余屏幕密度中自动进行缩放,或者是创建多个可绘制文件所必需的?假设您可以将gradientRadius属性指定为浮点(猜测它意味着px)或分数
可以是一个小数,它是一个附加了%或%p的浮点数,例如“14.5%”。%后缀始终表示基本大小的百分比;可选的%p后缀提供了相对于某个父容器的大小
你试过了吗?这样就可以在布局中指定dp或任何单位的大小,并从这里引用它。但奇怪的是,大多数尺寸都是以可绘制xml格式指定为尺寸的 那么,如果您这样做: res/drawable/foo.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#7faacaFF"
android:endColor="#cfe1edFF"
android:gradientRadius="@dimen/radius"
android:type="radial"/>
</shape>
<resources ...>
...
<item name="radius" format="float" type="dimen">326</item>
....
</resources>
<resources ...>
...
<item name="radius" format="float" type="dimen">200.34</item>
....
</resources>
res/values mdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#7faacaFF"
android:endColor="#cfe1edFF"
android:gradientRadius="@dimen/radius"
android:type="radial"/>
</shape>
<resources ...>
...
<item name="radius" format="float" type="dimen">326</item>
....
</resources>
<resources ...>
...
<item name="radius" format="float" type="dimen">200.34</item>
....
</resources>
...
326
....
res/values hdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#7faacaFF"
android:endColor="#cfe1edFF"
android:gradientRadius="@dimen/radius"
android:type="radial"/>
</shape>
<resources ...>
...
<item name="radius" format="float" type="dimen">326</item>
....
</resources>
<resources ...>
...
<item name="radius" format="float" type="dimen">200.34</item>
....
</resources>
...
200.34
....
你觉得怎么样?在android:gradientRadius字段中使用百分比 例如: 对于设置为50000%p的50%无线电波
<gradient
android:angle="45"
android:centerX="0.5"
android:centerY="0.5"
android:endColor="#123123"
android:gradientRadius="50000%p"
android:startColor="#456456"
android:type="radial" />
这是一个老问题,但这个答案表明谷歌排名很高,我相信另一种方法更具可扩展性。不必为每个密度提供维度,而是从XML创建一个与密度无关的维度:
<dimen name="gradient_radius">25dp</dimen>
这将覆盖
android:gradientRadius
字段,因此您可以将其保留为0。但是,您仍然不能使用(基于屏幕或其他方式)%维度。我将其应用于视图的背景(渐变绘制已经设置好了!),但它当然也应用于ImageView的src元素。我自己也尝试过,但没有成功。这似乎是百分比把我写的东西除以100。因此,上述基本尺寸为1px。%p符号也做了同样的操作,所以这是一个失败。。。我的缺点是:|分数不适用于Kitkat(API 19)和更低的nor 50%非50%p,可提取只是不显示这是完全错误的。正如托拜厄斯在他自己回答的评论中所说,%和%p只是将提供的数字除以100。%n不应“为你工作”。您是否在不同屏幕尺寸的设备上尝试过此功能?