這篇文章給大家介紹 canvas drawBitmap方法怎么樣在Android項(xiàng)目中使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
Android canvas drawBitmap方法詳解及實(shí)例
之前自己在自定義view,用到canvas.drawBitmap(Bitmap, SrcRect, DesRect, Paint)的時(shí)候,對(duì)其中的第2和3個(gè)參數(shù)的含義含糊不清??丛创a函數(shù)也沒(méi)理解,然后看了一些其他的博客加上自己的理解,整理如下。首先,我們看一張圖片,今天就要繪制這張圖片。
然后將圖片用紅色的線條分成4個(gè)部分,如下:
我們自定義一個(gè)View,代碼如下:
public class PoterDuffLoadingView extends View { private Resources mResources; private Paint mBitPaint; private Bitmap mBitmap; private int mTotalWidth, mTotalHeight; private Bitmap girlBitmap; private int girlBitWidth , girlBitHeight; private Rect girlSrcRect , girlDesRect; public PoterDuffLoadingView(Context context) { super(context); mResources = getResources(); initBitmap(); } private void initBitmap() { //美女圖片的寬和高 girlBitmap = ((BitmapDrawable)mResources.getDrawable(R.drawable.a1)).getBitmap(); girlBitWidth = girlBitmap.getWidth(); girlBitHeight = girlBitmap.getHeight(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(girlBitmap, girlSrcRect, girlDesRect, null); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { Log.d("xxxxxx", "onSizeChanged , w = "+w+" , h = "+h+" , mBitWidth = "+mBitWidth+" , mBitHeight = "+mBitHeight); super.onSizeChanged(w, h, oldw, oldh); mTotalWidth = w; mTotalHeight = h; girlSrcRect = new Rect(0, 0, girlBitWidth, girlBitHeight); girlDesRect = new Rect(0, 0, girlBitWidth, girlBitHeight); } }