Android中贪吃蛇游戏的学习(四)
16lz
2021-12-04
Android中贪吃蛇游戏的学习(四)
文章分类:移动开发
Java代码- packagecom.easyway.dev.android.snake;
- importandroid.content.Context;
- importandroid.content.res.TypedArray;
- importandroid.graphics.Bitmap;
- importandroid.graphics.Canvas;
- importandroid.graphics.Paint;
- importandroid.graphics.drawable.Drawable;
- importandroid.util.AttributeSet;
- importandroid.view.View;
- /**
- *Android平台裡,使用者介面都是透过ViewGroup或View类别来显示。
- *ViewGroup和View是Android平台上最基本的使用者介面表达单元。我
- *们可以透过程式直接呼叫的方法,调用描绘使用者介面,将萤幕上显示的介面元
- *素,与构成应用程式主体的程式逻辑,溷合在一起编写。或是,也可以将介面显示
- *与程式逻辑分离,照着Android提供的这个较优雅的方式,使用XML描述档,
- *来描述介面元件的组织。
- *
- *在Android系统中,我们使用XML来定义UI。但是有些稍微有经验的开发者可能会有疑问:
- *「用XML来描述介面固然方便,但是对于手机程式来说,直接用XML档桉是不是太占空间了?」。
- *没错,如果Android是直接使用XML来储存介面描述到手机上的话,一定会佔用比起现在大的多
- *的档桉空间。解决的方法是Android并不直接使用XML档桉,而是透过Android开发工具,
- *自动将XML描述档转换成资源档桉。一旦应用程式要操作某个介面元件,或是使用任何种类的资源
- *(字串、图片、图示、音效...),都使用索引来查询。
- *
- *
- *伟大的创意少之又少,多数时候只是一些小改进。小的改进也是好的。
- *
- *
- *TileView:aView-variantdesignedforhandlingarraysof"icons"orother
- *drawables.
- *
- */
- publicclassTileViewextendsView{
- /**
- *Parameterscontrollingthesizeofthetilesandtheirrangewithinview.
- *Width/Heightareinpixels,andDrawableswillbescaledtofittothese
- *dimensions.X/YTileCountsarethenumberoftilesthatwillbedrawn.
- */
- protectedstaticintmTileSize;
- protectedstaticintmXTileCount;
- protectedstaticintmYTileCount;
- privatestaticintmXOffset;
- privatestaticintmYOffset;
- /**
- *
- *Ahashthatmapsintegerhandlesspecifiedbythesubclassertothe
- *drawablethatwillbeusedforthatreference
- */
- privateBitmap[]mTileArray;
- /**
- *声明用来存放绘画图像的x,y轴的位置的数组
- *Atwo-dimensionalarrayofintegersinwhichthenumberrepresentsthe
- *indexofthetilethatshouldbedrawnatthatlocations
- */
- privateint[][]mTileGrid;
- privatefinalPaintmPaint=newPaint();
- publicTileView(Contextcontext,AttributeSetattrs,intdefStyle){
- super(context,attrs,defStyle);
- TypedArraya=context.obtainStyledAttributes(attrs,R.styleable.TileView);
- mTileSize=a.getInt(R.styleable.TileView_tileSize,12);
- a.recycle();
- }
- publicTileView(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- TypedArraya=context.obtainStyledAttributes(attrs,R.styleable.TileView);
- mTileSize=a.getInt(R.styleable.TileView_tileSize,12);
- a.recycle();
- }
- /**
- *ReststheinternalarrayofBitmapsusedfordrawingtiles,and
- *setsthemaximumindexoftilestobeinserted
- *
- *@paramtilecount
- */
- publicvoidresetTiles(inttilecount){
- mTileArray=newBitmap[tilecount];
- }
- @Override
- protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){
- mXTileCount=(int)Math.floor(w/mTileSize);
- mYTileCount=(int)Math.floor(h/mTileSize);
- mXOffset=((w-(mTileSize*mXTileCount))/2);
- mYOffset=((h-(mTileSize*mYTileCount))/2);
- mTileGrid=newint[mXTileCount][mYTileCount];
- clearTiles();
- }
- /**
- *FunctiontosetthespecifiedDrawableasthetileforaparticular
- *integerkey.
- *
- *@paramkey
- *@paramtile
- */
- publicvoidloadTile(intkey,Drawabletile){
- Bitmapbitmap=Bitmap.createBitmap(mTileSize,mTileSize,Bitmap.Config.ARGB_8888);
- Canvascanvas=newCanvas(bitmap);
- tile.setBounds(0,0,mTileSize,mTileSize);
- tile.draw(canvas);
- mTileArray[key]=bitmap;
- }
- /**
- *Resetsalltilesto0(empty)
- *
- */
- publicvoidclearTiles(){
- for(intx=0;x<mXTileCount;x++){
- for(inty=0;y<mYTileCount;y++){
- setTile(0,x,y);
- }
- }
- }
- /**
- *Usedtoindicatethataparticulartile(setwithloadTileandreferenced
- *byaninteger)shouldbedrawnatthegivenx/ycoordinatesduringthe
- *nextinvalidate/drawcycle.
- *
- *@paramtileindex图片的索引
- *@paramxx轴的位置
- *@paramyy轴的位置
- */
- publicvoidsetTile(inttileindex,intx,inty){
- mTileGrid[x][y]=tileindex;
- }
- /**
- *重写VIEW类里面的方法。把界线画出。
- *
- *地图其实就是由图片数组拼直面成的。面图片又是通过他的图片索引找到,并
- *在mTileGrid[x][y],获取他们的位置索引来确定图片的位置。这样在一个
- *手机的页面就形成了,
- *
- */
- @Override
- publicvoidonDraw(Canvascanvas){
- super.onDraw(canvas);
- for(intx=0;x<mXTileCount;x+=1){
- for(inty=0;y<mYTileCount;y+=1){
- if(mTileGrid[x][y]>0){
- canvas.drawBitmap(mTileArray[mTileGrid[x][y]],
- mXOffset+x*mTileSize,
- mYOffset+y*mTileSize,
- mPaint);
- }
- }
- }
- }
- }
更多相关文章
- Android中的AutoCompleteTextView的使用
- Android(安卓)角标 BadgeView/android-viewbadger使用介绍
- [转] 软键盘android:windowSoftInputMode属性使用
- Android使用DialogFragment,自定义对话框
- MAC上使用maven打android的包,报错:No Android(安卓)SDK path cou
- Android使用HttpClient时出现错误:java.lang.NoSuchFieldError: N
- Android--ExpandbleView源码学习一---ExpandableListView使用
- Android(安卓)Notification的使用
- 第12天android:短信发送+测试使用