[펌] 안드로이드 이미지 라운드처리하기 및 가로세로 비율 맞춰 크기 변경하기
원문 링크 : [펌] 안드로이드 이미지 라운드처리하기
안드로이드 이미지 라운드처리하기 및 가로세로 비율 맞춰 크기 변경하기
public 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 = 10; 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; }
가로세로 비율 맞춰 크기 늘이기/줄이기(화면 가로 또는 세로 기준)
원문 링크 : [펌] 가로 세로 비율 맞춰 이미지 크기 줄이기
/** * 지정한 패스의 파일을 화면 크기에 맞게 읽어서 Bitmap을 리턴 * * @param context * application context * @param imgFilePath * bitmap file path * @return Bitmap * @throws IOException */ public static Bitmap loadBackgroundBitmap(Context context, String imgFilePath) throws Exception, OutOfMemoryError { if (!FileUtil.exists(imgFilePath)) { throw new FileNotFoundException("background-image file not found : " + imgFilePath); } // 폰의 화면 사이즈를 구한다. Display display = ((WindowManager) context .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int displayWidth = display.getWidth(); int displayHeight = display.getHeight(); // 읽어들일 이미지의 사이즈를 구한다. BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Config.RGB_565; options.inJustDecodeBounds = true; BitmapFactory.decodeFile(imgFilePath, options); // 화면 사이즈에 가장 근접하는 이미지의 스케일 팩터를 구한다. // 스케일 팩터는 이미지 손실을 최소화하기 위해 짝수로 한다. float widthScale = options.outWidth / displayWidth; float heightScale = options.outHeight / displayHeight; float scale = widthScale > heightScale ? widthScale : heightScale; if (scale >= 8) options.inSampleSize = 8; else if (scale >= 6) options.inSampleSize = 6; else if (scale >= 4) options.inSampleSize = 4; else if (scale >= 2) options.inSampleSize = 2; else options.inSampleSize = 1; options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(imgFilePath, options); }
