在Xamarin Forms Android中使用位图时,我在加载高于通常大小的图像时出现内存不足错误。
void getImage() {
-------
imageView.setImageBitmap(Bitmap.createScaledBitmap(getBitmapFromViewUpdated(_view), (int) width, (int) height, true));
tempView = imageView;
--------
}
public static Bitmap getBitmapFromView(final View view) {
view.setLayoutParams(new LayoutParams(view.getWidth(),
view.getHeight()));
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
final Bitmap bitmap_ = Bitmap
.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap_);
view.draw(c);
return bitmap_;
}
Bitmap getBitmapFromViewUpdated(final View view) {
if (view.getWidth() == 0 || view.getHeight() == 0)
view.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
else
view.setLayoutParams(new LayoutParams(view.getWidth(),
view.getHeight()));
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, viewWidth, viewHeight);
final Bitmap bitmap_;
if(viewWidth>0 && viewHeight>0) {
bitmap_ = Bitmap
.createBitmap(viewWidth,
viewHeight, Bitmap.Config.ARGB_8888);
}
else
{
bitmap_= Bitmap
.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
}
Canvas c = new Canvas(bitmap_);
view.draw(c);
return bitmap_;
}
我试过android:largeHeap=true
,可以在使用时实现结果,但我需要知道如何修复位图大小调整。
你可以检查这两个链接,以便在xamarin android中进行有效的位图管理
https://forums.xamarin.com/discussion/5716/disposing-of-bitmaps-properly
https://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/
祝好运!