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)