【发布时间】:2021-01-29 20:04:13
【问题描述】:
我创建了一个带有自定义 LinearGradient 可绘制对象的搜索栏。但是,我希望能够更改每种颜色的渐变比例,这里我使用 3 种颜色,如果 positions 是 null,它们会均匀分布。实际上,我想要的是为每种颜色提供宽度或比例,例如更改红色比例并将其设置为仅从 seekBar width 的 0% 到 10%。
在这里,我想设置 0% 到 10% 红色、10% - 80% 黄色和 80% 到 100% 红色,并且能够动态更改每种颜色的宽度值。
这可能吗,如果可以,谁能指导我如何做?
我的代码
private ShapeDrawable getSeekBarGradientDrawable(int mRectWidth, int mRectHeight) {
int[] colors = new int[] { Color.RED, Color.YELLOW, getResources().getColor(R.color.primaryButton, null)};
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels - (int) getResources().getDimension(R.dimen.margin_padding_size_medium);
Shader shader = new LinearGradient(
0,
0,
width,
mRectHeight,
colors, new float[] {0.1f,0.7f,0.2f},
Shader.TileMode.CLAMP);
ShapeDrawable shape = new ShapeDrawable(new RectShape());
shape.getPaint().setShader(shader);
return shape;
}
图像可以从当前设置中看到,它与我描述的不同。
【问题讨论】:
标签: java android material-design android-seekbar android-graphics