/*** * 缩放图片并加描边 * * @param src * @param destWidth * @param destHeigth * @return */ private Bitmap zoomBitmapBorder(Bitmap src, int destWidth, int destHeigth) { String tag = "lessenBitmap"; if (src == null) { return null; } int w = src.getWidth();// 源文件的大小 int h = src.getHeight(); // calculate the scale - in this case = 0.4f float scaleWidth = ((float) destWidth - 4) / w;// 宽度缩小比例 float scaleHeight = ((float) destHeigth - 4) / h;// 高度缩小比例 Log.d(tag, "bitmap width is :" + w); Log.d(tag, "bitmap height is :" + h); Log.d(tag, "new width is :" + destWidth); Log.d(tag, "new height is :" + destHeigth); Log.d(tag, "scale width is :" + scaleWidth); Log.d(tag, "scale height is :" + scaleHeight); Matrix m = new Matrix();// 矩阵 m.postScale(scaleWidth, scaleHeight);// 设置矩阵比例 Bitmap resizedBitmap = Bitmap.createBitmap(src, 0, 0, w, h, m, true);// 直接按照矩阵的比例把源文件画入进行 Bitmap newb = Bitmap.createBitmap(destWidth, destHeigth, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图 Canvas cv = new Canvas(newb); //cv.drawColor(R.color.white); cv.drawRGB(0,128,128); cv.drawBitmap(resizedBitmap, 2, 2, null);// 设置ic_launcher的位置 // save all clip cv.save(Canvas.ALL_SAVE_FLAG);// 保存 // store cv.restore();// 存储 return getRoundedCornerBitmap(newb); } /** * 图片圆角 * @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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持网管之家。