创建一个view用来显示飞机
public class PlaneView extends View {
Bitmap bmap;
float currentx=40;
float currenty=40;
public PlaneView(Context context) {
super(context);
bmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.small142);
setFocusable(true);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint p=new Paint();
canvas.drawBitmap(bmap, currenty, currenty, p);
}
}
主程序
public class Expandlelv extends Activity {
int speed=6;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//去掉标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
final PlaneView pv=new PlaneView(this);
setContentView(pv);
//获取窗口管理
Display display=getWindowManager().getDefaultDisplay();
int screenwidth=display.getWidth();
int screenheight=display.getHeight();
pv.currentx=screenwidth/2;
pv.currenty=screenheight/2;
pv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
pv.currentx=event.getX();
pv.currenty=event.getY();
//通知pv组件重绘
pv.invalidate();
return true;
}
});
}
}