我對java編程還很陌生。現在我正在寫一個類的小平臺遊戲。我面臨的問題如下:我畫了一個男人bitmap
我的canvas
。如果你觸摸屏幕的右半邊,他應該向右移動。同樣的東西在屏幕的左側。即時通訊試圖在我單獨的「CanvasClass
」中調用onDraw
方法,該方法擴展了View Class
並每次更改繪製的bitmap
的變量。 幫我用invalidate/postInvalidate方法,我似乎無法理解它!謝謝:) GameActivity:召回/更新視圖onDraw()
public class GameActivity extends Activity implements OnTouchListener{
CanvasClass ourView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ourView = new CanvasClass(this);
setContentView(ourView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onTouch(View v, MotionEvent me) {
return false;
}
}
CanvasClass:
public class CanvasClass extends View implements OnTouchListener {
Bitmap mBackground, BackScaled,bobOri, bobScale;
int width, height,bobWidth = 33, bobHeight = 93;
float meX = 20,meY = 400;
Canvas canvas;
public CanvasClass(Context context) {
super(context);
((Activity) getContext()).requestWindowFeature(Window.FEATURE_NO_TITLE);
((Activity) getContext()).getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics displaymetrics = getResources().getDisplayMetrics();
height =displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
mBackground = BitmapFactory.decodeResource(getResources(), R.drawable.background);
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
BackScaled = Bitmap.createScaledBitmap(mBackground, width, height,true);
bobOri = BitmapFactory.decodeResource(getResources(),R.drawable.bobertfront);
bobScale = Bitmap.createScaledBitmap(bobOri, bobWidth,bobHeight, true);
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawBitmap(BackScaled,0,0,null);
canvas.drawBitmap(bobScale,20,410,null);
}
@Override
public boolean onTouch(View v, MotionEvent me) {
return true;
}
}
如果您有什麼需要,請諮詢! 我會檢查每5-10分鐘的答覆,謝謝:)