[英]In Android, when is an Interface called?
這是我第一次嘗試Android游戲。 我需要繪制一條從屏幕中心開始的路徑,並使用OnGlobalLayoutListener
獲取屏幕中心坐標。 為了將中心坐標傳遞給View類,我使用了Interface
。 現在,假設接口正在工作,則傳遞0,0,因為繪制的路徑從我的相對布局的左上方開始。 我得到正確的坐標,因為現在我將它們傳遞給TextView
以便可以看到坐標,但是它們沒有傳遞給View類,因為路徑從左上角開始,而不是我的中心相對布局。 所以我想我的問題是: 接口是否在onCreate
完成之前被觸發? 如果是這樣,如何在觸發界面之前將中間屏幕數據獲取到界面? 獲取屏幕數據后可以以某種方式觸發接口嗎? 這是我正在使用的類,首先是Main Activity:
public class Game1 extends Activity implements GBoardValueSetter {
DrawPath dp;
TextView gameScore_TV, gameTime_TV;
private CountDownTimer mainGameCD;
private boolean mainGameTimerHasStarted = false;
private final long mainCDTime = 30 * 1000;
private final long mainCDInterval = 1 * 1000;
// ---GameBoard variables
RelativeLayout root_RL, gBoardInfo_RL, gBoard_RL;
LayoutInflater LayInf = null;
View root_VUE, gBoardInfo_VUE;
LayoutParams root_LP, gBoardInfo_LP, gBoard_LP;
int xScr2Int, yScr2Int, xScrInt, yScrInt, xCenterInt, yCenterInt;
String xCenterStr, yCenterStr, xHStr, yWStr;
float xCenter_FL, yCenter_FL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.cutin, R.anim.cutout);
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);
setContentView(root_RL);
createBlankGBoard();
addDP();
}// --- END onCreate
//--- Interface call
@Override
public void passGBCenterIntValuesInterface(int xC_IInt, int yC_IInt) {
xC_IInt = xCenterInt;
yC_IInt = yCenterInt;
dp.setCenterInt(xC_IInt, yC_IInt);
}//--- END Interface call
public void createBlankGBoard(){
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.MAGENTA);
root_RL.addView(gBoard_RL);
getGameBoardNumbers();
}
public void addDP(){
root_RL.removeView(gBoard_RL);
dp = new DrawPath(this);
gBoard_RL.setBackgroundColor(Color.CYAN);
gBoard_RL.addView(dp);
root_RL.addView(gBoard_RL);
}
//-- Get GameBoard info using oGLL
public void getGameBoardNumbers(){
gBoard_RL.getViewTreeObserver().addOnGlobalLayoutListener(
NumbersoGLL);
}
//-- oGLL
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;
xHStr = String.valueOf(xScrInt);
yWStr = String.valueOf(yScrInt);
xCenter_FL = (float)xCenterInt;
yCenter_FL = (float)yCenterInt;
String xCInt_Str = String.valueOf(xCenterInt);
String yCInt_Str = String.valueOf(yCenterInt);
gameScore_TV.setText(xCInt_Str + ", " + yCInt_Str);
}
};
}
這是View類:
public class DrawPath extends View {
Paint paint = new Paint();
Path path = new Path();
private int xC_Int, yC_Int;
class Pt {
float x, y;
Pt(float _x, float _y) {
x = _x;
y = _y;
}
}
public void setCenterInt(int xC_IInt, int yC_IInt){
this.xC_Int = xC_IInt;
this.yC_Int = yC_IInt;
}
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);
}
}
和接口類:
public interface GBoardValueSetter {
void passGBCenterIntValuesInterface(int xC_IInt, int yC_IInt);
}
在對活動的接口調用中,分配從調用中獲取的參數:
public void passGBCenterIntValuesInterface(int xC_IInt, int yC_IInt) {
xC_IInt = xCenterInt; <-- RIGHT THERE
yC_IInt = yCenterInt; <-- RIGHT THERE
dp.setCenterInt(xC_IInt, yC_IInt);
}//--- END Interface call
xCenterInt和yCenterInt可能為0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.