渐变半径占屏幕尺寸的百分比 [英] Gradient Radius as percentage of screen size

查看:35
本文介绍了渐变半径占屏幕尺寸的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有径向渐变背景的可绘制形状,其半径将根据屏幕大小进行调整(查看相关的 文档).

I'm trying to create a shape drawable with radial gradient background, with radius that will adjust to the screen size (take a look at the relevant documentation).

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:endColor="#000"
        android:gradientRadius="50%p"
        android:startColor="#5d2456"
        android:type="radial" />
</shape>

但它似乎不起作用.如果我删除%p",它可以工作,但是半径将是静态的,因此不会调整到屏幕大小......知道出了什么问题吗?

But it doens't seem to work. if I remove the "%p", it works, but then the radius will be static, thus not adjusting to the screen size...Any idea what's wrong?

推荐答案

根据我的测试,% 确实有效,但与您预期的不一样.
首先

From what I´ve tested, the % does work, but not as you expected.
First of all

android:gradientRadius="50"

似乎取值为像素 50px

seems to take the value as pixels 50px

android:gradientRadius="50%"

转换为 50% = 0.5 px,试试

is converted as if 50% = 0.5 px, try

android:gradientRadius="5000%"

你会看到一个 50 像素的半径.
使用 %p 有类似的结果.显然这是我希望将来会改变的东西,因为它没有多大用处.通常 XML ShapeDrawable 资源会根据某个外部容器调整其大小,在这种情况下,gradientRadius 设置大小与容器无关.

and you will see a 50px radius.
Using %p has a similar result. Obviously this is something I hope will be changed in the future, because it does not have much use as it is. Usually XML ShapeDrawable resources adapt their size to some external container, in this case gradientRadius is setting the size regardless of the container.

这篇关于渐变半径占屏幕尺寸的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