图形与图像处理(静态处理)
使用简单的图片
- 通过Drawable对象进行访问。
R.drawable.file_name
@drawable/file_name
通过BitmapDrawable对Bitmap对象进行封装,以下皆为静态方法,来自BitmapFactory。
BitmapDrawagle drawable - new
BitmapDrawable(bitmap);通过Bitmap对象的到另外的图像createBitmap(Bitmap source,int x,int y,int width, int
height):从源位图source的制定位置开始截取一个图像。createScaledBitmap(Bitmap src,int dstWidth,int dstHeight,boolean
filter):对原来的位图进行缩放createBitmap(int width,int height,Bitmap.Config
config):创建一个宽width、height的新位图createBitmap(Bitmap source,int x,int y,int width,int height,Matris
m,boolean filter);截取一段图像decodeByteArray(byte[],int offset, int
length);从指定字节数组的offset位置开始,将长度length的字节解析成Bitmap对象。decodeFile(String pathName):从pathName指定文件中解析、创建Bitmap对象
用来判断是否被回收: isRecycled()
用来强制回收:recycle()
使用静态方法导入一张图片:
AssetManager assets=getAssets();
InputStream assetsFile = assets.open(images[currentImg++]);
BitmapDrawable bitmapDrawable=(BitmapDrawable) image.getDrawable();
image.setImageBitmap(BitmapFactory.decodeStream(assetFile));
>绘图
绘图基础
- 集成View组件,重写onDraw()回调方法。
*使用Canvas画布进行画图
drawArc()
drawBitmap()
drawCircle()
drawLine()
drawLines(),drawOvl()
drawPath(),drawPoint()
drawPoints(),drawRect()
drawRoundRect()
drawText()
drawTextOnPath()
clipRect()
clipRegion();
* Canvas坐标变换rotate()旋转,scale()缩放,skew()倾斜,translate()移动;
使用Paint进行绘图
setARGB()设置颜色。
setAlpha()设置透明度
setColor()设置颜色
setPathEffect()设置路径
setShader()设置画笔的填充效果
setShadowLayer()设置阴影
setStrokeWidth()设置笔触宽度
setStrokeJoin()设置笔转风格
setStyle()设置paint的填充风格
setTextAlign()设置文字对齐方式
setTextSize()设置文本的文字大小。
使用Path类
预先将N个点连成一条路径,然后在路径上绘制
子类ComposePathEffect/CornerPathEffect/DashPathEffect/DiscretePathEffect/SumPathEffect
绘图应用-绘制有用游戏
重复调用onDraw()方法,每次在View组件上绘制不同的东西,形成逐帧动画。
双缓冲画图板
- 弹球游戏
>图形特效处理
使用Matrix控制变换步骤
获取Matrix对象
调用Matrix的方法进行变换(矩阵方法)
将程序Matrix所做的变换应用到指定图形或组件。
Matrix提供的方法
setTranslage
setSkew()
setTotate()
setScale()
Matrix 应用举例
键盘操作图片移动
移动游戏背景
使用drawBitmapMesh扭曲图像
bitmap:扭曲的源位图
meshWidth/meshHeight划分格子数
verts扭曲后个顶点的位置
vertOffset:控制verts数组中扭曲的开始。
通过drawBitmapMesh()方法对源位图扭曲绘制。
drawBitmapMesh 应用举例
- 可揉的图片
>使用Shader填充图形
使用Shader对象指定渲染效果来填充图像。子类:
BitmapShader位图平铺
LinerGradient线性渐变
RadialGradient圆形渐变
SweepGradient角度渐变
ComposeShader组合渲染效果





