天天看点

【ANDROID游戏开发之四】基础的ANDROID 游戏框架(一个游戏角色在屏幕行走的DEMO)

———————————————————————

『很多童鞋说我的代码运行后,点击home或者back后会程序异常,如果你也这样遇到过,那么你肯定没有仔细读完himi的博文,第十九篇himi专门写了关于这些错误的原因和解决方法,这里我在博客都补充说明下,省的童鞋们总疑惑这一块;请点击下面联系进入阅读:

——————————————————————-

其实上一篇分析surfaceview的文章就是一个简单的游戏框架了,当然这里再强调一下,简单的游戏框架,以不要高手们不要乱喷哦 :kl: ~

这个demo是给群里一童鞋写的一个对图片操作以及按键处理,游戏简单框架的一个demo,这里放出给大家分享~

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

package com.himi;

import android.content.context;

import android.content.res.resources;

import android.graphics.bitmap;

import android.graphics.bitmapfactory;

import android.graphics.canvas;

import android.graphics.color;

import android.graphics.paint;

import android.util.log;

import android.view.keyevent;

import android.view.surfaceholder;

import android.view.surfaceview;

import android.view.surfaceholder.callback;

public class mysurfaceview extends surfaceview implements callback, runnable {

    private thread th = new thread(this);

    private surfaceholder sfh;

    private int sh, sw;

    private canvas canvas;

    private paint p;

    private paint p2;

    private resources res;

    private bitmap bmp;

    private int bmp_x = 100, bmp_y = 100;

    private boolean up, down, left, right;

    private int animation_up[] = { 3, 4, 5 };

    private int animation_down[] = { 0, 1, 2 };

    private int animation_left[] = { 6, 7, 8 };

    private int animation_right[] = { 9, 10, 11 };

    private int animation_init[] = animation_down;

    private int frame_count;

    public mysurfaceview(context context) {

        super(context);

        this.setkeepscreenon(true);

        res = this.getresources();

        bmp = bitmapfactory.decoderesource(res, r.drawable.enemy1);

        sfh = this.getholder();

        sfh.addcallback(this);

        p = new paint();

        p.setcolor(color.yellow);

        p2 = new paint();

        p2.setcolor(color.red);

        p.setantialias(true);

        setfocusable(true);  //备注1

    }

