• android 按键控制飞机



    演示一个很好的的效果,类似雷电,但功能上差很多,现在就实现了飞机的移动功能,不过,看上去还是挺漂亮的。

    首先新建一个布局main.xml文件

    View Code
     1 <?xml version="1.0" encoding="utf-8"?>
    2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:id="@+id/root"
    4 android:orientation="vertical"
    5 android:layout_width="fill_parent"
    6 android:layout_height="fill_parent"
    7 >
    8 <ImageView
    9 android:layout_width="fill_parent"
    10 android:layout_height="fill_parent"
    11 android:src="@drawable/back"
    12 android:scaleType="fitXY"
    13 />
    14 </LinearLayout>

    第二步:建一个飞机类,这样就可以在主程序里new出飞机来了。

    View Code
     1 package cn.edu.zwu.tel;
    2
    3 import android.content.Context;
    4 import android.graphics.Bitmap;
    5 import android.graphics.BitmapFactory;
    6 import android.graphics.Canvas;
    7 import android.graphics.Paint;
    8 import android.view.View;
    9
    10 public class PlaneView extends View
    11 {
    12 public float currentX;
    13 public float currentY;
    14 Bitmap plane;
    15
    16 public PlaneView(Context context)
    17 {
    18 super(context);
    19 // TODO Auto-generated constructor stub
    20 //定义飞机图片
    21 plane = BitmapFactory.decodeResource(context.getResources()
    22 , R.drawable.plane);
    23 setFocusable(true);
    24 }
    25 @Override
    26 public void onDraw (Canvas canvas)
    27 {
    28 super.onDraw(canvas);
    29 //创建画笔
    30 Paint p = new Paint();
    31 //绘制飞机
    32 canvas.drawBitmap(plane , currentX , currentY , p);
    33 }
    34 }

    第三,修改LayoutKeyEvent01.java文件,可以控制飞机上下移动

    View Code
     1 package cn.edu.zwu.tel;
    2
    3 import android.app.Activity;
    4 import android.os.Bundle;
    5 import android.view.Display;
    6 import android.view.KeyEvent;
    7 import android.view.View;
    8 import android.view.Window;
    9 import android.view.WindowManager;
    10 import android.view.View.OnKeyListener;
    11
    12 public class LayoutKeyEvent01Activity extends Activity {
    13 /** Called when the activity is first created. */
    14 private int speed = 12;
    15
    16 @Override
    17 public void onCreate(Bundle savedInstanceState)
    18 {
    19 super.onCreate(savedInstanceState);
    20 // 去掉窗口标题
    21 requestWindowFeature(Window.FEATURE_NO_TITLE);
    22 // 全屏显示
    23 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    24 WindowManager.LayoutParams.FLAG_FULLSCREEN);
    25 // 创建PlaneView组件
    26 final PlaneView planeView = new PlaneView(this);
    27 setContentView(planeView);
    28 planeView.setBackgroundResource(R.drawable.back);
    29 // 获取窗口管理器
    30 WindowManager windowManager = getWindowManager();
    31 Display display = windowManager.getDefaultDisplay();
    32 // 获得屏幕宽和高
    33 int screenWidth = display.getWidth();
    34 int screenHeight = display.getHeight();
    35 // 设置飞机的初始位置
    36 planeView.currentX = screenWidth / 2;
    37 planeView.currentY = screenHeight - 40;
    38 // 为draw组件键盘事件绑定监听器
    39 planeView.setOnKeyListener(new OnKeyListener()
    40 {
    41 @Override
    42 public boolean onKey(View source, int keyCode, KeyEvent event)
    43 {
    44 // 获取由哪个键触发的事件
    45 switch (event.getKeyCode())
    46 {
    47 // 控制飞机下移
    48 case KeyEvent.KEYCODE_DPAD_DOWN:
    49 planeView.currentY += speed;
    50 break;
    51 // 控制飞机上移
    52 case KeyEvent.KEYCODE_DPAD_UP:
    53 planeView.currentY -= speed;
    54 break;
    55 // 控制飞机左移
    56 case KeyEvent.KEYCODE_DPAD_LEFT:
    57 planeView.currentX -= speed;
    58 break;
    59 // 控制飞机右移
    60 case KeyEvent.KEYCODE_DPAD_RIGHT:
    61 planeView.currentX += speed;
    62 break;
    63 }
    64 // 通知planeView组件重绘
    65 planeView.invalidate();
    66 return true;
    67 }
    68 });
    69 }
    70 }

    下面是效果图:



  • 相关阅读:
    软件杯第一阶段
    架构漫谈读后感二
    架构漫谈读后感一
    软件架构实践读后感三
    Refined Architecture阶段读后感
    软件架构实践读后感二
    Excel打开csv文件乱码
    安装机器学习所需的库
    认识Numpy Ndarray对象
    初识pandas
  • 原文地址:https://www.cnblogs.com/shaoyangjiang/p/2371752.html
Copyright © 2020-2023  润新知