Android自带示例程序--Snake

Android示例程序------->Snake解析(1)

2011-05-11 19:40:02

Android SDK中自带的有很多示例程序,这是我我们提高的很好的途径,多看这些文档对我们提高很有帮助。

此篇是对Snake的初步分析,后续还会更新较完整的解析。由于本人是初学Android 如果有错的地方或者更好的实现方法,

请不吝赐教,帮助处于新手阶段的我们。

分析Snake的程序,此程序的主体都是一个一个的小方块的东西实现的,android程序中叫TileView(切片),只是颜色不同而已。所以此篇我们从画一个TileView学起。

下面来看代码部分:

1.先自己定义一个类DrawSnakeNode继承View 用来画出一个方块;

package com.yin.snake; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.View; public class DrawSnakeNode extends View { //每个Node(节点)的大小 private final int mTileSize =12; // private static String TAG = "SnakeDemo"; //新建一个画笔 private static Paint paint = new Paint(); private Bitmap mBitmap; //Node的坐标(x,y) private int x; private int y; public DrawView(Context context) { super(context); //获得一个Bitmap对象 mBitmap = loadTile(); } //继承自View的类的构造函数,必须重载,否则会抛出异常 public DrawView(Context context, AttributeSet attrs) { super(context, attrs); } //继承自View的类的构造函数,必须重载,否则会抛出异常 public DrawView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public Bitmap loadTile(){ Resources r = getResources(); //获取图片的资源,Snake程序自带的有此图片 Drawable tile = r.getDrawable(R.drawable.greenstar); /** * 创建一个Bitmap *width : The width of the bitmap *height :The height of the bitmap *config :The bitmap config to create. * */ Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize,Bitmap.Config.ARGB_8888 ); Canvas canvas = new Canvas(bitmap); //设置图像的边界 tile.setBounds(0, 0, mTileSize, mTileSize); tile.draw(canvas); return bitmap; } //每次重画调用此方法 protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(mBitmap, x, y, paint); } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } }


2.创建一个控制类来控制Snake的移动 此篇是方块的移动

package com.yin.snake; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; public class ContralSnake extends Activity { DrawSnakeNode dsn; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); dsn = new DrawView(getApplicationContext()); // setContentView(R.layout.main); setContentView(dsn); } public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DPAD_UP){ dv.setY(dv.getY()-10); }else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){ dv.setY(dv.getY()+10); }else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){ dv.setX(dv.getX()-10); }else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){ dv.setX(dv.getX()+10); } //强制重画(跟新当前画面) dv.invalidate(); return super.onKeyDown(keyCode, event); } }


由于Bitmap等很多画图是来自JAVA中,不是很熟悉,可能写的有错误的地方,请积极指出,多多交流。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
List of Sample Apps The list below provides a summary of the sample applications that are available with the Android SDK. Using the links on this page, you can view the source files of the sample applications in your browser. You can also download the source of these samples into your SDK, then modify and reuse it as you need. For more information, see Getting the Samples. API Demos A variety of small applications that demonstrate an extensive collection of framework topics. Backup and Restore A simple example that illustrates a few different ways for an application to implement support for the Android data backup and restore mechanism. Bluetooth Chat An application for two-way text messaging over Bluetooth. BusinessCard An application that demonstrates how to launch the built-in contact picker from within an activity. This sample also uses reflection to ensure that the correct version of the contacts API is used, depending on which API level the application is running under. Contact Manager An application that demonstrates how to query the system contacts provider using the ContactsContract API, as well as insert contacts into a specific account. Home A home screen replacement application. JetBoy A game that demonstrates the SONiVOX JET interactive music technology, with JetPlayer. Live Wallpaper An application that demonstrates how to create a live wallpaper and bundle it in an application that users can install on their devices. Lunar Lander A classic Lunar Lander game. Multiple Resolutions A sample application that shows how to use resource directory qualifiers to provide different resources for different screen configurations. Note Pad An application for saving notes. Similar (but not identical) to the Notepad tutorial. SampleSyncAdapter Demonstrates how an application can communicate with a cloud-based service and synchronize its data with data stored locally in a content provider. The sample uses two related parts of the Android framework — the account manager and the synchronization manager (through a sync adapter). Searchable Dictionary A sample application that demonstrates Android's search framework, including how to provide search suggestions for Quick Search Box. Snake An implementation of the classic game "Snake." Soft Keyboard An example of writing an input method for a software keyboard. Spinner A simple application that serves as an application-under-test for the SpinnerTest sample application. SpinnerTest An example test application that contains test cases run against the Spinner sample application. To learn more about the application and how to run it, please read the Activity Testing tutorial. TicTacToeLib An example of an Android library project that provides a game-play Activity to any dependent application project. For an example of how an application can use the code and resources in an Android library project, see the TicTacToeMain sample application. TicTacToeMain An example of an Android application that makes use of code and resources provided in an Android library project. Specifically, this application uses code and resources provided in the TicTacToeLib library project. Wiktionary An example of creating interactive widgets for display on the Android home screen. Wiktionary (Simplified) A simple Android home screen widgets example.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值