我试图弄清楚如何构建给定Bitmap
大小的Rect
。我可以独立生成位图(如下所示),但我对Android SDK不够熟悉,无法了解要查看的库以及这两个类的关系。
Rect右/上/左/下属性如何与位图的大小相关?这些是像素吗?
public Bitmap renderFragment(Rect rect){
// step 1: collect underpants
Bitmap fullBitmap = getBitmap();
// step 2: ...
Bitmap fragment = /* do some magic */
// step 3: profit
return fragment;
}
private Bitmap getBitmap()
{
Bitmap bitmap;
String imageUrl = "http://developer.android.com/design/media/creative_vision_main.png";
bitmap = getBitmapFromURL(imageUrl);
return bitmap;
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
答案 0 :(得分:1)
如果您有原始位图,请使用原始位图创建一个新的位图。首先,您应该创建位图的边界。
Bitmap.createBitmap(Bitmap orginalBitmap, int x, int y, int width, int height) // This is part of orginal bitmap
Bitmap.createScaledBitmap(Bitmap orginalBitmap, int destWidth, int destHeight, boolean filter ) // Create new bitmap
小心内存不足......