dafeiji

Posted gentspy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}}}};

以上是关于dafeiji的主要内容,如果未能解决你的问题,请参考以下文章