Res Bitmap Drawable Canvas 详解 1.Resources 1.res/raw目录下的 InputStream inputStream=getResources().openRawResource(int id); 2.getDimension(int id) 3.DisplayMetrics getDisplayMetrics() 4.Drawable getDrawable(int id) 5.int[] getIntArray(int id) 6.int getInteger(int id) 7.XmlResourceParser getLayout(int id) 8.String getString(int id) 9.String[] getStringArray(int id) 10.XmlResourceParser getXml(int id) 11.TypedArray obtainAttributes(AttributeSet set, int[] attrs) 12.TypedArray obtainTypedArray(int id) 13.InputStream openRawResource(int id, TypedValue value) 14.InputStream openRawResource(int id) 15.AssetFileDescriptor openRawResourceFd(int id) 16.void parseBundleExtras(XmlResourceParser parser, Bundle outBundle) 从XML文件中获取数据 备注:final InputStream open(String fileName, int accessMode) | final AssetFileDescriptor openFd (String fileName) 获取asset目录下文件 FileDescriptor getFileDescriptor() 在Context中final TypedArray obtainStyledAttributes(int[] attrs) 或 final TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs) 获取TypedArray 2.Bitmap 代表一张位图,BitmapDrawable 里封装的就是一个bitmap 1.Bitmap drawable.getBitmap(); 2.static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) 从原为图source 的指定坐标点(x,y) 开始,从中抠出宽width,高height的一块,创建出新的位图,并 按照Matrix指定的规则进行转换 3.static Bitmap createBitmap(int width, int height, Bitmap.Config config) 4.static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)从原为图source 的 指定坐标点(x,y) 开始, 从中抠出宽width,高height的一块,创建出新的位图 5.static Bitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config) 6.static Bitmap createBitmap(Bitmap src) BitmapFactory中的类 7.static Bitmap decodeFileDescriptor(FileDescriptor fd) 8.static Bitmap decodeFile(String pathName, BitmapFactory.Options opts) 9.static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts) 10.static Bitmap decodeResource(Resources res, int id, BitmapFactory.Options opts) 11.static Bitmap decodeStream(InputStream is) Bitmap中的方法 1.boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) 将一个压缩的位图 写到outputstream中 (只支持PNG、JPG格式的压缩) Bitmap.CompressFormat JPEG PNG WEBP 2.final int getByteCount() 返回的字节数,用于存储此图的像素 3.final Bitmap.Config getConfig() Bitmap.Config ALPHA_8(每个像素都存储为一个单一的半透明通道)| ARGB_4444(废弃了)|ARGB_8888(每个像素都储存在4个字节)|RGB_565 4.int getDensity() 返回这个位图的密度 5.final int getHeight() 6.int getPixel(int x, int y) 返回指定位置的颜色 7.final int getWidth() 8.final boolean isRecycled() 9.void recycle() 10.boolean sameAs(Bitmap other) 11.void setDensity(int density) 指定这个位图的密度 12.void setPixel(int x, int y, int color) 将指定的颜色写在位图的指定位图的x,y坐标 13.void setPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height) 取代像 素的位图的颜色数组中 2.Drawable 当在Drawable三个文件中导入图像文件时,android会自动为这个图像文件生成一个Drawable对象,可以通过 getResource.getDrawable(int)直接获取 Deawable直接子类:BitmapDrawable, ClipDrawable, ColorDrawable, DrawableContainer, GradientDrawable, InsetDrawable, LayerDrawable, NinePatchDrawable, PictureDrawable, RotateDrawable, ScaleDrawable, ShapeDrawable 间接之类:AnimationDrawable, LevelListDrawable, PaintDrawable, StateListDrawable, TransitionDrawable Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还 有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象,就可以将这个可画对象当作一 块“画布(Canvas)”,在其上面操作可画对象,并最终将这种可画对象显示在画布上,有点类似于“内存画布“ 。 1.BitmapDrawable 1.BitmapDrawable() 2.BitmapDrawable(Resources res) 3.BitmapDrawable(Bitmap bitmap) 4.BitmapDrawable(InputStream is) 5.void draw(Canvas canvas) 6.final Bitmap getBitmap() 7.int getGravity() 8.int getIntrinsicHeight() 返回潜在的drawable对象的固有高度 9.int getIntrinsicWidth() 10.int getOpacity() 返回这个drawable的透明度或不透明度 11.final Paint getPaint() 返回用于给这个drawable着色的paint对象 12.void setAlpha(int alpha) 13.void setAntiAlias(boolean aa) 设置这个drawable是否有锯齿 14.void setGravity(int gravity) 15.void setTileModeX(Shader.TileMode mode)设置重复行为的X轴绘图Shader.TileMode :CLAMP| MIRROR |REPEAT 16.final Rect getBounds() 返回这个drawable的边界矩形 17.int getMinimumHeight() 返回这个drawable建议的最小高度 18.int getMinimumWidth() 19.void setBounds(int left, int top, int right, int bottom) 这个drawable特色的矩形边界框 20.接口 Drawable.Callback 需实现三个方法 1.void invalidateDrawable (Drawable who) 这个drawable被要 求重画的时候回调,因此我们对应于该View的背景Drawable对象能够”绘制出来”.2.scheduleDrawable (Drawable who, Runnable what, long when) drawable可以通过该方法来安排动画的下一帧。可以调用postAtTime(Runnable, Object, long)来实现该方法。参数分别与方法的参数对 3.unscheduleDrawable (Drawable who, Runnable what) 可以用于取消先前通过scheduleDrawable(Drawable who, Runnable what, long when)调度的某一帧。可以通过调 用removeCallbacks(Runnable,Object)来实现 3.Canvas 1.new Canvas() 2.Canvas(Bitmap bitmap) 3.boolean clipPath(Path path) 剪切当前的路径 4.boolean clipRect(RectF rect) 剪切矩形区域 5.boolean clipRect(Rect rect) Rect 和RectF 精度不一样 Rect是int,RectF是float 6.boolean clipRect(float left, float top, float right, float bottom) 7.boolean clipRegion(Region region) 8.boolean clipRect(Rect rect, Region.Op op) 其他方法中也有Region.Op INTERSECT 取两者交集,默认的 方式; DIFFERENCE 第一次不同于第二次的部分显示出来 ; REPLACE 显示第二次的 ;REVERSE_DIFFERENCE 第二 次不同于第一次的部分显示 UNION 取全集 ;XOR补集,就是全集的减去交集的剩余部分显示 9.void drawARGB(int a, int r, int g, int b) 10.void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint) 弧 11.void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) 12.void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) 13.void drawBitmapMesh (Bitmap bitmap, int meshWidth, int meshHeight, float[] verts, int vertOffset, int[] colors, int colorOffset, Paint paint) meshWidth:横向上的格数 meshHeight:纵向的格 数 ;verts:数组的x,y对,指定要画的网格,必须有至少(meshWidth + 1)*(meshHeight + 1)* 2 + meshOffset值的数 组;vertOffset :控制verts数组从第几个数组开始对bitmap进行扭曲 14.void drawCircle(float cx, float cy, float radius, Paint paint) 15.void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 16.void drawOval(RectF oval, Paint paint) 椭圆 17.void drawPath(Path path, Paint paint) 18.void drawPicture(Picture picture) 19.void drawPoint(float x, float y, Paint paint) 20.void drawPoints(float[] pts, int offset, int count, Paint paint) 21.void drawRoundRect(RectF rect, float rx, float ry, Paint paint) 圆角矩形 22.void drawText(String text, float x, float y, Paint paint) 23.void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) 24.int getDensity() 25.int getHeight() 26.final Matrix getMatrix() 27.void restore() 28.final void rotate(float degrees, float px, float py) 29.int save() 30.final void scale(float sx, float sy, float px, float py) 进行缩放 31.void setBitmap(Bitmap bitmap) 32.void translate(float dx, float dy) 在canvas中有个方法:drawPicture(),picture类,它并不存储实际的像素,仅仅记录了每个绘制的过程 1.Canvas beginRecording(int width, int height) //开始记录绘制过程 2.static Picture createFromStream(InputStream stream) //静态方法,从输入流创建一个Pictrue对象 3.void draw(Canvas canvas) //在canvas上画这个picture对象 4.void endRecording() //结束录制绘制过程 5.int getHeight() | int getWidth() 6.void writeToStream(OutputStream stream) //将绘制结果写到输出流中 PictureDrawable类 继承自drawable 1.PictureDrawable(Picture picture) 2.void draw(Canvas canvas) 3.Picture getPicture() 4.void setPicture(Picture picture) Paint类 1.Paint() | Paint(Paint paint) 2.void setARGB(int a, int r, int g, int b) 设置颜色 3.void setAlpha(int a) 4.void setAntiAlias(boolean aa) 5.void setColor(int color) 6.Shader setShader(Shader shader) 设置填充效果 7.void setShadowLayer(float radius, float dx, float dy, int color) 设置阴影 8.void setStrokeWidth(float width) 设置画笔宽度 9.void setStyle(Paint.Style style) Paint.Style FILL FILL_AND_STROKE STROKE 10.void setTextAlign(Paint.Align align) Paint.Align CENTER LEFT RIGHT 11.float measureText(String text) 返回text宽度 12.float getTextSize() 13.PathEffect setPathEffect(PathEffect effect) 路径效果 14.void setStrokeJoin(Paint.Join join) Paint.Join BEVEL MITER ROUND 画笔转弯处的连接风格 Path类 1.Path() | Path(Path src) 2.void addArc(RectF oval, float startAngle, float sweepAngle) 3.void addPath(Path src, float dx, float dy) 4.void close() 5.boolean isEmpty() 6.boolean isRect(RectF rect) 7.void lineTo(float x, float y) 8.void moveTo(float x, float y) 设置开始下一个轮廓点(x,y)。 9.void reset() 10.void transform(Matrix matrix) 转换通过Matrix 11.void addCircle(float x, float y, float radius, Path.Direction dir) Path.Direction CCW CW 12.void addRect(float left, float top, float right, float bottom, Path.Direction dir) 13.void addOval(RectF oval, Path.Direction dir)