    public void surfacecreated(surfaceholder holder) {

        sh = this.getheight();

        sw = this.getwidth();

        th.start();

    public void draw() {

        canvas = sfh.lockcanvas();

        canvas.drawrect(0, 0, sw, sh, p);   //备注2

        canvas.save();   //备注3

        canvas.drawtext("himi", bmp_x-2, bmp_y-10, p2);

        canvas.cliprect(bmp_x, bmp_y, bmp_x + bmp.getwidth() / 13, bmp_y+bmp.getheight());

        if (animation_init == animation_up) {

            canvas.drawbitmap(bmp, bmp_x - animation_up[frame_count] * (bmp.getwidth() / 13), bmp_y, p);

        } else if (animation_init == animation_down) {

            canvas.drawbitmap(bmp, bmp_x - animation_down[frame_count] * (bmp.getwidth() / 13), bmp_y, p);

        } else if (animation_init == animation_left) {

            canvas.drawbitmap(bmp, bmp_x - animation_left[frame_count] * (bmp.getwidth() / 13), bmp_y, p);

        } else if (animation_init == animation_right) {

            canvas.drawbitmap(bmp, bmp_x - animation_right[frame_count] * (bmp.getwidth() / 13), bmp_y, p);

        }

        canvas.restore();  //备注3

        sfh.unlockcanvasandpost(canvas);

    public void cycle() {

        if (down) {

            bmp_y += 5;

        } else if (up) {

            bmp_y -= 5;

        } else if (left) {

            bmp_x -= 5;

        } else if (right) {

            bmp_x += 5;

        if (down || up || left || right) {

            if (frame_count < 2) {

                frame_count++;

            } else {

                frame_count = 0;

            }

        if (down == false && up == false && left == false && right == false) {

            frame_count = 0;

    @override

    public boolean onkeydown(int key, keyevent event) {

        if (key == keyevent.keycode_dpad_up) {

            if (up == false) {

                animation_init = animation_up;

            up = true;

        } else if (key == keyevent.keycode_dpad_down) {

            if (down == false) {

                animation_init = animation_down;

            down = true;

        } else if (key == keyevent.keycode_dpad_left) {

            if (left == false) {

                animation_init = animation_left;

            left = true;

        } else if (key == keyevent.keycode_dpad_right) {

            if (right == false) {

                animation_init = animation_right;

            right = true;

        return super.onkeydown(key, event);

    /* (non-javadoc)

     * @see android.view.view#onkeyup(int, android.view.keyevent)

     */

    public boolean onkeyup(int keycode, keyevent event) {

            down = false;

            up = false;

            left = false;

            right = false;

        return super.onkeyup(keycode, event);

    public void run() {

        // todo auto-generated method stub

        while (true) {

            draw();

            cycle();

            try {

                thread.sleep(100);

            } catch (exception ex) {

    public void surfacechanged(surfaceholder holder, int format, int width, int height) {

    public void surfacedestroyed(surfaceholder holder) {

}

备注1

此方法是用来响应按键!如果是自己定义一个继承自view的类,重新实现onkeydown方法后,只有当该view获得焦点时才会调用onkeydown方法,actvity中的onkeydown方法是当所有控件均没有处理该按键事件时,才会调用.

 备注2

   这里也是对屏幕进行刷屏操作,其实这也只是一种,之前文章里我也用到drawrgb的方法同样实现,当然也可以用fillrect等来刷屏。

那么这里我想说下,在继承view中,因为ondraw方法是系统自动调用的,不像在surfaceview这里这样去在run里面自己去不断调用,在view中我们可以抵用 invalidate()/postinvalidate() 这两种方法实现让系统调用ondraw方法,这里也是和surfaceview中的不同之一!

 备注3

   这里canvas.save();和canvas.restore();是两个相互匹配出现的,作用是用来保存画布的状态和取出保存的状态的。这里稍微解释一下,

当我们对画布进行旋转,缩放,平移等操作的时候其实我们是想对特定的元素进行操作,比如图片,一个矩形等,但是当你用canvas的方法来进行这些操作的时候,其实是对整个画布进行了操作,那么之后在画布上的元素都会受到影响,所以我们在操作之前调用canvas.save()来保存画布当前的状态,当操作之后取出之前保存过的状态,这样就不会对其他的元素进行影响

对于 canvas.save();和canvas.restore();  还有不少童鞋不懂,ok、我再补充点:

代码段1:

public void draw() {

  canvas canvas = sfh.lockcanvas();

  canvas.drawcolor(color.black);

  canvas.drawbitmap(bmp1, 0,0,paint);

  canvas.save();

  canvas.scale(1.5f, 1.5f);

  canvas.restore();

  canvas.drawbitmap(bmp2, 0,0,paint);

  sfh.unlockcanvasandpost(canvas);

代码段2:

上面这两个代码片段中我们都假设有两张图片 bmp1和bmp2,并且都画在画布上!

那么代码段1和代码段2的不同:

代码段1中我们进行画布缩放的之前保存了画布状态,做了缩放操作之后又取出之前保存的状态,这样做是为了保证bmp2正常画出来不受到缩放的影响!

代码段2里,画了bmp1后就执行了缩放操作,并且没有保存状态!紧接着画了bmp2,那么bmp2也会一样受到缩放的影响!!

所以我们如果单独处理一张图片的时候,而且不想影响其他部分的绘制,那么应该如下来做:

    canvas canvas = sfh.lockcanvas();

    canvas.drawcolor(color.black);

    canvas.drawbitmap(bmp1, 0,0,paint);

    canvas.save();

    canvas.scale(1.5f, 1.5f);

    canvas.drawbitmap(bmp2, 0,0,paint);

    canvas.restore();

    sfh.unlockcanvasandpost(canvas);

  }