android事件监听器以及bitmap的运用

好长时间没有写博客了,近段时间学的都是andriod,写了一个小的例子:
MainActivty.cpp:
package com.example.dflenovo.plant;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends AppCompatActivity {

    private int speed = 10;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        setContentView(R.layout.activity_main);
        final Plant plant = new Plant(this);
        setContentView(plant);
        plant.setBackgroundResource(R.drawable.back);//设置背景
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();//获取窗体
        DisplayMetrics displayMetrics = new DisplayMetrics();//获取屏幕分辨率
        display.getMetrics(displayMetrics);
        plant.countX = displayMetrics.widthPixels/2;//初始的宽度
        plant.countY = displayMetrics.heightPixels-20;//初始的高度
        plant.setOnKeyListener(new View.OnKeyListener() {//按钮的监听
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch(event.getKeyCode()){
                    case KeyEvent.KEYCODE_S:
                             plant.countY+=speed;
                             break;
                    case KeyEvent.KEYCODE_W:
                        plant.countY-=speed;
                        break;
                    case KeyEvent.KEYCODE_A:
                        plant.countX-=speed;
                        break;
                    case KeyEvent.KEYCODE_D:
                        plant.countX+=speed;
                }
                plant.invalidate();//图片的重绘
                return true;

            }
        });
    }
}

Plant.cpp

package com.example.dflenovo.plant;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

/**
 * Created by DFLENOVO on 2016/3/26.
 */
public class Plant extends View {

    public float countX;
    public float countY;
    Bitmap plane;
    public Plant(Context context){
        super(context);
        plane = BitmapFactory.decodeResource(context.getResources(),R.drawable.plane);
        setFocusable(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        canvas.drawBitmap(plane,countX,countY,p);
    }


}
运行结束呢是用wsad来控制物体的移动,其中对于bitmap也有一点的运用,适合星空大战的游戏等,对于事件的运用比较的简单,与其他的监听差不多。


你可能感兴趣的:(android)