sha*_*ylh 34 android radial-gradients android-xml shapedrawable
我正在尝试使用径向渐变背景创建一个可绘制的形状,半径将根据屏幕大小进行调整(请参阅相关文档).
这是我的代码:
<?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>
Run Code Online (Sandbox Code Playgroud)
但它似乎没有用.如果我删除"%p",它可以工作,但是半径将是静态的,因此不会调整到屏幕尺寸......任何想法有什么问题?
ilo*_*mbo 12
根据我的测试,它%确实有效,但不如你预期的那样.
首先
android:gradientRadius="50"
Run Code Online (Sandbox Code Playgroud)
似乎将值作为像素50px
android:gradientRadius="50%"
Run Code Online (Sandbox Code Playgroud)
被转换为好像50%= 0.5 px,试试
android:gradientRadius="5000%"
Run Code Online (Sandbox Code Playgroud)
你会看到一个50像素的半径.
使用%p有类似的结果.显然,这是我希望将来会改变的,因为它没有太多用处.通常,XML ShapeDrawable资源会将其大小调整为某个外部容器,在这种情况下gradientRadius,无论容器如何都要设置大小.
我最终使用以下覆盖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);
}
Run Code Online (Sandbox Code Playgroud)
在您的布局中添加视图:
<yourpackage.GradientView
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
当然,可以为View创建属性,例如.颜色,允许通过XML自定义的百分比.
请注意,gradientRadius百分比在Lollipop中有效.但是如果你必须支持pre-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);
}
}
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
遗憾的是,您无法从自定义类创建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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24045 次 |
| 最近记录: |