drawable-图片绘制
首先看一下,下端代码
private Bitmap createSelectedChip(RecipientEntry contact, TextPaint paint) { int height = (int) mChipHeight; int deleteWidth = height; float[] widths = new float[1]; paint.getTextWidths(" ", widths); CharSequence ellipsizedText = ellipsizeText(createChipDisplayText(contact), paint, calculateAvailableWidth() - deleteWidth - widths[0]); printDebugLog(TAG,"[createSelectedChip] " + ellipsizedText); int width = Math.max( 0 /*deleteWidth * 2*/, (int) Math.floor(paint.measureText(ellipsizedText, 0, ellipsizedText.length())) + (mChipPadding * 2) + deleteWidth); /// @} // Create the background of the chip. //创建一个画布,宽为width,高为height Bitmap tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); //创建一个工具,这个工具的操作,都是在这个画布上进行的 Canvas canvas = new Canvas(tmpBitmap); if (mChipBackgroundPressed != null) { //创建一个矩形 Rect backgroundPadding = new Rect(); mChipBackgroundPressed.getPadding(backgroundPadding); //获得一个图片的大小,这个应该是按比例压缩的感觉 mChipBackgroundPressed.setBounds(0, 0, width - deleteWidth, height); //把这个压缩以后的图片绘制到这个画布上(图过工具) mChipBackgroundPressed.draw(canvas); //创建一个画笔,给他设定颜色 paint.setColor(sSelectedTextColor); //通过工具跟画笔,绘制一个文字 canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), mChipPadding, getTextYOffset((String) ellipsizedText, paint, height), paint); mChipDelete.setBounds(width - deleteWidth - backgroundPadding.left, + backgroundPadding.top/2, width - backgroundPadding.right/2, height); mChipDelete.draw(canvas); } else { Log.w(TAG, "Unable to draw a background for the chips as it was never set"); } return tmpBitmap; }
如
Drawable drawable = xxxx; drawable.setBounds(int, int, int, int); drawable.draw(canvas);
setBounds 有人给出的解释是,绘制图片时候,相对于父容器的边距
但是,通过验证,我感觉应该是如下的解释,看图