简体   繁体   English

在Android中,将方法从“无效”更改为“返回”?

[英]In Android change a Method from Void to Return?

I'm creating a game in which I'm drawing a path that starts at the center x,y coordinates of a RelativeLayout . 我正在创建一个游戏,在其中绘制从RelativeLayout的中心x,y坐标开始的路径。 I'm getting the x,y coordinates, but I can't figure out how to pass the coordinates into the Path Drawing Class using my getNumbers() method. 我正在获取x,y坐标,但是我不知道如何使用我的getNumbers()方法将坐标传递到路径绘图类中。 I've got this bit in the onCreate : 我在onCreate有了这一点:

getNumbers();
path_em = new PathEM(this, xTint, yTint);
gBoard_RL.addView(path_em);
root_RL.addView(gBoard_RL);

and here's getNumbers() : 这是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;   
    }
};

I think I have to rewrite getNumbers() so that's it's not void, but returns xTint and yTint, right? 我认为我必须重写getNumbers(),这不是无效的,而是返回xTint和yTint,对吗? I don't know how to do that because public void onGlobalLayout() can't be changed to pass an int, when I try I get an error saying that it HAS to be void. 我不知道该怎么做,因为无法将public void onGlobalLayout()更改为传递int,当我尝试尝试时,我收到一条错误消息,说它必须是void。

So basically, what I'm trying to do is in the onCreate of Game3 Class I need to get the center coordinate integers, then pass them into my Path Drawing Class. 所以基本上,我想做的是在Game3类的onCreate ,我需要获取中心坐标整数,然后将它们传递到我的Path Drawing类中。 Here's the entire Game 3 Class: 这是整个第三场比赛:

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

You should put the code where you initialize the PathEM object in the onGlobalLayout() Method. 您应该将代码放在onGlobalLayout()方法中初始化PathEM对象的onGlobalLayout() Like this: 像这样:

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);
  }
};

How about creating a Constants Class and setting the variable of contants variable in your onGlobalLayout() method. 如何在onGlobalLayout()方法中创建常量类并设置contants变量的变量。

That way you need not do any return. 这样,您就不需要做任何退货。

public class Constants {

public static int value1=0;
public static int value2=0;
public static int value3=0;
public static int value4=0;

}

In your onGlobalLayout() method 在您的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; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM