我正在尝试制作一个颜色选择器,允许用户通过简单的触摸输入来选择颜色。我的hueShade给色环提供了色调,而饱和度给出了色调 我把鳕鱼从中心掏出来。 SweepGradient不需要半径,但Radial Gradient确实需要它。但是,我在创建时调用它时无法获得宽度和高度的实际值。相反,它会获取所述的值。是否存在可在该信息可用时设置半径的中间步骤?或者我应该尝试不同的方法吗?
private Paint mPaint;
private int[] mColors;
private int viewWidth = 120;
private int viewHeight = 120;
private int centerX = 60;
private int centerY = 60;
private int padding = 10;
public ColorWheelView(Context context) {
super(context);
onInitialize();
// TODO Auto-generated constructor stub
}
private void onInitialize()
{
mColors = new int[] {
0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
0xFFFFFF00, 0xFFFF0000
};
Shader totalShade = new SweepGradient(0, 0, mColors, null);
Shader hueShade = new SweepGradient(0, 0, mColors, null);
Shader saturationShade = new RadialGradient(0, 0, (float)viewWidth-padding, 0xFFFFFFFF, 0x00FFFFFF, Shader.TileMode.CLAMP);
//Shader valueShade;
totalShade = new ComposeShader(hueShade, saturationShade, PorterDuff.Mode.SCREEN);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setShader(totalShade);
mPaint.setStyle(Paint.Style.FILL);
}
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
{
super.onSizeChanged(xNew, yNew, xOld, yOld);
viewWidth = xNew;
viewHeight = yNew;
centerX = viewWidth / 2;
centerY = viewHeight / 2;
}
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
super.onLayout(changed, left, top, right, bottom);
viewWidth = right - left;
viewHeight = bottom - top;
centerX = viewWidth / 2;
centerY = viewHeight / 2;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredWidth, measuredHeight);
}
如果它有助于弄清楚我正在实现的目标,我正在努力使这个视图可用于不同尺寸的屏幕。这就是我需要设备信息的原因。
答案 0 :(得分:0)
最好在onLayout中调用它,因为当执行任何大小或方向的更改时,通常会保证这一点。我以前没有获得所需的值,但那是因为我继承了Android.view.View类以外的基类。