有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android中的java将方法从Void更改为Return?

我正在创建一个游戏,其中我正在绘制一条路径,从aRelativeLayout的中心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(),这样它就不是void,而是返回xTint和yTint,对吗?我不知道该怎么做,因为public void onGlobalLayout()不能被更改为传递int,当我尝试时,我得到一个错误,说它必须是空的

所以基本上,我要做的是在Game3类的onCreate中,我需要得到中心坐标整数,然后将它们传递到我的路径绘制类中。下面是整个游戏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;
        }
    }
}
}

共 (2) 个答案

  1. # 1 楼答案

    您应该将代码放在onGlobalLayout()方法中初始化PathEM对象的地方。 像这样:

    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);
      }
    };
    
  2. # 2 楼答案

    在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;