安卓中view

2019-07-04 02:46:36 -0400

我是开发者

Android中的view

View in Android

The View class is a superclass of Android that contains almost all screen types. View类是Android的一个超类,这个类几乎包含了所有的屏幕类型\u3002每一个View都有一个用于绘图的画布,这...Each View has a canvas for drawing that can be extended at will. In game development, you can customize the view, and this canvas is more functional to meet our needs in game development. In Android, any View class simply rewrites the onDraw method to implement the interface display, and a custom view can be a complex 3D implementation or a very simple text form.

The most important thing in the game is the need to interact with players, such as keyboard input, stylus click events, how do we deal with these events? Android offers onKeyUp, onKeyDown, onKeyMultiple, onKeyPreIme, onTouchEvent, onTrackball Event, and more, and can easily handle event information in the game. Therefore, when inheriting View, you need to overload these methods, when there are keystrokes pressed or bounceevents, the key code will automatically be transmitted to these corresponding methods to handle.

At the heart of the game is the constant drawing and refresh of the interface, the graph we have drawn through the onDraw method, below to analyze how to refresh the interface. Android provides the invalidate method to implement interface refresh, note that invalidate can not be called directly in the online process, is it not possible to call in the subthread understand? Because it violates Android's single-threaded model: Android UI operations are not thread-safe, and they must be performed in the UI thread, the most common method in Android is to use Handler to implement UI thread updates. In fact, with AsyncTask can also.

Here's an example of a rectangle that changes colors on the screen, and we define events that can be adjusted by the simulator's up and down keys, such as moving the rectangle up or moving it down.

Below, look at the operation.

Effect:

01_view.jpg

01_view2.jpg

01_view3.jpg

We have a total of 2 classes one inherited View to draw, and the Activity class used to refresh our view, the two categories are Activity01 and GameView.

GameView Class

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class GameView extends View {
    int miCount = 0;
    int y = 0;
    public GameView(Context context) {
        super(context);
    }
    // 画图类
    public void onDraw(Canvas canvas) {
        if (miCount < 100) {
            miCount++;
        } else {
            miCount = 0;
        }
        // 绘图
        Paint mPaint = new Paint();
        switch (miCount % 4) {
        case 0:
            mPaint.setColor(Color.BLUE);
            break;
        case 1:
            mPaint.setColor(Color.GREEN);
            break;
        case 2:
            mPaint.setColor(Color.RED);
            break;
        case 3:
            mPaint.setColor(Color.YELLOW);
            break;
        default:
            mPaint.setColor(Color.WHITE);
            break;
        }
        // 绘制矩形
        canvas.drawRect((320 - 80) / 2, y, (320 - 80) / 2 + 80, y + 40, mPaint);
    }
}

Activity class is mainly used to refresh our view

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
public class Activity01 extends Activity {
    private static final int REFRESH = 0x000001;
    /* 声明GameView类对象 */
    private GameView mGameView = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* 实例化GameView对象 */
        this.mGameView = new GameView(this);
        // 设置显示为我们自定义的View(GameView)
        setContentView(mGameView);
        // 开启线程
        new Thread(new GameThread()).start();
    }
    Handler myHandler = new Handler() {
        // 接收到消息后处理
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case Activity01.REFRESH:
                // 注意这里的刷新界面实际上是在UI 线程中执行的 不是另外开启一个线程这里要搞清楚
                mGameView.invalidate();
                break;
            }
            super.handleMessage(msg);
        }
    };
    class GameThread implements Runnable {
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                Message message = new Message();
                message.what = Activity01.REFRESH;
                // 发送消息
                Activity01.this.myHandler.sendMessage(message);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
    /**
     * 当然可以将GameThread类这样写 同样可以更新界面,并且不在需要 Handler在接受消息 class GameThread
     * implements Runnable { public void run() { while
     * (!Thread.currentThread().isInterrupted()) { try { Thread.sleep(100); }
     * catch (InterruptedException e) { Thread.currentThread().interrupt(); }
     * //使用postInvalidate可以直接在线程中更新界面 //我认为它这个方法也是给主线程发送消息 最后刷新界面的工作还是在主线程中执行的
     * //如果我的看法错误 还请 大家疯狂留言。 mGameView.postInvalidate(); } } }
     */
    // 详细事件处理见第三章
    // 当然这些事件也可以写在GameView中
    // 触笔事件
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }
    // 按键按下事件
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return true;
    }
    // 按键弹起事件
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
        // 上方向键
        case KeyEvent.KEYCODE_DPAD_UP:
            mGameView.y -= 3;
            break;
        // 下方向键
        case KeyEvent.KEYCODE_DPAD_DOWN:
            mGameView.y += 3;
            break;
        }
        return false;
    }
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
        return true;
    }
}




我是开发者APP


必应在线翻译
原文:
Android中提供了onKeyUp、onKeyDown、onKeyMultiple、onKeyPreIme、onTouchEvent、onTrackballEvent等方法,可以轻松地处理游戏中的事件信息。
«Newer      Older»
Comment:
Name:

Back to home



Subscribe | Register | Login | N