我正在创建一个游戏,在其中绘制从RelativeLayout
的中心x,y坐标开始的路径。我正在获取x,y坐标,但是我不知道如何使用我的getNumbers()
方法将坐标传递到路径绘图类中。我在onCreate
中有这句话:
getNumbers();
path_em = new PathEM(this, xTint, yTint);
gBoard_RL.addView(path_em);
root_RL.addView(gBoard_RL);
这是
getNumbers()
: public void getNumbers() {
gBoard_RL.getViewTreeObserver().addOnGlobalLayoutListener(NumbersoGLL);
}
OnGlobalLayoutListener NumbersoGLL = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
xRLWInt = gBoard_RL.getWidth() / 2;
yRLHInt = gBoard_RL.getHeight() / 2;
xTint = gBoard_RL.getLeft() + xRLWInt;
yTint = gBoard_RL.getTop() + yRLHInt;
}
};
我认为我必须重写getNumbers(),这不是无效的,而是返回xTint和yTint,对吗?我不知道该怎么做,因为
public void onGlobalLayout()
不能更改为传递int值,当我尝试尝试时收到一个错误消息,提示它必须为空。所以基本上,我想做的是在
onCreate
类的Game3
中,我需要获取中心坐标整数,然后将其传递给我的Path Drawing类。这是整个第三场比赛:public class Game3 extends Activity {
PathEM path_em;
RelativeLayout root_RL, gBoard_RL, startTimer_RL;
LayoutParams root_LP, startTimer_LP, gBoardInfo_LP, gBoard_LP, rootStartTimer_LP;
LayoutInflater LayInf = null;
View root_VUE, gBoardInfo_VUE;
TextView gameScore_TV, gameTime_TV, readyCD_TV;
FrameLayout root_FrameL;
int xScrInt, yScrInt, xRLWInt, yRLHInt, xTint, yTint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.cutin, R.anim.cutout);
// -- Create Root Layout
root_RL = new RelativeLayout(this);
root_LP = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
root_RL.setId(1);
root_RL.setBackgroundColor(Color.WHITE);
root_RL.setLayoutParams(root_LP);
// --- END Create Root Layout
// --- Create GameBoard
LayInf = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
gBoardInfo_VUE = LayInf.inflate(R.layout.game1_info, null);
gBoardInfo_LP = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
gBoardInfo_LP.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
gBoardInfo_VUE.setId(2);
gBoardInfo_VUE.setLayoutParams(gBoardInfo_LP);
gameTime_TV = (TextView) gBoardInfo_VUE
.findViewById(R.id.game1_timeValue2_TV);
gameScore_TV = (TextView) gBoardInfo_VUE
.findViewById(R.id.game1_scoreValue2_TV);
root_RL.addView(gBoardInfo_VUE);
gBoard_RL = new RelativeLayout(this);
gBoard_LP = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
gBoard_LP.addRule(RelativeLayout.BELOW, gBoardInfo_VUE.getId());
gBoard_LP.setMargins(10, 10, 10, 10);
gBoard_RL.setLayoutParams(gBoard_LP);
gBoard_RL.setBackgroundColor(Color.BLUE);
//--- add stuff here
getNumbers();
path_em = new PathEM(this, xTint, yTint);
gBoard_RL.addView(path_em);
root_RL.addView(gBoard_RL);
root_FrameL = new FrameLayout(this);
root_FrameL.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
root_FrameL.addView(root_RL);
setContentView(root_FrameL);
// --- END Create GameBoard
}//--- END onCreate
public void getNumbers() {
gBoard_RL.getViewTreeObserver().addOnGlobalLayoutListener(NumbersoGLL);
}
OnGlobalLayoutListener NumbersoGLL = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
xRLWInt = gBoard_RL.getWidth() / 2;
yRLHInt = gBoard_RL.getHeight() / 2;
xTint = gBoard_RL.getLeft() + xRLWInt;
yTint = gBoard_RL.getTop() + yRLHInt;
}
};
//--- PathEM Class
public class PathEM extends View {
Paint paint = new Paint();
Path path = new Path();
public PathEM(Context context, int xTint, int yTint){
super(context);
}
Pt[] thePath = { new Pt(xTint, yTint),
new Pt(200, 200),
new Pt(200, 500),
new Pt(400, 500)
};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.GREEN);
paint.setStrokeWidth(7);
paint.setStyle(Paint.Style.STROKE);
path.moveTo(thePath[0].x, thePath[0].y);
for (int i = 1; i < thePath.length; i++) {
path.lineTo(thePath[i].x, thePath[i].y);
}
canvas.drawPath(path, paint);
}// --- END onDraw
class Pt {
float x, y;
Pt(float _x, float _y) {
x = _x;
y = _y;
}
}
}
}
最佳答案
您应该将代码放在PathEM
方法中初始化onGlobalLayout()
对象的位置。
像这样:
OnGlobalLayoutListener NumbersoGLL = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
xRLWInt = gBoard_RL.getWidth() / 2;
yRLHInt = gBoard_RL.getHeight() / 2;
xTint = gBoard_RL.getLeft() + xRLWInt;
yTint = gBoard_RL.getTop() + yRLHInt;
path_em = new PathEM(this, xTint, yTint);
gBoard_RL.addView(path_em);
root_RL.addView(gBoard_RL);
}
};