我正在开发一个应用程序,允许用户在位图上的受限画布内绘制。每个图形都在一个单独的位图上创建,然后将其添加到onDraw()方法的主位图中。此主位图需要大于屏幕尺寸,以便用户有足够的空间来绘制详细的场景。因此,我还让用户能够平移/缩放此主位图。
我注意到这个主位图大小会影响设备的绘图能力。此时静态设置的尺寸为3000X3000,适用于我的Galaxy 10.1 Pro平板电脑,但在我的低端Galaxy手机上非常不稳定。此应用程序不适用于手机,但问题仍然是相同的:如何动态确定主位图的尺寸,以便跨设备的性能保持一致?
答案 0 :(得分:0)
如果您想查看屏幕尺寸,请执行此操作。文档here
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
然后您可以使用该信息调整图像大小。
您可以设置o.inJustDecodeBounds = true来获取图像大小而不加载图像。如果图像很大,可以调整大小。示例代码如下。
private Bitmap getBitmap(String path) {
Uri uri = getImageUri(path);
InputStream in = null;
try {
final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
in = mContentResolver.openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) >
IMAGE_MAX_SIZE) {
scale++;
}
Log.d(TAG, "scale = " + scale + ", orig-width: " + o.outWidth + ",
orig-height: " + o.outHeight);
Bitmap b = null;
in = mContentResolver.openInputStream(uri);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);
// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
Log.d(TAG, "1th scale operation dimenions - width: " + width + ",
height: " + height);
double y = Math.sqrt(IMAGE_MAX_SIZE
/ (((double) width) / height));
double x = (y / height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
(int) y, true);
b.recycle();
b = scaledBitmap;
System.gc();
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();
Log.d(TAG, "bitmap size - width: " +b.getWidth() + ", height: " +
b.getHeight());
return b;
} catch (IOException e) {
Log.e(TAG, e.getMessage(),e);
return null;
}