Android 遊戲開發(一)瞭解View

來源:互聯網
上載者:User

標籤:android   style   blog   color   os   使用   io   strong   ar   

Android遊戲當中主要的除了控制類外就是顯示類View。SurfaceView是從View基類中派生出來的顯示類。android遊戲開發中常用的三種視圖是:view、SurfaceView和GLSurfaceView。

View:顯示視圖,內建畫布,提供圖形繪製函數、觸屏事件、按鍵事件函數等;必須在UI主線程內更新畫面,速度較慢。

SurfaceView:基於view視圖進行拓展的視圖類,更適合2D遊戲的開發;是view的子類,類似使用雙緩機制,在新的線程中更新畫面所以重新整理介面速度比view快。

GLSurfaceView:基於SurfaceView視圖再次進行拓展的視圖類,專用於3D遊戲開發的視圖;是SurfaceView的子類,openGL專用。

 

 在2D遊戲開發中,大致可以分為兩種遊戲架構,View和SurfaceView。 View和SurfaceView區別:

 View:必須在UI的主線程中更新畫面,用於被動更新畫面。

 SurfaceView:UI線程和子線程中都可以。在一個新啟動的線程中重新繪製畫面,主動更新畫面。

 

什麼是Paint?要繪圖,首先得調整畫筆,待畫筆調整好之後,再將映像繪製到畫布上,這樣才可以顯示在手機螢幕上。Android 中的畫筆是 Paint類。

什麼是Canvas?
當我們調整好畫筆之後,現在需要繪製到畫布上,這就得用Canvas類了。在Android中既然把Canvas當做畫布,那麼就可以在畫布上繪製我們想要的任何東西。除了在畫布上繪製之外,還需要設定一些關於畫布的屬性,比如,畫布的顏色、尺寸等。

 

遊戲中Avitivy基本會設定成無標題。

public class BeginActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //隱去標題列        this.requestWindowFeature(Window.FEATURE_NO_TITLE);        //隱去狀態列部分,包括電池等表徵圖        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);        setContentView(new MyView(this));    }}

 

有了基礎知識下面來寫一些文字,下面是要實現文字跟隨手移動。

import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.view.MotionEvent;import android.view.View;public class MyView extends View {        private int posX = 50;    private int posY = 50;    public MyView(Context context) {            super(context);        this.setBackgroundColor(Color.BLACK);        this.setFocusableInTouchMode(true);    }    @Override    protected void onDraw(Canvas canvas) {        Paint mPaint = new Paint();        mPaint.setColor(0xffffffff);        mPaint.setTextSize(50);        canvas.drawText("Game", posX, posY, mPaint);        super.onDraw(canvas);    }    //觸摸事件    @Override    public boolean onTouchEvent(MotionEvent event) {        int x = (int) event.getX();        int y = (int) event.getY();        switch (event.getAction()) {        //按下        case MotionEvent.ACTION_DOWN:            posX = x;            posY = y;            break;        //移動        case MotionEvent.ACTION_MOVE:            posX = x;            posY = y;                        break;        //彈出        case MotionEvent.ACTION_UP:            posX = x;            posY = y;                    break;        default:            break;        }        invalidate();        return super.onTouchEvent(event);    }    }

 

上面的代碼是無法實現文字跟隨文字移動,因為在MOVE這個Action中super.onTouchEvent事件中途會返回false。導致後面的event動作可能就會收不到了。

應該讓監聽函數的傳回值永遠為true。

//代碼修改@Overridepublic boolean onTouchEvent(MotionEvent event) {    int x = (int) event.getX();    int y = (int) event.getY();    posX = x;    posY = y;            invalidate();    return true;}

 

這樣就可以實現想要的效果了,^_^是不是很簡單!感謝百度提供的一些資料!

 

Android 遊戲開發(一)瞭解View

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.