public static Bitmap roundBitmap(Bitmap bitmap, int i, int i2, float f, float f2, float f3, float f4) throws Throwable { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Rect rect = new Rect(0, 0, width, height); Bitmap createBitmap = (width == i && height == i2) ? Bitmap.createBitmap(bitmap.getWidth (), bitmap.getHeight(), Config.ARGB_8888) : Bitmap.createBitmap(i, i2, Config .ARGB_8888); Canvas canvas = new Canvas(createBitmap); Paint paint = new Paint(); Rect rect2 = new Rect(0, 0, i, i2); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(-12434878); float[] fArr = new float[]{f, f, f2, f2, f3, f3, f4, f4}; ShapeDrawable shapeDrawable = new ShapeDrawable(new RoundRectShape(fArr, new RectF(0.0f, 0.0f, 0.0f, 0.0f), fArr)); shapeDrawable.setBounds(rect2); shapeDrawable.draw(canvas); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect2, paint); return createBitmap; }
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable == null) { return null; } if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } try { Bitmap bitmap = Bitmap.createBitmap(Math.max(drawable.getIntrinsicWidth(), 2), Math.max(drawable.getIntrinsicHeight(), 2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } catch (IllegalArgumentException e) { e.printStackTrace(); return null; } }
/** * 合并Bitmap * * @param bgd 背景Bitmap * @param fg 前景Bitmap * @return 合成后的Bitmap */ public static Bitmap combineImages(Bitmap bgd, Bitmap fg) { Bitmap bmp; int width = bgd.getWidth() > fg.getWidth() ? bgd.getWidth() : fg .getWidth(); int height = bgd.getHeight() > fg.getHeight() ? bgd.getHeight() : fg .getHeight(); bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888); Paint paint = new Paint(); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP)); Canvas canvas = new Canvas(bmp); canvas.drawBitmap(bgd, 0, 0, null); canvas.drawBitmap(fg, 0, 0, paint); return bmp; }
public static Bitmap createQRCode(String str, int size) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, size, size); int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for(int x = 0; x < width; x ++){ for(int y = 0; y < height; y ++){ if(matrix.get(x, y)){ pixels[y * width + x] = BLACK; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
public MapClusterOptionsProvider(Resources resources) { int poiSize = resources.getDimensionPixelSize(R.dimen.map_poi_size); Drawable d = ResourcesCompat.getDrawable(resources, R.drawable.ic_wrapper_poi_cluster, null); d.setBounds(0, 0, poiSize, poiSize); Bitmap bitmap = Bitmap.createBitmap(poiSize, poiSize, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); d.draw(canvas); baseBitmap = bitmap; littleFontPaint.setColor(Color.BLACK); littleFontPaint.setTextAlign(Align.CENTER); littleFontPaint.setFakeBoldText(true); littleFontPaint.setTextSize(resources.getDimension(R.dimen.map_marker_cluster_text_size_small)); bigFontPaint.setColor(Color.BLACK); bigFontPaint.setTextAlign(Align.CENTER); bigFontPaint.setFakeBoldText(true); bigFontPaint.setTextSize(resources.getDimension(R.dimen.map_marker_cluster_text_size_big)); }
/** *圆角图片 * @param bitmap * @return */ public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = 12; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
private void initCanvas() { mOriginalBitmapWidth = mOriginalBitmap.getWidth(); mOriginalBitmapHeight = mOriginalBitmap.getHeight(); // 初始状态值 mWaveOriginalY = mOriginalBitmapHeight; mWaveY = 1.2f * mWaveOriginalY; mBezierControlOriginalY = 1.25f * mWaveOriginalY; mBezierControlY = mBezierControlOriginalY; mXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN); mBezierPath = new Path(); mCanvas = new Canvas(); mUltimateBitmap = Bitmap.createBitmap(mOriginalBitmapWidth, mOriginalBitmapHeight, Config.ARGB_8888); mCanvas.setBitmap(mUltimateBitmap); }
protected Request<Bitmap> makeImageRequest(String requestUrl, int maxWidth, int maxHeight, ScaleType scaleType, final String cacheKey) { return new ImageRequest(requestUrl, new Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { onGetImageSuccess(cacheKey, response); } }, maxWidth, maxHeight, scaleType, Config.RGB_565, new ErrorListener() { @Override public void onErrorResponse(VolleyError error) { onGetImageError(cacheKey, error); } }); }
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable == null) return null; if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; }
private Bitmap createColorWheelBitmap(int width, int height) { Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); int colorCount = 12; int colorAngleStep = 360 / 12; int colors[] = new int[colorCount + 1]; float hsv[] = new float[] { 0f, 1f, 1f }; for (int i = 0; i < colors.length; i++) { hsv[0] = (i * colorAngleStep + 180) % 360; colors[i] = Color.HSVToColor(hsv); } colors[colorCount] = colors[0]; SweepGradient sweepGradient = new SweepGradient(width / 2, height / 2, colors, null); RadialGradient radialGradient = new RadialGradient(width / 2, height / 2, colorWheelRadius, 0xFFFFFFFF, 0x00FFFFFF, TileMode.CLAMP); ComposeShader composeShader = new ComposeShader(sweepGradient, radialGradient, PorterDuff.Mode.SRC_OVER); colorWheelPaint.setShader(composeShader); Canvas canvas = new Canvas(bitmap); canvas.drawCircle(width / 2, height / 2, colorWheelRadius, colorWheelPaint); return bitmap; }
@Test public void load_withColorDrawable_fixedSize_functionalBitmapTransform_doesNotRecycleOutput() throws ExecutionException, InterruptedException { Drawable colorDrawable = new ColorDrawable(Color.RED); int width = 100; int height = 200; Drawable result = GlideApp.with(context) .load(colorDrawable) .circleCrop() .override(width, height) .submit() .get(); BitmapSubject.assertThat(result).isNotRecycled(); BitmapPool bitmapPool = Glide.get(context).getBitmapPool(); // Make sure we didn't put the same Bitmap twice. Bitmap first = bitmapPool.get(width, height, Config.ARGB_8888); Bitmap second = bitmapPool.get(width, height, Config.ARGB_8888); assertThat(first).isNotSameAs(second); }
private void ensureExpandedTexture() { if (this.mExpandedTitleTexture == null && !this.mExpandedBounds.isEmpty() && !TextUtils.isEmpty(this.mTextToDraw)) { calculateOffsets(0.0f); this.mTextureAscent = this.mTextPaint.ascent(); this.mTextureDescent = this.mTextPaint.descent(); int w = Math.round(this.mTextPaint.measureText(this.mTextToDraw, 0, this.mTextToDraw.length())); int h = Math.round(this.mTextureDescent - this.mTextureAscent); if (w > 0 && h > 0) { this.mExpandedTitleTexture = Bitmap.createBitmap(w, h, Config.ARGB_8888); new Canvas(this.mExpandedTitleTexture).drawText(this.mTextToDraw, 0, this.mTextToDraw.length(), 0.0f, ((float) h) - this.mTextPaint.descent(), this.mTextPaint); if (this.mTexturePaint == null) { this.mTexturePaint = new Paint(3); } } } }
/** * 将图片变为圆角 * @param bitmap 原Bitmap图片 * @param pixels 图片圆角的弧度(单位:像素(px)) * @return 带有圆角的图片(Bitmap 类型) */ public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
@Override public int countBitmaps(int width, int height, Config config) { int c = 0; synchronized (mCache) { Iterator<Bitmap> it = mCache.iterator(); while (it.hasNext()) { Bitmap bmp = it.next(); if (bmp.isRecycled()) { it.remove(); continue; } if (bmp.getWidth() == width && bmp.getHeight() == height && bmp.getConfig() == config) { c++; } } } return c; }
public synchronized Bitmap recreateIcon(Bitmap icon) { int[] offset = new int[2]; Bitmap shadow = icon.extractAlpha(mBlurPaint, offset); Bitmap result = Bitmap.createBitmap(mIconSize, mIconSize, Config.ARGB_8888); mCanvas.setBitmap(result); // Draw ambient shadow mDrawPaint.setAlpha(AMBIENT_SHADOW_ALPHA); mCanvas.drawBitmap(shadow, offset[0], offset[1], mDrawPaint); // Draw key shadow mDrawPaint.setAlpha(KEY_SHADOW_ALPHA); mCanvas.drawBitmap(shadow, offset[0], offset[1] + KEY_SHADOW_DISTANCE * mIconSize, mDrawPaint); // Draw the icon mDrawPaint.setAlpha(255); mCanvas.drawBitmap(icon, 0, 0, mDrawPaint); mCanvas.setBitmap(null); return result; }
/** * 色相处理 * * @param bitmap 原图 * @param hueValue 新的色相值 * @return 改变了色相值之后的图片 */ public static Bitmap hue(Bitmap bitmap, int hueValue) { // 计算出符合要求的色相值 float newHueValue = (hueValue - 127) * 1.0F / 127 * 180; // 创建一个颜色矩阵 ColorMatrix hueColorMatrix = new ColorMatrix(); // 控制让红色区在色轮上旋转的角度 hueColorMatrix.setRotate(0, newHueValue); // 控制让绿红色区在色轮上旋转的角度 hueColorMatrix.setRotate(1, newHueValue); // 控制让蓝色区在色轮上旋转的角度 hueColorMatrix.setRotate(2, newHueValue); // 创建一个画笔并设置其颜色过滤器 Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(hueColorMatrix)); // 创建一个新的图片并创建画布 Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); // 将原图使用给定的画笔画到画布上 canvas.drawBitmap(bitmap, 0, 0, paint); return newBitmap; }
private Bitmap getResBitmap(int bmpResId) { Options opts = new Options(); opts.inDither = false; Resources res = getResources(); Bitmap bmp = BitmapFactory.decodeResource(res, bmpResId, opts); if (bmp == null && isInEditMode()) { // BitmapFactory.decodeResource doesn't work from the rendering // library in Eclipse's Graphical Layout Editor. Use this workaround instead. Drawable d = res.getDrawable(bmpResId); int w = d.getIntrinsicWidth(); int h = d.getIntrinsicHeight(); bmp = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas c = new Canvas(bmp); d.setBounds(0, 0, w - 1, h - 1); d.draw(c); } return bmp; }
private static Bitmap createDrawableBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); if (width <= 0 || height <= 0) { return null; } float scale = Math.min(1.0f, ((float) MAX_IMAGE_SIZE) / ((float) (width * height))); if ((drawable instanceof BitmapDrawable) && scale == 1.0f) { return ((BitmapDrawable) drawable).getBitmap(); } int bitmapWidth = (int) (((float) width) * scale); int bitmapHeight = (int) (((float) height) * scale); Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Rect existingBounds = drawable.getBounds(); int left = existingBounds.left; int top = existingBounds.top; int right = existingBounds.right; int bottom = existingBounds.bottom; drawable.setBounds(0, 0, bitmapWidth, bitmapHeight); drawable.draw(canvas); drawable.setBounds(left, top, right, bottom); return bitmap; }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) public static void blurAdvanced(Context context, Bitmap bkg, View view) { if (bkg != null) { Bitmap overlay = Bitmap.createBitmap(bkg.getWidth(), bkg.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(overlay); Paint paint = new Paint(); paint.setFlags(2); canvas.drawBitmap(bkg, 0.0f, 0.0f, paint); overlay = FastBlur.doBlur(overlay, (int) 12.0f, true); if (LetvUtils.getSDKVersion() >= 16) { view.setBackground(new BitmapDrawable(context.getResources(), overlay)); } else { view.setBackgroundDrawable(new BitmapDrawable(context.getResources(), overlay)); } } }
public static Bitmap a(Drawable drawable) { int i = 1; if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int intrinsicWidth = drawable.getIntrinsicWidth(); if (intrinsicWidth <= 0) { intrinsicWidth = 1; } int intrinsicHeight = drawable.getIntrinsicHeight(); if (intrinsicHeight > 0) { i = intrinsicHeight; } Bitmap createBitmap = Bitmap.createBitmap(intrinsicWidth, i, Config.ARGB_8888); Canvas canvas = new Canvas(createBitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return createBitmap; }
private static Bitmap getBitmapFromNative(Bitmap bitmap) { int width = nativeGetBitmapWidth(); int height = nativeGetBitmapHeight(); if (bitmap == null || width != bitmap.getWidth() || height != bitmap.getHeight() || !bitmap.isMutable()) { // in Config config = Config.ARGB_8888; if (bitmap != null) { config = bitmap.getConfig(); bitmap.recycle(); } bitmap = Bitmap.createBitmap(width, height, config); } int[] pixels = new int[width]; for (int y = 0; y < height; y++) { nativeGetBitmapRow(y, pixels); bitmap.setPixels(pixels, 0, width, 0, y, width, 1); } return bitmap; }
public static Bitmap createBitmap(InputStream in, int w, int h, boolean closeStream) { if (in == null) { return null; } com.larvalabs.svgandroid.SVG svg; svg = new SVGBuilder().readFromInputStream(in).build(); Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Picture pic = svg.getPicture(); // svg.renderToCanvas(canvas/*, new RectF(0f, 0f, (float)w, (float)h)*/); // svg.renderToCanvas(canvas, new RectF(0f, 0f, (float)w, (float)h)); canvas.drawPicture(pic, new Rect(0, 0, w, h)); if (closeStream) { try { in.close(); } catch (IOException e) { throw new RuntimeException(e); } } return bitmap; }
/** * 将彩色图转换为黑白图 * * @param bmp 位图 * @return 返回转换好的位图 */ public static Bitmap convertToBlackWhite(Bitmap bmp) { int width = bmp.getWidth(); int height = bmp.getHeight(); int[] pixels = new int[width * height]; bmp.getPixels(pixels, 0, width, 0, 0, width, height); int alpha = 0xFF << 24; // 默认将bitmap当成24色图片 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int grey = pixels[width * i + j]; int red = ((grey & 0x00FF0000) >> 16); int green = ((grey & 0x0000FF00) >> 8); int blue = (grey & 0x000000FF); grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11); grey = alpha | (grey << 16) | (grey << 8) | grey; pixels[width * i + j] = grey; } } Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565); newBmp.setPixels(pixels, 0, width, 0, 0, width, height); return newBmp; }
private static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int roundPixels, Rect srcRect, Rect destRect, int width, int height) { Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); final Paint paint = new Paint(); final RectF destRectF = new RectF(destRect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(0xFF000000); canvas.drawRoundRect(destRectF, roundPixels, roundPixels, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, srcRect, destRectF, paint); return output; }
/** * 饱和度处理 * * @param bitmap 原图 * @param saturationValue 新的饱和度值 * @return 改变了饱和度值之后的图片 */ public static Bitmap saturation(Bitmap bitmap, int saturationValue) { // 计算出符合要求的饱和度值 float newSaturationValue = saturationValue * 1.0F / 127; // 创建一个颜色矩阵 ColorMatrix saturationColorMatrix = new ColorMatrix(); // 设置饱和度值 saturationColorMatrix.setSaturation(newSaturationValue); // 创建一个画笔并设置其颜色过滤器 Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(saturationColorMatrix)); // 创建一个新的图片并创建画布 Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); // 将原图使用给定的画笔画到画布上 canvas.drawBitmap(bitmap, 0, 0, paint); return newBitmap; }
public Bitmap cropCircle(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
/** * @param bitmap * @return 设置涟漪的边界,涟漪在这个区域里面可见。这里可以设置四角的弧度数 */ public Bitmap cropRoundRect(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()); canvas.drawRoundRect(rectF, rippleBorderRadius, rippleBorderRadius, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { if (bitmap == null) { return null; } Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap .getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
/** * 从View获取Bitmap * * @param view View * @return Bitmap */ public static Bitmap getBitmapFromView(View view) { if (view == null) { return null; } Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom()); view.draw(canvas); return bitmap; }
/**将图片改为圆角类型 * @param bitmap * @param pixels * @return */ public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
private static Bitmap getRoundedCornerBitmap(Bitmap bitmap) { try { Bitmap createBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(createBitmap); Paint paint = new Paint(); Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); RectF rectF = new RectF(rect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(-12434878); canvas.drawRoundRect(rectF, (float) (bitmap.getWidth() / 6), (float) (bitmap .getHeight() / 6), paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); bitmap.recycle(); return createBitmap; } catch (OutOfMemoryError e) { Log.w(TAG, "Cant`t create round corner bitmap. [OutOfMemoryError] "); return null; } }
/** * 4.尺寸压缩(通过缩放图片像素来减少图片占用内存大小) * * @param bmp * @param file */ public static void sizeCompress(Bitmap bmp, File file) { // 尺寸压缩倍数,值越大,图片尺寸越小 int ratio = 8; // 压缩Bitmap到对应尺寸 Bitmap result = Bitmap.createBitmap(bmp.getWidth() / ratio, bmp.getHeight() / ratio, Config.ARGB_8888); Canvas canvas = new Canvas(result); Rect rect = new Rect(0, 0, bmp.getWidth() / ratio, bmp.getHeight() / ratio); canvas.drawBitmap(bmp, null, rect, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 把压缩后的数据存放到baos中 result.compress(Bitmap.CompressFormat.JPEG, 100, baos); try { FileOutputStream fos = new FileOutputStream(file); fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * 根据自定义的弧度生成一张圆角图片 . <br> * @author liulongzhenhai 2012-7-12 上午9:06:01 <br> * @param bitmap 图片 * @param pixels 圆角 * @return 图片 */ public static Bitmap toRoundCorner(final Bitmap bitmap, final int pixels) { final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); final Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
/** * This will generate a bitmap with the pattern as big as the rectangle we were allow to draw on. * We do this to chache the bitmap so we don't need to recreate it each time draw() is called since it takes a few milliseconds */ private void generatePatternBitmap() { if (getBounds().width() <= 0 || getBounds().height() <= 0) { return; } bitmap = Bitmap.createBitmap(getBounds().width(), getBounds().height(), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Rect r = new Rect(); boolean verticalStartWhite = true; for (int i = 0; i <= numRectanglesVertical; i++) { boolean isWhite = verticalStartWhite; for (int j = 0; j <= numRectanglesHorizontal; j++) { r.top = i * rectangleSize; r.left = j * rectangleSize; r.bottom = r.top + rectangleSize; r.right = r.left + rectangleSize; canvas.drawRect(r, isWhite ? paintWhite : paintGray); isWhite = !isWhite; } verticalStartWhite = !verticalStartWhite; } }
/** * @param formula * the formula * @param style * the style * @param size * the size * @param transparency * , if true the background is transparent * @return the generated image */ public static Bitmap createBufferedImage(String formula, int style, float size, Integer fg, Integer bg) throws ParseException { TeXFormula f = new TeXFormula(formula); TeXIcon icon = f.createTeXIcon(style, size); icon.setInsets(new Insets(2, 2, 2, 2)); int w = icon.getIconWidth(), h = icon.getIconHeight(); Bitmap image = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas g2 = new Canvas(image); if (bg != null) { Paint st = new Paint(); st.setStyle(Style.FILL_AND_STROKE); st.setColor(bg); g2.drawRect(0, 0, w, h, st); } icon.setForeground(fg == null ? Color.BLACK : fg); icon.paintIcon(g2, 0, 0); return image; }
/** * @param formula * the formula * @param style * the style * @param size * the size * @param transparency * , if true the background is transparent * @return the generated image */ public Bitmap createBufferedImage(int style, float size, Integer fg, Integer bg) throws ParseException { TeXIcon icon = createTeXIcon(style, size); icon.setInsets(new Insets(2, 2, 2, 2)); int w = icon.getIconWidth(), h = icon.getIconHeight(); Bitmap image = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas g2 = new Canvas(image); if (bg != null) { Paint st = new Paint(); st.setStyle(Style.FILL_AND_STROKE); st.setColor(bg); g2.drawRect(0, 0, w, h, st); } icon.setForeground(fg == null ? Color.BLACK : fg); icon.paintIcon(g2, 0, 0); return image; }
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } try { Bitmap bitmap = Bitmap.createBitmap(Math.max(drawable.getIntrinsicWidth(), 2), Math .max(drawable.getIntrinsicHeight(), 2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } catch (Exception e) { e.printStackTrace(); Log.w(TAG, "Failed to create bitmap from drawable!"); return null; } }
public static Bitmap c(String str) { int i = 1; Options options = new Options(); options.inJustDecodeBounds = true; options.inDither = false; options.inPurgeable = true; options.inInputShareable = true; options.inPreferredConfig = Config.RGB_565; BitmapFactory.decodeFile(str, options); if (options.outHeight > 200 || options.outWidth > 200) { i = Math.min(Math.round(((float) options.outHeight) / 200.0f), Math.round(((float) options.outWidth) / 200.0f)); } options.inJustDecodeBounds = false; options.inSampleSize = i; return BitmapFactory.decodeFile(str, options).copy(Config.ARGB_8888, false); }