我正在Android中创建我的第一款游戏。在游戏的一部分中,我正在绘制一条路径,我希望路径从RelativeLayout
的中心开始。我成功获得了中心坐标,但现在我需要将这些坐标(int xCenterInt, yCenterInt;
)传递给我的自定义视图,这就是我无法弄清楚的。除非我错误地使用Intent,否则我无法让Intents工作。那么如何将我的int值传递到我的自定义视图类?这是我在主活动中使用的方法来获取中心坐标:
public void getBoardNumbers() {
gBoard_RL.getViewTreeObserver().addOnGlobalLayoutListener(NumbersoGLL);
}
OnGlobalLayoutListener NumbersoGLL = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
xScrInt = gBoard_RL.getWidth();
yScrInt = gBoard_RL.getHeight();
xScr2Int = gBoard_RL.getWidth() / 2;
yScr2Int = gBoard_RL.getHeight() / 2;
xCenterInt = gBoard_RL.getLeft() + xScr2Int;
yCenterInt = gBoard_RL.getTop() + yScr2Int;
//---- How do I get xCenterInt, yCenterInt to my DrawPath Class?
}
};
这是我需要插入坐标的自定义视图类:
public class DrawPath extends View {
Paint paint = new Paint();
Path path = new Path();
int xC_Int, yC_Int;
//--- The coordinates Integers need to go here:
Pt[] thePath = { new Pt(xC_Int, yC_Int),
new Pt(200, 200),
new Pt(200, 500),
new Pt(400, 500) };
public DrawPath(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.RED);
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;
}
}//--- END PT
}
答案 0 :(得分:0)
您可以通过在xCenterInt
课程中为yCenterInt
和DrawPath
创建 setter - getter 来实现此目的,例如:
public class DrawPath extends View {
Paint paint = new Paint();
Path path = new Path();
int xC_Int, yC_Int;
int xCenterInt, yCenterInt; //create new variables
//your other codes
public void setxCenterInt(int setxCenterInt) {
this.setxCenterInt = setxCenterInt;
}
public int getxCenterInt() {
return setxCenterInt;
}
//do the same for yCenterInt
之后,使用我们之前创建的 setter-getter :
OnGlobalLayoutListener NumbersoGLL = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
xScrInt = gBoard_RL.getWidth();
yScrInt = gBoard_RL.getHeight();
xScr2Int = gBoard_RL.getWidth() / 2;
yScr2Int = gBoard_RL.getHeight() / 2;
xCenterInt = gBoard_RL.getLeft() + xScr2Int;
yCenterInt = gBoard_RL.getTop() + yScr2Int;
//use your DrawPath instance
myDrawPathInstance.setxCenterInt(xCenterInt);
//do the same for yCenterInt
}
};
答案 1 :(得分:0)
好的,我明白了。我更改了一些变量名称(我对其他名称感到厌烦),并且getBoardNumbers()
方法得到了修改,现在我也在动态创建方法中的游戏板布局。所以这是方法和新的DrawPath
类,现在称为PathEM
:
// -- Get GameBoard info using oGLL
public void getGameBoard() {
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);
gBoard_RL.getViewTreeObserver().addOnGlobalLayoutListener(NumbersoGLL);
//--- add stuff here
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);
}
// -- oGLL
OnGlobalLayoutListener NumbersoGLL = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
xScrInt = gBoard_RL.getWidth();
yScrInt = gBoard_RL.getHeight();
xRLWInt = gBoard_RL.getWidth() / 2;
yRLHInt = gBoard_RL.getHeight() / 2;
xTint = gBoard_RL.getLeft() + xRLWInt;
yTint = gBoard_RL.getTop() + yRLHInt;
path_em = new PathEM(getApplicationContext(), xTint, yTint);
gBoard_RL.addView(path_em);
}
};// --- END oGLL
// -- END Get GameBoard info using oGLL
//--- 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.RED);
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;
}
}// --- END PT
}//--- END PathEM Class
}