安卓扫描渐变

标签 android android-canvas ondraw gradient

我有一个 SweepGradient 定义为

circle_paint.setShader(new SweepGradient(getWidth()/2, getHeight()/2, new int[] { circle_start_color, circle_end_color}, new float[] { 0f, 1f}))

应用于定义为

的拱门
canvas.drawArc(circle_bounds, circle_start_perc*360f, circle_end_perc*360f, true, circle_paint);

这很好用,但我需要拱门从屏幕顶部开始绘制,即

canvas.drawArc(circle_bounds, ((circle_start_perc*360f)-90f)%360, circle_end_perc*360f, true, circle_paint);

问题是 SweepGradient 似乎仍然从 0 度开始,我需要它从 270 度开始(类似于在绘制圆弧时完成的平移)。换句话说,如果我有一个从白色到蓝色的渐变,我需要将圆弧的顶部涂成白色,然后将圆弧的最后部分涂成蓝色。我该怎么做?

最佳答案

使用 Matrix.preRotate 旋转 SweepGradient 的原点:

final int[] colors = {circle_start_color, circle_end_color};
final float[] positions = {0f, 1f};
Gradient gradient = new SweepGradient(circle_bounds.centerX(), circle_bounds.centerY(), colors, positions);
float rotate = 270f;
Matrix gradientMatrix = new Matrix();        
gradientMatrix.preRotate(rotate, circle_bounds.centerX(), circle_bounds.centerY());
gradient.setLocalMatrix(gradientMatrix);
mPaint.setShader(gradient);

关于安卓扫描渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13403649/

相关文章:

android - canvas.scale() 无法正常工作

android - 我如何在 HTML5 中创建类似 Android 的登录模式?

android - 将元素与不同 ConstraintLayout 中的元素对齐

android - 在图像android的中间绘制文本

java - 为什么我的图像缩放不正确?

android - 如何在android中使用数据库数据在canvas上绘图

Android-如何画一 strip 特定参数的可变线?

java - 将 ArrayList 从 SecondActivity 传递到 MainActivity Android

android - React-native google auth android DEVELOPER_ERROR 代码 10

android - Canvas /旋转/屏幕方向