1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象
2、Canvas画布,绘图的目的区域,用于绘图
3、Bitmap位图,用于图的处理
4、Matrix矩阵
Resources res = getResources(); Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);
[] Bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); baos.toByteArray(); }
Bitmap Bytes2Bimap([] b) { (b.length != 0) { BitmapFactory.decodeByteArray(b, 0, b.length); } { ; } }
Bitmap zoomBitmap(Bitmap bitmap, width, height) { w = bitmap.getWidth(); h = bitmap.getHeight(); Matrix matrix = Matrix(); scaleWidth = (() width / w); scaleHeight = (() height / h); matrix.postScale(scaleWidth, scaleHeight); Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, ); newbmp; }
Bitmap drawableToBitmap(Drawable drawable) { w = drawable.getIntrinsicWidth(); h = drawable.getIntrinsicHeight(); Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; Bitmap bitmap = Bitmap.createBitmap(w, h, config); Canvas canvas = Canvas(bitmap); drawable.setBounds(0, 0, w, h); drawable.draw(canvas); bitmap; }
Bitmap getRoundedCornerBitmap(Bitmap bitmap, roundPx) { w = bitmap.getWidth(); h = bitmap.getHeight(); Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas canvas = Canvas(output); color = 0xff424242; Paint paint = Paint(); Rect rect = Rect(0, 0, w, h); RectF rectF = RectF(rect); paint.setAntiAlias(); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode( PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); output; }
Bitmap createReflectionImageWithOrigin(Bitmap bitmap) { reflectionGap = 4; w = bitmap.getWidth(); h = bitmap.getHeight(); Matrix matrix = Matrix(); matrix.preScale(1, -1); Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, ); Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Config.ARGB_8888); Canvas canvas = Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap, 0, 0, ); Paint deafalutPaint = Paint(); canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint); canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, ); Paint paint = Paint(); LinearGradient shader = LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); paint.setShader(shader); paint.setXfermode( PorterDuffXfermode(Mode.DST_IN)); canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint); bitmapWithReflection; }
Bitmap bm=xxx; BitmapDrawable bd= BitmapDrawable(getResource(), bm); 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
Drawable zoomDrawable(Drawable drawable, w, h) { width = drawable.getIntrinsicWidth(); height = drawable.getIntrinsicHeight(); Bitmap oldbmp = drawableToBitmap(drawable); Matrix matrix = Matrix(); sx = (() w / width); sy = (() h / height); matrix.postScale(sx, sy); Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, ); BitmapDrawable(newbmp); }