Android中由于设置的背景图片太大可能会导致OOM

例如mImageView.setImageBitmap();

布局文件中

android:background="@drawable/xxx.jpg"

下面提供其中的一种处理方式:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,                                                             int reqWidth, int reqHeight) {            // First decode with inJustDecodeBounds=true to check dimensions            final BitmapFactory.Options options = new BitmapFactory.Options();            options.inJustDecodeBounds = true;            BitmapFactory.decodeResource(res, resId, options);            // Calculate inSampleSize            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);            // Decode bitmap with inSampleSize set            options.inJustDecodeBounds = false;            return BitmapFactory.decodeResource(res, resId, options);        }        public static int calculateInSampleSize(                BitmapFactory.Options options, int reqWidth, int reqHeight) {            // Raw height and width of image            final int height = options.outHeight;            final int width = options.outWidth;            int inSampleSize = 1;            if (height > reqHeight || width > reqWidth) {                // Calculate ratios of height and width to requested height and width                final int heightRatio = Math.round((float) height / (float) reqHeight);                final int widthRatio = Math.round((float) width / (float) reqWidth);                // Choose the smallest ratio as inSampleSize value, this will guarantee                // a final image with both dimensions larger than or equal to the                // requested height and width.                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;            }            return inSampleSize;        }

通过在代码中对所需要加载的图片进行处理,得到的bitmap设置到控件上即可。

更多相关文章

  1. Android常用控件—TextView及其子类Button、EditText等
  2. android ImageView 控件
  3. Android控件开发之TextView
  4. android中textview控件中的文字的位置是如何调整的
  5. Android中设置控件可见与不可见详解
  6. Android下拉框控件Spinner的使用
  7. android之控件EditText学习
  8. Android 线性布局(LinearLayout)内各控件如何设置间距
  9. android刮奖控件,使用简单。

随机推荐

  1. 【转】Android中对json的解析和处理
  2. AspectJ 在 Android 中的使用
  3. 搭建 Android X86 系统,替代AVD模拟器,解决
  4. Android中的一个简单的List应用
  5. 【Android】android开发---实现屏幕旋转
  6. Android 内核与标准Linux内核的区别
  7. 2017年Android恶意软件专题报告
  8. Android 开发优化 知识点
  9. Android 左右滑动切换页面或Activity的效
  10. Android NDK调试方式之一: adb logcat