public void buildCache(int w, int h, int density, boolean checkSizeEquals) { boolean reuse = checkSizeEquals ? (w == width && h == height) : (w <= width && h <= height); if (reuse && bitmap != null && !bitmap.isRecycled()) { // canvas.drawColor(Color.TRANSPARENT); canvas.setBitmap(null); bitmap.eraseColor(Color.TRANSPARENT); canvas.setBitmap(bitmap); recycleBitmapArray(); return; } if (bitmap != null) { recycle(); } width = w; height = h; bitmap = NativeBitmapFactory.createBitmap(w, h, Bitmap.Config.ARGB_8888); if (density > 0) { mDensity = density; bitmap.setDensity(density); } if (canvas == null) { canvas = new Canvas(bitmap); canvas.setDensity(density); } else canvas.setBitmap(bitmap); }
@SuppressLint("NewApi") public void splitWith( int dispWidth, int dispHeight, int maximumCacheWidth, int maximumCacheHeight) { recycleBitmapArray(); if (width <= 0 || height <= 0 || bitmap == null || bitmap.isRecycled()) { return; } if (width <= maximumCacheWidth && height <= maximumCacheHeight) { return; } maximumCacheWidth = Math.min(maximumCacheWidth, dispWidth); maximumCacheHeight = Math.min(maximumCacheHeight, dispHeight); int xCount = width / maximumCacheWidth + (width % maximumCacheWidth == 0 ? 0 : 1); int yCount = height / maximumCacheHeight + (height % maximumCacheHeight == 0 ? 0 : 1); int averageWidth = width / xCount; int averageHeight = height / yCount; final Bitmap[][] bmpArray = new Bitmap[yCount][xCount]; if (canvas == null) { canvas = new Canvas(); if (mDensity > 0) { canvas.setDensity(mDensity); } } Rect rectSrc = new Rect(); Rect rectDst = new Rect(); for (int yIndex = 0; yIndex < yCount; yIndex++) { for (int xIndex = 0; xIndex < xCount; xIndex++) { Bitmap bmp = bmpArray[yIndex][xIndex] = NativeBitmapFactory.createBitmap( averageWidth, averageHeight, Bitmap.Config.ARGB_8888); if (mDensity > 0) { bmp.setDensity(mDensity); } canvas.setBitmap(bmp); int left = xIndex * averageWidth, top = yIndex * averageHeight; rectSrc.set(left, top, left + averageWidth, top + averageHeight); rectDst.set(0, 0, bmp.getWidth(), bmp.getHeight()); canvas.drawBitmap(bitmap, rectSrc, rectDst, null); } } canvas.setBitmap(bitmap); bitmapArray = bmpArray; }