dafeiji

public class PlanView extends View{
public float currentX;
public float currenty;
Bitmap plan;

public PlanView(Context context) {
super(context);
plan=BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher);
setFocusable(true);

}
@Override
protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

Paint paint=new Paint();
canvas.drawBitmap(plan, currentX,currenty, paint);
}

}

public class EventDemoByPlanActivity extends Activity {
//飞机的速度
private int speed=10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_demo_by_plan);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//创建planView组件
final PlanView planView=new PlanView(this);
setContentView(planView);
planView.setBackgroundResource(R.drawable.ic_launcher);
//huo qu chuang kou guan li qi
WindowManager windowManager=getWindowManager();
Display display=windowManager.getDefaultDisplay();
DisplayMetrics metrics= new DisplayMetrics();
//huo qu ping mu kuan gao.
display.getMetrics(metrics);
//设置起始位置
planView.currentX=metrics.widthPixels/2;
planView.currenty=metrics.heightPixels-40;
//为planView 键盘事件绑定监听器
planView.setOnKeyListener(new OnKeyListener(){

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_S:
planView.currenty+=speed;
break;
case KeyEvent.KEYCODE_W:
planView.currenty-=speed;
break;
case KeyEvent.KEYCODE_A:
planView.currentX+=speed;
break;
case KeyEvent.KEYCODE_D:
planView.currentX-=speed;
break;
}
planView.invalidate();
return false;
}}}};

时间: 2024-10-14 00:23:22

dafeiji的相关文章

python------列表的使用

列表的知识点 1.创建列表 给变量赋值一个列表形如:list = [ ] 列表可空,可实形如:list_name =['ergou','goudan','xiaolizi'] 2.列表的操作 在python中创建的一切东西都是对象,想要对对象做些什么就需要方法.python中调取对对象的操作方法形如:对象的名字.操作方法 修改列表元素 使用索引来修改某个列表的元素,形如:list_name[1] = 'sunwukong' 向列表添加元素 append():向列表末尾添加元素 .形如:list_

C#高级------委托

namespace out_ref { //声明一个委托 public delegate void MyDelegate(); class Program { static void Main(string[] args) { MyDelegate mdl = Say; Do(mdl); Console.ReadKey(); } static void Do(MyDelegate mdl) { mdl(); } static void Say() { Console.WriteLine("哈哈&

PAT Huffman Codes

In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redundancy Codes", and hence printed his name in the history of computer science.  As a professor who gives the final exam problem on Huffman codes, I am enc