android画图学习笔记

只几天看了Snake这个Demo,没事那就做个俄罗斯方块吧,再此做个笔记。

 

只几天看了Snake这个Demo,没事那就做个俄罗斯方块吧,再此做个笔记。

 

首先要把一张图片载入到屏幕上,写一个类TileView(按照Snake的模式),在onDraw方法中加入

 

 mTileSize是全局变量,值为24,是素材图片的长宽。一共有三个图片,红黄绿,三个方块,可以构成贪吃蛇,也可以玩俄罗斯方块。

 

Log.i(tag, "TileView onDraw");
Resources r = this.getContext().getResources();
Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize, Bitmap.Config.ARGB_8888);
Drawable tile = r.getDrawable(R.drawable.redstar);
tile.setBounds(0, 0, mTileSize, mTileSize);
tile.draw(canvas);
canvas.drawBitmap(bitmap, 0, 0, mPaint);

 

首先create一个Bitmap。

Drawable tile = r.getDrawable(R.drawable.redstar);
就是把红色方块载入到程序中,然后设置显示的范围,然后把这个Drawable画到画布canvas上,最后画布开始画画,这个图像就显示出来了。

 

怎么感觉这么复杂呢?那么多的参数都是干啥的?继续研究

 

既然一个图片可以显示出来了,现在让三个方块都显示出来,一排显示

 

Resources r = this.getContext().getResources();
Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize, Bitmap.Config.ARGB_8888);

Drawable tile1 = r.getDrawable(R.drawable.redstar);
tile1.setBounds(0, 0, mTileSize, mTileSize);
tile1.draw(canvas);
		
Drawable tile2 = r.getDrawable(R.drawable.yellowstar);
tile2.setBounds(mTileSize, 0, mTileSize*2, mTileSize);
tile2.draw(canvas);
	
Drawable tile3 = r.getDrawable(R.drawable.greenstar);
tile3.setBounds(mTileSize*2, 0, mTileSize*3, mTileSize);
tile3.draw(canvas);
		
canvas.drawBitmap(bitmap, 0, 0, mPaint);

 

由此可见,setBounds的前两个是图片的左上角坐标,后两个是图片右下角的坐标(并不是图片的长和宽)

 

canvas.drawBitmap(bitmap, 0, 0, mPaint);

改成

canvas.drawBitmap(bitmap, mTileSize, mTileSize, mPaint);

怎么没变化呢?

 

 api解析是

写道

Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, transformed by the current matrix.

Note: if the paint contains a maskfilter that generates a mask which extends beyond the bitmap's original width/height (e.g. BlurMaskFilter), then the bitmap will be drawn as if it were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be the edge color replicated.

If the bitmap and canvas have different densities, this function will take care of automatically scaling the bitmap to draw at the same density as the canvas.

Parameters
bitmap The bitmap to be drawn
left The position of the left side of the bitmap being drawn
top The position of the top side of the bitmap being drawn
paint The paint used to draw the bitmap (may be null)

 

 

 

 --------------------------

 

 

新的Tetris不知怎么TextView不能显示在图层之上,就像Snake的状态提示text那样,在SnakeView层之上。

 

找了半天,才发现原来是Layout搞错了,必须要用FrameLayout才能达到这样的效果,默认的LinearLayout是不行的

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值