Android 渐变半径占屏幕尺寸的百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11176590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gradient Radius as percentage of screen size
提问by shaylh
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).
我正在尝试创建一个具有径向渐变背景的可绘制形状,其半径将根据屏幕大小进行调整(查看相关文档)。
This is my code:
这是我的代码:
<?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>
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?
但它似乎不起作用。如果我删除“%p”,它会起作用,但是半径将是静态的,因此不会调整到屏幕大小......知道出了什么问题吗?
回答by ilomambo
From what I′ve tested, the %
does work, but not as you expected.
First of all
从我的测试来看,%
确实有效,但不像你预期的那样。
首先
android:gradientRadius="50"
seems to take the value as pixels 50px
似乎将值作为像素 50px
android:gradientRadius="50%"
is converted as if 50% = 0.5 px, try
转换为 50% = 0.5 px,试试
android:gradientRadius="5000%"
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.
你会看到一个 50px 的半径。
使用%p
也有类似的结果。显然这是我希望将来会改变的东西,因为它没有多大用处。通常 XML ShapeDrawable 资源将它们的大小调整到一些外部容器,在这种情况下gradientRadius
设置大小而不考虑容器。
回答by marnaish
I ended up creating a custom View with following overriden onDraw method:
我最终使用以下覆盖的 onDraw 方法创建了一个自定义视图:
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
int maxSize = Math.max(getHeight(), getWidth());
RadialGradient gradient = new RadialGradient(
getWidth()/2,
getHeight()/2,
maxSize,
new int[] {Color.RED, Color.BLACK},
new float[] {0, 1},
android.graphics.Shader.TileMode.CLAMP);
paint.setDither(true);
paint.setShader(gradient);
canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
}
In your layout just add the View:
在您的布局中,只需添加视图:
<yourpackage.GradientView
android:layout_width="match_parent"
android:layout_height="match_parent" />
Of course it would be possible to create attributes for the View, eg. color, percentage to allow customization via XML.
当然,可以为视图创建属性,例如。颜色,百分比以允许通过 XML 进行自定义。
回答by miguel
Note that gradientRadius percentages do work in Lollipop. But if you have to support pre-Lollipop I expanded upon @marnaish's answer adding XML attributes. My gradientRadius is defined as a percentage of the parent view's width:
请注意,gradientRadius 百分比在 Lollipop 中确实有效。但是,如果您必须支持前棒棒糖,我扩展了@marnaish 的答案,添加了 XML 属性。我的 gradientRadius 被定义为父视图宽度的百分比:
public class RadialGradientView extends View {
private final int endColor;
private final int startColor;
private final float gradientRadiusWidthPercent;
private final float centerY;
private final float centerX;
private Paint paint;
public RadialGradientView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RadialGradientView, 0, 0);
startColor = a.getColor(R.styleable.RadialGradientView_startColor, Color.RED);
endColor = a.getColor(R.styleable.RadialGradientView_endColor, Color.BLACK);
gradientRadiusWidthPercent = a.getFloat(R.styleable.RadialGradientView_gradientRadiusWidthPercent, 1);
centerX = a.getFloat(R.styleable.RadialGradientView_centerX, .5f);
centerY = a.getFloat(R.styleable.RadialGradientView_centerY, .5f);
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
RadialGradient gradient = new RadialGradient(
parentWidth*centerX,
parentHeight*centerY,
parentWidth*gradientRadiusWidthPercent,
new int[] {startColor, endColor},
null,
android.graphics.Shader.TileMode.CLAMP);
paint.setDither(true);
paint.setShader(gradient);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
}
}
In attrs.xml:
在 attrs.xml 中:
<declare-styleable name="RadialGradientView">
<attr name="startColor" format="color|reference"/>
<attr name="endColor" format="color|reference"/>
<attr name="gradientRadiusWidthPercent" format="float"/>
<attr name="centerX" format="float"/>
<attr name="centerY" format="float"/>
</declare-styleable>
Unfortunately you can't create an XML drawable from a custom class, so you can't set it as a View's android:background. The workaround is to use a FrameLayout to layer it as the background.
不幸的是,您无法从自定义类创建 XML drawable,因此您无法将其设置为 View 的 android:background。解决方法是使用 FrameLayout 将其分层为背景。
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp">
<com.RadialGradientView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:centerX=".3"
app:centerY=".5"
app:endColor="#0f0"
app:startColor="#f00"
app:gradientRadiusWidthPercent=".5"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="What's up world?"/>
</FrameLayout>
回答by Adobels
It's impossible to set a shape corner radius expressed in percentage in a drawable xml.
在可绘制的 xml 中设置以百分比表示的形状角半径是不可能的。
回答by Josh
You could create a shape drawable at runtime similiar to this post https://stackoverflow.com/a/4943888/604504
您可以在运行时创建一个类似于这篇文章的可绘制形状https://stackoverflow.com/a/4943888/604504
This way you could calculate the percentage after retrieving the screen size.
这样您就可以在检索屏幕尺寸后计算百分比。