我正在尝试旋转Img(位图),通过休眠代码。它工作良好,直到5-6转,之后我得到了OME?
private void rotateImg() {
Matrix matrix = new Matrix();
matrix.postScale(curScale, curScale);
matrix.postRotate(curRotate);
try {
temp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(),
temp.getHeight(), matrix, true);
setImage.setImageBitmap(temp);
} catch (OutOfMemoryError e) {
curRotate = curRotate - 45.0f;
Toast.makeText( this,"Out Of Memory",Toast.LENGTH_LONG).show();
}
}这里的“测试”是从SDCard加载的静态位图文件。
发布于 2012-05-28 13:06:48
为什么每次都要创建位图?有什么特别的原因吗?如果没有,请使用以下代码:
private void rotateImg() {
int cx = temp.getWidth() / 2;
int cy = temp.getHeight() / 2;
matrix.preTranslate(-cx, -cy);
matrix.postRotate(curRotate);
matrix.postTranslate(cx, cy);
setImage.setImageMatrix(matrix);
}发布于 2012-05-18 19:37:03
第一个答案可能是一个潜在的解决方案。这里的问题是,您正在创建大量的位图对象(),这些对象相当大,而且无论出于什么原因,它们都不会被gc。
更好的解决方案可能是使用单个位图,并在绘制它时应用旋转/缩放。例如,如果在属于View的Canvas上绘图,rotateImg可以简单地旋转矩阵并在视图上调用invalidate,然后在视图的onDraw方法中使用画布上的void drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint)来呈现位图。文档是here。
发布于 2012-05-18 19:24:25
也许您每次调用此方法时都必须使用bitmap.recycle()方法。试试这样的东西,
private void rotateImg() {
Matrix matrix = new Matrix();
matrix.postScale(curScale, curScale);
matrix.postRotate(curRotate);
try {
temp.recycle(); //removes the memory occupied by this bitmap object
temp=null;
temp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(),
temp.getHeight(), matrix, true);
setImage.setImageBitmap(temp);
} catch (OutOfMemoryError e) {
curRotate = curRotate - 45.0f;
Toast.makeText( this,"Out Of Memory",Toast.LENGTH_LONG).show();
}
}https://stackoverflow.com/questions/10651717
复制相似问题