如何调整Android中的位图?

我从远程数据库中获取了Base64string的位图( encodedImage是用Base64表示图像的string):

 profileImage = (ImageView)findViewById(R.id.profileImage); byte[] imageAsBytes=null; try { imageAsBytes = Base64.decode(encodedImage.getBytes()); } catch (IOException e) {e.printStackTrace();} profileImage.setImageBitmap( BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length) ); 

profileImage是我的ImageView

好吧,但我必须调整这个图像之前,我的布局在我的ImageView上显示它。 我必须调整到120×120。

有人可以告诉我的代码来调整它吗?

我发现的例子不能应用于base64string获得的位图。

更改:

 profileImage.setImageBitmap( BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length) 

至:

 Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length) profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false)); 
 public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) { int width = bm.getWidth(); int height = bm.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // CREATE A MATRIX FOR THE MANIPULATION Matrix matrix = new Matrix(); // RESIZE THE BIT MAP matrix.postScale(scaleWidth, scaleHeight); // "RECREATE" THE NEW BITMAP Bitmap resizedBitmap = Bitmap.createBitmap( bm, 0, 0, width, height, matrix, false); bm.recycle(); return resizedBitmap; } 

编辑:由@aveschinibuild议,我已经添加bm.recycle(); 内存泄漏。 请注意,如果您将以前的对象用于其他目的,则应相应处理。

如果你已经有一个位图,你可以使用下面的代码来resize:

 Bitmap originalBitmap = <original initialization>; Bitmap resizedBitmap = Bitmap.createScaledBitmap( originalBitmap, newWidth, newHeight, false); 

根据纵横比进行缩放:

 float aspectRatio = yourSelectedImage.getWidth() / (float) yourSelectedImage.getHeight(); int width = 480; int height = Math.round(width / aspectRatio); yourSelectedImage = Bitmap.createScaledBitmap( yourSelectedImage, width, height, false); 

要使用高度作为宽度的基本单位,请更改为:

 int height = 480; int width = Math.round(height * aspectRatio); 

使用目标最大尺寸和宽度缩放位图,同时保持宽高比:

 int maxHeight = 2000; int maxWidth = 2000; float scale = Math.min(((float)maxHeight / bitmap.getWidth()), ((float)maxWidth / bitmap.getHeight())); Matrix matrix = new Matrix(); matrix.postScale(scale, scale); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 

试试这个代码:

 BitmapDrawable drawable = (BitmapDrawable) imgview.getDrawable(); Bitmap bmp = drawable.getBitmap(); Bitmap b = Bitmap.createScaledBitmap(bmp, 120, 120, false); 

我希望这是有用的。

有人问在这种情况下如何保持宽高比:

计算您用于缩放的因子,并将其用于两个维度。 比方说,你想要一个图像的高度20%的屏幕

 int scaleToUse = 20; // this will be our percentage Bitmap bmp = BitmapFactory.decodeResource( context.getResources(), R.drawable.mypng); int sizeY = screenResolution.y * scaleToUse / 100; int sizeX = bmp.getWidth() * sizeY / bmp.getHeight(); Bitmap scaled = Bitmap.createScaledBitmap(bmp, sizeX, sizeY, false); 

为了获得屏幕分辨率,你有这个解决scheme: 以像素为单位获取屏幕尺寸

试试这个:这个函数按比例调整一个位图。 当最后一个参数设置为“X”时, newDimensionXorY被视为新的宽度,当被设置为“Y”时,新的高度。

 public Bitmap getProportionalBitmap(Bitmap bitmap, int newDimensionXorY, String XorY) { if (bitmap == null) { return null; } float xyRatio = 0; int newWidth = 0; int newHeight = 0; if (XorY.toLowerCase().equals("x")) { xyRatio = (float) newDimensionXorY / bitmap.getWidth(); newHeight = (int) (bitmap.getHeight() * xyRatio); bitmap = Bitmap.createScaledBitmap( bitmap, newDimensionXorY, newHeight, true); } else if (XorY.toLowerCase().equals("y")) { xyRatio = (float) newDimensionXorY / bitmap.getHeight(); newWidth = (int) (bitmap.getWidth() * xyRatio); bitmap = Bitmap.createScaledBitmap( bitmap, newWidth, newDimensionXorY, true); } return bitmap; } 
  public Bitmap scaleBitmap(Bitmap mBitmap) { int ScaleSize = 250;//max Height or width to Scale int width = mBitmap.getWidth(); int height = mBitmap.getHeight(); float excessSizeRatio = width > height ? width / ScaleSize : height / ScaleSize; Bitmap bitmap = Bitmap.createBitmap( mBitmap, 0, 0,(int) (width/excessSizeRatio),(int) (height/excessSizeRatio)); //mBitmap.recycle(); if you are not using mBitmap Obj return bitmap; } 
 profileImage.setImageBitmap( Bitmap.createScaledBitmap( BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length), 80, 80, false ) ); 

从API 19开始,存在位图setWidth(int width)和setHeight(int height)。 http://developer.android.com/reference/android/graphics/Bitmap.html

位图调整基于任何显示大小

 public Bitmap bitmapResize(Bitmap imageBitmap) { Bitmap bitmap = imageBitmap; float lengthbmp = bitmap.getHeight(); float widthbmp = bitmap.getWidth(); // Get Screen width DisplayMetrics displaymetrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); float hight = displaymetrics.heightPixels / 3; float width = displaymetrics.widthPixels / 3; int convertHighet = (int) hight, convertWidth = (int) width; // high length if (lengthbmp > hight) { convertHighet = (int) hight - 20; bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, convertHighet, true); } // high width if (widthbmp > width) { convertWidth = (int) width - 20; bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, convertHighet, true); } return bitmap; } 
 public static Bitmap resizeBitmapByScale( Bitmap bitmap, float scale, boolean recycle) { int width = Math.round(bitmap.getWidth() * scale); int height = Math.round(bitmap.getHeight() * scale); if (width == bitmap.getWidth() && height == bitmap.getHeight()) return bitmap; Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap)); Canvas canvas = new Canvas(target); 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; } private static Bitmap.Config getConfig(Bitmap bitmap) { Bitmap.Config config = bitmap.getConfig(); if (config == null) { config = Bitmap.Config.ARGB_8888; } return config; }