我正在创建一个游戏,我正在绘制一条从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
类我需要获取中心坐标整数,然后将它们传递到我的路径绘图类中。这是整个第3场比赛:
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;
}
}
}
}
答案 0 :(得分:2)
您应该将代码放在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);
}
};
答案 1 :(得分:0)
如何在onGlobalLayout()方法中创建一个Constants类并设置contants变量的变量。
这样你就不需要做任何回报了。
public class Constants {
public static int value1=0;
public static int value2=0;
public static int value3=0;
public static int value4=0;
}
在你的onGlobalLayout()方法
中Constants.value1= gBoard_RL.getWidth() / 2;
Constants.value2= gBoard_RL.getHeight() / 2;
Constants.value3= gBoard_RL.getLeft() + xRLWInt;
Constants.value4= gBoard_RL.getTop() + yRLHInt;