Java Android中的ViewGroup Only布局
我正试图画一个上面有颜料的调色板。我不太明白onLayout函数是如何工作的。用我现在的代码,我可以在一个椭圆形中画出一堆小正方形。但是我不知道如何在正方形下面画椭圆形 FingerPaint\u活动Java Android中的ViewGroup Only布局,java,android,Java,Android,我正试图画一个上面有颜料的调色板。我不太明白onLayout函数是如何工作的。用我现在的代码,我可以在一个椭圆形中画出一堆小正方形。但是我不知道如何在正方形下面画椭圆形 FingerPaint\u活动 public class FingerPaint_Activity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInsta
public class FingerPaint_Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//This organizes the layout of the different views
LinearLayout rootLayout = new LinearLayout(this);
rootLayout.setOrientation(LinearLayout.VERTICAL);
Paint_Area paintView = new Paint_Area(this);
rootLayout.addView(paintView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
//This is the paint palette object
Paint_Palette palette = new Paint_Palette(this);
rootLayout.addView(palette, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
setContentView(rootLayout);
for(int splotchIndex = 0; splotchIndex < 6; splotchIndex++)
{
View splotchView = new View(this);
splotchView.setBackgroundColor((0xFF000000) | (int)(Math.random() * 0x00FFFFFF));
palette.addView(splotchView);
}
}
}
公共类FingerPaint\u活动扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//这将组织不同视图的布局
LinearLayout rootLayout=新的LinearLayout(此);
根布局。设置方向(线性布局。垂直);
油漆区域油漆视图=新油漆区域(本);
rootLayout.addView(paintView,新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_父项,ViewGroup.LayoutParams.WRAP_内容,1));
//这是“绘制调色板”对象
绘制调色板=新的绘制调色板(本);
rootLayout.addView(调色板,新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_父项,ViewGroup.LayoutParams.WRAP_内容,1));
setContentView(rootLayout);
对于(int splotchIndex=0;splotchIndex<6;splotchIndex++)
{
视图splotchView=新视图(此视图);
splotchView.setBackgroundColor((0xFF000000)|(int)(Math.random()*0x00FFFFFF));
palete.addView(splotchView);
}
}
}
绘制调色板
public class Paint_Palette extends ViewGroup
{
public Paint_Palette(Context context)
{
super(context);
}
//Laying out the two views
//The space for the drawing and the part for the palette
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
float splotchWidth = 50.0f;
float splotchHeight = 50.0f;
RectF ovalRect = new RectF();
ovalRect.left = (float)getPaddingLeft() + splotchWidth * 0.5f;
ovalRect.top = (float)getPaddingTop() + splotchHeight * 0.5f;
ovalRect.right = (float)(getWidth() - getPaddingRight()) - splotchWidth * 0.5f;
ovalRect.bottom = (float)(getHeight() - getPaddingBottom()) - splotchHeight * 0.5f;
for(int splotchIndex = 0; splotchIndex < getChildCount(); splotchIndex++)
{
View splotchView = getChildAt(splotchIndex);
float childTheta = (float)splotchIndex / (float)getChildCount() * 2.0f * (float)Math.PI;
PointF splotchCenter = new PointF();
splotchCenter.x = ovalRect.centerX() + ovalRect.width() * 0.5f * (float)Math.cos(childTheta);
splotchCenter.y = ovalRect.centerY() + ovalRect.height() * 0.5f * (float)Math.sin(childTheta);
Rect splotcheRect = new Rect();
splotcheRect.left = (int)(splotchCenter.x - splotchWidth * 0.5f);
splotcheRect.top = (int)(splotchCenter.y - splotchHeight * 0.5f);
splotcheRect.right = (int)(splotchCenter.x + splotchWidth * 0.5f);
splotcheRect.bottom = (int)(splotchCenter.y + splotchHeight * 0.5f);
splotchView.layout(splotcheRect.left, splotcheRect.top, splotcheRect.right, splotcheRect.bottom);
}
}
//Draws the oval
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawColor(Color.LTGRAY);
Paint ovalPaint = new Paint();
RectF oval = new RectF(0,0,getWidth(),getHeight());
ovalPaint.setColor(Color.parseColor("#e1af70"));
canvas.drawOval(oval, ovalPaint);
}
}
公共类绘制调色板扩展了视图组
{
公共绘画调色板(上下文)
{
超级(上下文);
}
//提出两种观点
//图形的空间和选项板的零件
@凌驾
仅受保护的void布局(布尔值已更改、int l、int t、int r、int b)
{
浮动斑点宽度=50.0f;
浮子斑点高度=50.0f;
RectF ovalRect=新的RectF();
ovalRect.left=(float)getPaddingLeft()+splotchWidth*0.5f;
ovalRect.top=(float)getPaddingTop()+斑点高度*0.5f;
ovalRect.right=(float)(getWidth()-getPaddingRight())-splotchWidth*0.5f;
ovalRect.bottom=(float)(getHeight()-getPaddingBottom())-splotchHeight*0.5f;
对于(int splotchIndex=0;splotchIndex
有人能帮我了解一下onLayout是如何工作的,以及我如何在主椭圆形视图的顶部绘制孩子们的视图吗 您需要什么
onLayout
?贴一张你真正想要的图片,谢谢你的回复。我想出来了。我只需要在onLayout方法中添加setWillDraw(false)