-----------------------------main.java-----------------------------
package com.example.hh;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends ActionBarActivity {
//定义飞机的移动速度
private int speed = 12;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main); //这句话是不要的,看下面第8行
//去掉窗口标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//从string资源中获取指定字符串资源,并设置为该窗口的标题
//getWindow().setTitle(getResources().getText(R.string.hello_world));
//全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//创建PlaneView组件
final PlaneView planeView = new PlaneView(this);
setContentView(planeView);
planeView.setBackgroundResource(R.drawable.back);
//获取窗口管理器
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
//获得屏幕宽和高
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
//设置飞机的初始位置
planeView.currentX = screenWidth /2;
planeView.currentY = screenHeight - 80;
//为draw组件键盘事件绑定监听器
planeView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (event.getKeyCode()) {
//控制飞机下移
case KeyEvent.KEYCODE_DPAD_DOWN:
planeView.currentY += speed;
break;
//控制飞机上移
case KeyEvent.KEYCODE_DPAD_UP:
planeView.currentY -= speed;
break;
//控制飞机左移
case KeyEvent.KEYCODE_DPAD_LEFT:
planeView.currentX -= speed;
break;
//控制飞机右移
case KeyEvent.KEYCODE_DPAD_RIGHT:
planeView.currentX += speed;
break;
}
//通知planeView组件重绘
planeView.invalidate();
return true;
}
});
}
@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 onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
------------------------------------PlaneView.java----------------------------------
package com.example.hh;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
public class PlaneView extends View {
public float currentX = 0;
public float currentY = 0;
Bitmap plane;
public PlaneView(Context context) {
super(context);
//定义飞机图片
plane = BitmapFactory.decodeResource(context.getResources(), R.drawable.plane);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//创建画笔
Paint paint = new Paint();
//画飞机
canvas.drawBitmap(plane, currentX, currentY, paint);
}
}
。。。。。。。。。。。。。。main.xml。。。。。。。。。。。。。。。。。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/back"
android:scaleType="fitXY"
/>
</LinearLayout>