public static Bitmap screenshot(View view, boolean hasstatubar) { if (view == null) { return null; } view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap cachebmp = view.getDrawingCache(); Rect rect = new Rect(); view.getWindowVisibleDisplayFrame(rect); int statusBarHeights = rect.top; DisplayMetrics m = view.getResources().getDisplayMetrics(); // 获取屏幕宽和高 int widths = m.widthPixels; int heights = m.heightPixels; Bitmap bmp = null; if (cachebmp != null) { if (hasstatubar) { bmp = Bitmap.createBitmap(cachebmp, 0, 0, widths, heights); } else { bmp = Bitmap.createBitmap(cachebmp, 0, statusBarHeights, widths, heights - statusBarHeights); } } view.destroyDrawingCache(); return bmp; }
/** Reads Logical Screen Descriptor */ protected void readLSD() { // logical screen size width = readShort(); height = readShort(); // packed fields final int packed = read(); gctFlag = (packed & 0x80) != 0; // 1 : global color table flag // 2-4 : color resolution // 5 : gct sort flag gctSize = 2 << (packed & 7); // 6-8 : gct size bgIndex = read(); // background color index pixelAspect = read(); // pixel aspect ratio // Now that we know the size, init scratch arrays try { mainPixels = new byte[width * height]; mainScratch = new int[width * height]; copyScratch = new int[width * height]; previousImage = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); currentImage = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); } catch (final OutOfMemoryError e) { // Try with halving down the bitmap size final int dimension = (width * height) / 2; mainPixels = new byte[dimension]; mainScratch = new int[dimension]; copyScratch = new int[dimension]; previousImage = Bitmap.createBitmap(width / 2, height / 2, Bitmap.Config.ARGB_4444); currentImage = Bitmap.createBitmap(width / 2, height / 2, Bitmap.Config.ARGB_4444); } }
@Override public Bitmap transform(Bitmap source) { int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); if (squaredBitmap != source) { source.recycle(); } if (source.getConfig() == null) { return source; } Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); paint.setShader(shader); paint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, paint); squaredBitmap.recycle(); return bitmap; }
Bitmap drawEye() { Paint paint_eyebg = new Paint(); paint_eyebg.setColor(Color.parseColor("#EDEDED")); Paint paint_eyerim = new Paint(); paint_eyerim.setColor(Color.parseColor("#F2E16C")); Paint paint_eyeinside = new Paint(); paint_eyeinside.setColor(Color.parseColor("#FFFFFF")); Paint paint_red = new Paint(); paint_red.setColor(Color.parseColor("#FFE342")); Paint paint_black = new Paint(); paint_black.setColor(Color.parseColor("#000000")); Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888); Canvas canvas_eye = new Canvas(bg); Bitmap bg_eyeinside = Bitmap.createBitmap(bg.getWidth(), bg.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas_bg_inside = new Canvas(bg_eyeinside); canvas_bg_inside.drawOval(new RectF(240, 200, 300, 280), paint_red); canvas_bg_inside.drawBitmap(bg_eyeinside, 0, 0, null); canvas_eye.drawCircle(230, 200, 60, paint_eyebg); canvas_eye.drawCircle(230, 200, 50, paint_eyebg); canvas_eye.drawBitmap(bg_eyeinside, -40, -40, null); canvas_eye.drawCircle(230, 200, 35, paint_eyeinside); canvas_eye.drawCircle(220, 200, 20, paint_black); canvas_eye.drawCircle(220, 200, 2, paint_eyebg); return bg; }
@Override public Bitmap transform(Bitmap source) { int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); if (squaredBitmap != source) { source.recycle(); } Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); Canvas canvas = new Canvas(bitmap); Paint avatarPaint = new Paint(); BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); avatarPaint.setShader(shader); Paint outlinePaint = new Paint(); outlinePaint.setColor(Color.WHITE); outlinePaint.setStyle(Paint.Style.STROKE); outlinePaint.setStrokeWidth(STROKE_WIDTH); outlinePaint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, avatarPaint); canvas.drawCircle(r, r, r - STROKE_WIDTH / 2, outlinePaint); squaredBitmap.recycle(); return bitmap; }
public Bitmap getReflectedBitmap(Bitmap bitmap) { // LogUtil.i("chenxu", "bitmap's width:"+bitmap.getWidth()+" height:"+bitmap.getHeight()+" // totalHeight:"+height); Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, itemWidth, height * 2 / 3, true); Bitmap resultBitmap = Bitmap.createBitmap(itemWidth, height, Config.ARGB_8888); Canvas canvas = new Canvas(resultBitmap); canvas.drawBitmap(scaledBitmap, null, new Rect(0, 0, itemWidth, height * 2 / 3), null); Paint paint = new Paint(); paint.setAntiAlias(true); LinearGradient shader = new LinearGradient(0, height * 2 / 3, 0, height, 0x88ffffff, 0xffffffff, TileMode.CLAMP); paint.setShader(shader); paint.setAlpha(180); Matrix matrix = new Matrix(); matrix.postScale(1, -1); Bitmap reflectionBitmap = Bitmap.createBitmap( scaledBitmap, 0, height * 2 / 3 * 2 / 3, itemWidth, height * 2 / 3 * 1 / 3, matrix, true); canvas.drawBitmap(reflectionBitmap, 0, height * 2 / 3, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); Rect r = new Rect(0, height * 2 / 3, 0, height); canvas.drawRect(r, paint); return resultBitmap; }
@Override public Bitmap transform(Bitmap bitmap) { if (null == bitmap) return null; PaintFlagsDrawFilter fliter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); int w = bitmap.getWidth(); int h = bitmap.getHeight(); if (w <= 0 || h <= 0) { return null; } int r = (w < h ? w : h) / 2; Bitmap circle = Bitmap.createBitmap(2 * r, 2 * r, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(circle); canvas.setDrawFilter(fliter); Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); p.setColor(Color.RED); canvas.drawCircle(r, r, r, p); p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); // 生成结果图片 Bitmap out = Bitmap.createBitmap(2 * r, 2 * r, Bitmap.Config.ARGB_8888); Canvas outCan = new Canvas(out); outCan.setDrawFilter(fliter); outCan.drawBitmap(bitmap, 0, 0, null); outCan.drawBitmap(circle, 0, 0, p); circle.recycle(); bitmap.recycle(); return out; }
public static Bitmap createReflectedImage(Bitmap bitmap, int i) { int j = bitmap.getWidth(); int k = bitmap.getHeight(); Bitmap bitmap2; if (k <= i) { bitmap2 = null; } else { Matrix matrix = new Matrix(); matrix.preScale(1.0F, -1F); Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, k - i, j, i, matrix, true); bitmap2 = Bitmap.createBitmap(j, i, android.graphics.Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap2); canvas.drawBitmap(bitmap1, 0.0F, 0.0F, null); LinearGradient lineargradient = new LinearGradient( 0.0F, 0.0F, 0.0F, bitmap2.getHeight(), -2130706433, 16777215, android.graphics.Shader.TileMode.CLAMP); Paint paint = new Paint(); paint.setShader(lineargradient); paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN)); canvas.drawRect(0.0F, 0.0F, j, bitmap2.getHeight(), paint); } return bitmap2; }
private byte[] rotateImage( byte[] cameraData, final int imageWidth, final int imageHeight, int currentCameraId) { Bitmap landscapeCameraDataBitmap = BitmapFactory.decodeByteArray(cameraData, 0, cameraData.length); Bitmap portraitBitmap = null; if (currentCameraId == getCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT)) { // front camera portraitBitmap = Bitmap.createBitmap( landscapeCameraDataBitmap, 0, 0, imageWidth, imageHeight, rotationMatrixFront, true); } else { // back camera portraitBitmap = Bitmap.createBitmap( landscapeCameraDataBitmap, 0, 0, imageWidth, imageHeight, rotationMatrixBack, true); } landscapeCameraDataBitmap.recycle(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); if (!portraitBitmap.isRecycled()) { portraitBitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, stream); byte[] portraitCameraData = stream.toByteArray(); portraitBitmap.recycle(); return portraitCameraData; } else { return new byte[0]; } }
public static Bitmap createBitmapOnHeight(Bitmap bitmap, int height, float degrees) { if (bitmap == null) return null; float ratio = 0; if (degrees == 90 || degrees == 270) { ratio = (float) height / bitmap.getWidth(); } else { ratio = (float) height / bitmap.getHeight(); } Matrix matrix = new Matrix(); matrix.postScale(ratio, ratio); Bitmap bm_scale = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); bitmap.recycle(); Bitmap bm_degress = null; if (degrees == 0) { bm_degress = bm_scale; } else { matrix.setRotate(degrees); bm_degress = Bitmap.createBitmap( bm_scale, 0, 0, bm_scale.getWidth(), bm_scale.getHeight(), matrix, false); bm_scale.recycle(); } return bm_degress; }
public static Bitmap decodeBitmapFromDrawable( Drawable drawable, final int width, final int height) { if (drawable == null) { return null; } if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } try { Bitmap bitmap; if (drawable instanceof ColorDrawable) { bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); } else { bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } catch (OutOfMemoryError e) { return null; } }
/** * @param bitmap * @return */ public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) { final int reflectionGap = 4; int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1); Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false); Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap, 0, 0, null); Paint deafalutPaint = new Paint(); canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint); canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient( 0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; }
// AUT:[email protected]. DATE:2012-12-22. START. public static Bitmap resizeAndCropCenterExt(Bitmap bitmap, int size, boolean recycle) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); if (w == size && h == size) return bitmap; // scale the image so that the shorter side equals to the target; // the longer side will be center-cropped. float scale = (float) size / Math.min(w, h); int width = Math.round(scale * bitmap.getWidth()); int height = Math.round(scale * bitmap.getHeight()); if (width > height) { int largeSize = (int) (width <= size * 1.5 ? width : size * 1.5); Bitmap target = Bitmap.createBitmap(largeSize, size, getConfig(bitmap)); Canvas canvas = new Canvas(target); canvas.translate((largeSize - width) / 2f, (size - height) / 2f); canvas.scale(scale, scale); Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); canvas.drawBitmap(bitmap, 0, 0, paint); if (recycle) bitmap.recycle(); return target; } else { int largeSize = (int) (height <= size * 1.5 ? height : size * 1.5); Bitmap target = Bitmap.createBitmap(size, largeSize, getConfig(bitmap)); Canvas canvas = new Canvas(target); canvas.translate((size - width) / 2f, (largeSize - height) / 2f); canvas.scale(scale, scale); Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); canvas.drawBitmap(bitmap, 0, 0, paint); if (recycle) bitmap.recycle(); return target; } }
/** * 生成带镜面倒影的bitmap * * @param originBitmap * @return bitmapWithReflection * @author 考虑到体验的流畅感,以及Gallery的加载机制缺陷,不推荐使用此方法。 仅保留此代码段作备用和记录 by Felix */ public Bitmap getReflection(Bitmap image) { final int reflectionGap = 4; Bitmap originalImage = image; int width = originalImage.getWidth(); int height = originalImage.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1); Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false); Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(originalImage, 0, 0, null); Paint deafaultPaint = new Paint(); canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint); canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient( 0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x80ffffff, 0x00ffffff, TileMode.CLAMP); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; }
@Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable == null) { return; } if (getWidth() == 0 || getHeight() == 0) { return; } Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); if (null == bitmap) { return; } final Paint paint = new Paint(); final Rect rect1 = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final Rect rect2 = new Rect(0, 0, bitmap.getWidth(), bitmap.getWidth()); paint.setAntiAlias(true); paint.setFilterBitmap(true); Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas1 = new Canvas(output); canvas1.drawCircle( bitmap.getWidth() / 2.0f, bitmap.getWidth() / 2.0f, bitmap.getWidth() / 2.0f, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas1.drawBitmap(bitmap, rect1, rect1, paint); float sx = ((float) getWidth()) / (float) output.getWidth(); float sy = ((float) getHeight()) / (float) output.getHeight(); Matrix matrix = new Matrix(); matrix.setScale(sx, sx); output = Bitmap.createBitmap(output, 0, 0, output.getWidth(), output.getHeight(), matrix, true); canvas.drawBitmap(output, 0, 0, null); }
public void setNiti() { // 起動時 int charId = re.getIdentifier("suuji", "drawable", "rising.sun"); msuji = BitmapFactory.decodeResource(re, charId, opt); suuji[0] = Bitmap.createBitmap(msuji, 0, 0, 20, 26); suuji[1] = Bitmap.createBitmap(msuji, 60, 0, 20, 26); suuji[2] = Bitmap.createBitmap(msuji, 120, 0, 20, 26); }
/** * Divide the bitmap into the number of pieces specified by the WIDTH_IN_PIECES and * HEIGHT_IN_PIECES constants * * @param bitmap * @return */ private Bitmap[] createPieces(Bitmap bitmap) { float piecesWide = WIDTH_IN_PIECES; float piecesHigh = HEIGHT_IN_PIECES; Bitmap[] bitmapArray = new Bitmap[TOTAL_PIECES]; float pieceHeight = bitmap.getHeight() / piecesHigh; float pieceWidth = bitmap.getWidth() / piecesWide; mPieceWidth = (int) pieceWidth; mPieceHeight = (int) pieceHeight; int i = 0; int emptySquareIndex = mRand.nextInt(TOTAL_PIECES); for (int w = 0; w < piecesWide; w++) { for (int h = 0; h < piecesHigh; h++) { int x = (int) (w * pieceWidth); int y = (int) (h * pieceHeight); if (i != emptySquareIndex) { bitmapArray[i] = Bitmap.createBitmap(bitmap, x, y, (int) pieceWidth, (int) pieceHeight); } else { bitmapArray[i] = null; mMissingCoord = new Coordinate(w, h); mMissingPiece = Bitmap.createBitmap(bitmap, x, y, (int) pieceWidth, (int) pieceHeight); } mGridState[w][h] = i; i++; } } return bitmapArray; }
private Bitmap drawTextToBitmap(String gText) { Bitmap bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888); bitmap.eraseColor(Color.TRANSPARENT); bitmap.setHasAlpha(true); // bitmap.setHasAlpha(true); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.TRANSPARENT); TextPaint textPaint = new TextPaint(); textPaint.setColor(Color.GREEN); textPaint.setTextSize(35); RectF rect = new RectF(0, 0, 300, 300); StaticLayout sl = new StaticLayout( gText, textPaint, (int) rect.width(), Layout.Alignment.ALIGN_NORMAL, 1, 1, false); canvas.save(); sl.draw(canvas); canvas.restore(); // Flip the image. Matrix m = new Matrix(); m.preScale(-1, 1); Bitmap output = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, false); output.setDensity(DisplayMetrics.DENSITY_DEFAULT); return output; }
Bitmap loadImage(String path) { boolean landscape = false; Bitmap bm; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); int oh = options.outHeight; int ow = options.outWidth; if (ow > oh) { landscape = true; oh = options.outWidth; ow = options.outHeight; } options.inJustDecodeBounds = false; options.inSampleSize = Math.max(ow / w, oh / h); bm = BitmapFactory.decodeFile(path, options); if (landscape) { Matrix matrix = new Matrix(); matrix.setRotate(90.0f); bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, false); } bm = Bitmap.createScaledBitmap( bm, (int) (w), (int) (w * (((double) oh) / ((double) ow))), false); Bitmap offBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas offCanvas = new Canvas(offBitmap); offCanvas.drawBitmap(bm, 0, (h - bm.getHeight()) / 2, null); bm = offBitmap; return bm; }
public static BitmapDrawable createCheckerBoard(Resources res, int size) { size *= res.getDisplayMetrics().density; BitmapShader shader = new BitmapShader( Bitmap.createBitmap( new int[] {0xFFFFFFFF, 0xFFCCCCCC, 0xFFCCCCCC, 0xFFFFFFFF}, 2, 2, Bitmap.Config.RGB_565), BitmapShader.TileMode.REPEAT, BitmapShader.TileMode.REPEAT); Matrix matrix = new Matrix(); matrix.setScale(size, size); shader.setLocalMatrix(matrix); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setShader(shader); Bitmap bm2 = Bitmap.createBitmap(size * 2, size * 2, Bitmap.Config.RGB_565); new Canvas(bm2).drawPaint(paint); BitmapDrawable drawable = new BitmapDrawable(res, bm2); drawable.setTileModeXY(BitmapShader.TileMode.REPEAT, BitmapShader.TileMode.REPEAT); return drawable; }
private void sendScreenshot() { Bitmap bitmap = mIitcWebView.getDrawingCache(); if (bitmap == null) { mIitcWebView.buildDrawingCache(); bitmap = mIitcWebView.getDrawingCache(); if (bitmap == null) { Log.e("could not get bitmap!"); return; } bitmap = Bitmap.createBitmap(bitmap); if (!mIitcWebView.isDrawingCacheEnabled()) mIitcWebView.destroyDrawingCache(); } else { bitmap = Bitmap.createBitmap(bitmap); } try { final File cache = getExternalCacheDir(); final File file = File.createTempFile("IITC screenshot", ".png", cache); if (!bitmap.compress(CompressFormat.PNG, 100, new FileOutputStream(file))) { // quality is ignored by PNG throw new IOException("Could not compress bitmap!"); } startActivityForResult( ShareActivity.forFile(this, file, "image/png"), new ResponseHandler() { @Override public void onActivityResult(final int resultCode, final Intent data) { file.delete(); } }); } catch (final IOException e) { Log.e("Could not generate screenshot", e); } }
/** * 将给定图片维持宽高比缩放后,截取正中间的正方形部分。 * * @param bitmap 原图 * @return 缩放截取正中部分后的位图。 */ public static Bitmap getCirclePhoto(Bitmap bitmap) { if (null == bitmap) { return null; } int edgeLength = 0; Bitmap result = bitmap; int widthOrg = bitmap.getWidth(); int heightOrg = bitmap.getHeight(); if (widthOrg >= heightOrg) { edgeLength = heightOrg; try { result = Bitmap.createBitmap(result, (widthOrg - heightOrg) / 2, 0, edgeLength, edgeLength); } catch (Exception e) { return null; } } else { edgeLength = widthOrg; try { result = Bitmap.createBitmap(result, 0, (heightOrg - widthOrg) / 2, edgeLength, edgeLength); } catch (Exception e) { return null; } } return toRoundCorner(result, 2); // 使正方形图片变成正圆 }
public void captureScreen() { int topHeight = 300, bottomHeight = 250; View v = getWindow().getDecorView().getRootView(); v.setDrawingCacheEnabled(true); Bitmap b = Bitmap.createBitmap(v.getDrawingCache()); screenShot = Bitmap.createBitmap( b, 0, bottomHeight, b.getWidth(), b.getHeight() - (bottomHeight + topHeight)); v.setDrawingCacheEnabled(false); try { FileOutputStream fos = this.openFileOutput("screenshot", Context.MODE_PRIVATE); screenShot.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); Intent intent = new Intent(this, EditFinish.class); intent.putExtra("image", "screenshot"); startActivity(intent); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * Returns the given page as an RGB image at the given scale. * * @param pageIndex the zero-based index of the page to be converted * @param scale the scaling factor, where 1 = 72 DPI * @param config the bitmap config to create * @return the rendered page image * @throws IOException if the PDF cannot be read */ public Bitmap renderImage(int pageIndex, float scale, Bitmap.Config config) throws IOException { PDPage page = document.getPage(pageIndex); PDRectangle cropbBox = page.getCropBox(); float widthPt = cropbBox.getWidth(); float heightPt = cropbBox.getHeight(); int widthPx = Math.round(widthPt * scale); int heightPx = Math.round(heightPt * scale); int rotationAngle = page.getRotation(); // swap width and height Bitmap image; if (rotationAngle == 90 || rotationAngle == 270) { image = Bitmap.createBitmap(heightPx, widthPx, config); } else { image = Bitmap.createBitmap(widthPx, heightPx, config); } // use a transparent background if the imageType supports alpha Paint paint = new Paint(); Canvas canvas = new Canvas(image); if (config != Bitmap.Config.ARGB_8888) { paint.setColor(Color.WHITE); paint.setStyle(Paint.Style.FILL); canvas.drawRect(0, 0, image.getWidth(), image.getHeight(), paint); paint.reset(); } renderPage(page, paint, canvas, image.getWidth(), image.getHeight(), scale, scale); return image; }
private void drawStar(Canvas canvas, int position) { float fraction = mRating - (position); Bitmap ratedStar = getRatedStar(); if ((position + 1) < mRating) { canvas.drawBitmap(ratedStar, (position * (ratedStar.getWidth() + space)), 0, null); } else { // fraction 小数部分 if (fraction > 0 && fraction <= 1) { int sourceWidth = ratedStar.getWidth(); int sourceHeight = ratedStar.getHeight(); int targetWidth = (int) (ratedStar.getWidth() * fraction); int bgWidth = sourceWidth - targetWidth; if (targetWidth > 0) { Bitmap croppedBmp = Bitmap.createBitmap(ratedStar, 0, 0, targetWidth, sourceHeight); canvas.drawBitmap(croppedBmp, (position * (sourceWidth + space)), 0, null); // croppedBmp.recycle(); } if (bgWidth > 0) { Bitmap croppedBg = Bitmap.createBitmap(progressBackground, targetWidth, 0, bgWidth, sourceHeight); canvas.drawBitmap(croppedBg, (position * (sourceWidth + space)) + targetWidth, 0, null); // croppedBg.recycle(); } } else { canvas.drawBitmap( progressBackground, (position * (progressBackground.getWidth() + space)), 0, null); } } }
@Override protected Bitmap transform(BitmapPool pool, Bitmap source, int outWidth, int outHeight) { int sourceWidth = source.getWidth(); int sourceHeight = source.getHeight(); double outRatio = (double) outWidth / (double) outHeight; int x = 0; int width = sourceWidth; int height = (int) (width / outRatio); int y = (sourceHeight - height) / 2; if (height >= sourceHeight) { height = sourceHeight; width = (int) (outRatio * height); y = 0; x = (sourceWidth - width) / 2; } Bitmap bitmap = Bitmap.createBitmap(source, x, y, width, height); // 圆角 Bitmap output = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF dst = new RectF(0, 0, outWidth, outHeight); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(dst, mRadius, mRadius, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, src, dst, paint); return output; }
public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = null; if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if (bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { bitmap = Bitmap.createBitmap( 1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel } else { bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; }
PictureGalleryDialog( Context context, long trackId, long[] legIds, PictureGalleryImage[] imageDetails, DisplayMetrics displayMetrics) { super(context); setTitle(context.getString(R.string.picture_gallery_dialog_title)); this.trackId = trackId; this.legIds = legIds; this.imageDetails = imageDetails; this.displayMetrics = displayMetrics; this.thumbnailDefaultBitmap = Bitmap.createBitmap(getThumbnailSize(), getThumbnailSize(), Bitmap.Config.ARGB_4444); Canvas canvas = new Canvas(thumbnailDefaultBitmap); canvas.drawColor(Color.WHITE); this.browserDefaultBitmap = Bitmap.createBitmap( displayMetrics.widthPixels, displayMetrics.heightPixels, Bitmap.Config.ARGB_4444); canvas = new Canvas(browserDefaultBitmap); canvas.drawColor(Color.WHITE); }
private Bitmap getBitmapFromDrawable(Drawable drawable) { if (drawable == null) { return null; } if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } try { Bitmap bitmap; if (drawable instanceof ColorDrawable) { bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG); } else { bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth() + 1, drawable.getIntrinsicHeight() + 1, BITMAP_CONFIG); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } catch (OutOfMemoryError e) { return null; } }
@SuppressWarnings("deprecation") public void setCircleButtonStateListDrawable( View circleButton, int radius, int pressedColor, int normalColor) { WeakReference<Bitmap> imagePressed = new WeakReference<>(Bitmap.createBitmap(2 * radius, 2 * radius, Bitmap.Config.ARGB_8888)); Canvas canvasPressed = new Canvas(imagePressed.get()); Paint paintPressed = new Paint(); paintPressed.setAntiAlias(true); paintPressed.setColor(pressedColor); canvasPressed.drawCircle(radius, radius, radius, paintPressed); WeakReference<Bitmap> imageNormal = new WeakReference<>(Bitmap.createBitmap(2 * radius, 2 * radius, Bitmap.Config.ARGB_8888)); Canvas canvasNormal = new Canvas(imageNormal.get()); Paint paintNormal = new Paint(); paintNormal.setAntiAlias(true); paintNormal.setColor(normalColor); canvasNormal.drawCircle(radius, radius, radius, paintNormal); StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState( new int[] {android.R.attr.state_pressed}, new BitmapDrawable(circleButton.getContext().getResources(), imagePressed.get())); stateListDrawable.addState( StateSet.WILD_CARD, new BitmapDrawable(circleButton.getContext().getResources(), imageNormal.get())); if (android.os.Build.VERSION.SDK_INT >= 16) { circleButton.setBackground(stateListDrawable); } else { circleButton.setBackgroundDrawable(stateListDrawable); } }