在Android中缩小位图大小后如何知道其大小?
我使用在Android中缩小位图大小后如何知道其大小?,android,bitmap,Android,Bitmap,我使用LoadImage方法从url加载图像,并将其存储在位图对象中。然后我缩小它的尺寸。在获得缩小的图像大小后,我将其显示在布局的ImageView中。现在我想知道图像大小是否缩小了。我该怎么查呢 public class ReduceImageActivity extends Activity { String image_URL = "http://pennapps.com/biblioteka/images/C.jpg"; /** Called when the ac
LoadImage
方法从url加载图像,并将其存储在位图对象中。然后我缩小它的尺寸。在获得缩小的图像大小后,我将其显示在布局的ImageView
中。现在我想知道图像大小是否缩小了。我该怎么查呢
public class ReduceImageActivity extends Activity {
String image_URL = "http://pennapps.com/biblioteka/images/C.jpg";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView bmImage = (ImageView) findViewById(R.id.imageView1);
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
Bitmap bm = LoadImage(image_URL, bmOptions);
getResizedBitmap(bm, 200, 200);
System.out.println("after resizing it"+bm);
bmImage.setImageBitmap(getResizedBitmap(bm, 200, 200));
}
private Bitmap LoadImage(String URL, BitmapFactory.Options options) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (IOException e1) {
}
return bitmap;
}
private InputStream OpenHttpConnection(String strURL) throws IOException {
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
} catch (Exception ex) {
}
return inputStream;
}
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
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);
System.out.println("in getresizebitmap"+resizedBitmap.toString());
return resizedBitmap;
}
}
缩小图像大小后,可以使用检查新(缩小)图像的大小;
getWidth()
getHeight()
使用->
resizedBitmap.getWidth(),resizedBitmap.getHeight()
使用@barzos建议的getWidth
,getHeight
。请记住,调用getResizedBitmap(bm,200200)
不会更改原始bm
位图,而是生成一个新的位图,因此请按如下方式使用它:
Bitmap bm = LoadImage(image_URL, bmOptions);
Bitmap resized = getResizedBitmap(bm, 200, 200);
System.out.println("after resizing [w: " + resized.getWidth() + ", h: " + resized.getHeight() + "]");
bmImage.setImageBitmap(resized);
这也消除了对
getResizedBitmap
节省CPU和内存资源的一个不必要的调用。它工作正常,不会使用CPU内存,正如您所说的那样。上面提到的将使用CPU内存,非常感谢。