7.1使用简单Drawable图片
使用R.drawablw.xxx来引用即可
7.1.2BitMap和BitmapFactory
bitmap转drawable:使用new BitmapDrawable(bitmap);
drawable转bitmap:使用drawable.getBitmap();
BitmapFactory的方法:
1. decodeByteArray(byte[] data,int offset,int length);
2. decodeFile(String pathName);
3. decodeFileDescriptor(FileDescriptor fd);
4. decodeSource(Resource res, int id);
5. decodeStream(inputStream is);
问题:大量使用封装该图片的Drawable对象,可能由于前面创建的Bitmap所占用的内存还没有回收,导致程序出现OutOfMemory的错误。
解决办法:Android为Bitmap提供两种判读Bitmap是否已回收的方法,以及强制Bitmap回收自己。
1. boolean isRecycled():返回该Bitmap对象是否已被回收。
2. void recycle():强制一个Bitmap对象立即回收自己。
方法为:if(!bitmapDrawable.getBitmap().isRecycled()){bitmapDrawable.getBitmap().recycle()};
如果Android应用需要访问其他路径(SD卡),需要借助BitmapFactory来解析、创建对象。
7.2绘图
丰富的Android界面离不开绘图罗,废话不多说,入正题。
7.2.1 Android绘图基础:Canvas、Paint等
Andriod绘图过程:
Canvas的方法:
1. drawArc(RectF oval,float startAngle,float sweepAngle,boolean useCenter,Paint paint);画弧
2. drawBitmap(Bitmap bitmap,Rect src,Rect dst,Paint paint);制定图片挖取一块
3. drawBitmap(Bitmap bitmap,float left ,float top,Paint paint);指定点画位图
4. drawCircle(float cx,float cy,float radius,Paint paint);画圆
5. drwaLine(float startX,float startY,float stopX,float stopY,Paint paint);
6. drawLines(float[] pts,int offset ,int counts,Paint paint);
7. drawOval(RectF oval,Paint paint);
8. drawPath(Path path,Paint paint);沿指定的Path绘制任意形状
9. drawPoint(float x,float y,Paint paint);
10. drawPoints(float[] pts,int offset,int count,Paint paint);
11. drawRect(float left,float top,float right,float bottom,Paint paint);
12. drawRoundRect(RectF rect,float rx,float ry,Paint paint);圆角矩形
13. drawText(String text,int start,int end,Paint paint);
14. drawTextOnPath(String text,Path path,float hOffset,float vOffset,Paint paint);
15. clipRect(float left,float top,float right,float bottom);
16. clipRegion(Region region);裁剪指定区域
17. drawBitmapMesh(Bitmap bitmap,int meshWidth,int meshHeight,float[] verts,int vertOffset,int[] colors,int colorOffset,Paint paint);对Bitmap进行扭曲
除此之外,Canvas还提供如下方法进行坐标变换:
1. rotate(float degress,float px,float py)
2. scale(float sx,float sy,float px,float py)
3. skew(float sx,float sy)倾斜变换
4. translate(float dx,float dy)
说完Canvas,来来来,皮皮虾,我们去看看Paint:
如果说Canvas是画布的话,那么Paint就是画笔罗。包括绘画风格,粗细,填充。
Paint的方法&#x