android – 在设备上创建位图时内存不足

android – 在设备上创建位图时内存不足,第1张

概述我有高分辨率图像的问题. 我使用nodpi-drawable文件夹为1280×720图像,并使用此代码来缩放它. public static Drawable scaleDrawable(Drawable d, int width, Activity cxt) { BitmapDrawable bd = (BitmapDrawable)d; double 我有高分辨率图像的问题.

我使用nodpi-drawable文件夹为1280×720图像,并使用此代码来缩放它.

public static Drawable scaleDrawable(Drawable d,int wIDth,Activity cxt)    {        BitmapDrawable bd = (BitmapDrawable)d;        double olDWIDth = bd.getBitmap().getWIDth();        double scaleFactor = wIDth / olDWIDth;        int newHeight = (int) (d.getIntrinsicHeight() * scaleFactor);        int newWIDth = (int) (olDWIDth * scaleFactor);        Drawable drawable = new BitmapDrawable(cxt.getResources(),MainScreen.getResizedBitmap(bd.getBitmap(),newHeight,newWIDth));        BitmapDrawable bd2 = (BitmapDrawable)drawable;        return  drawable;    }    public static Bitmap getResizedBitmap(Bitmap bm,int newHeight,int newWIDth) {         int wIDth = bm.getWIDth();         int height = bm.getHeight();         float scaleWIDth = ((float) newWIDth) / wIDth;        float scaleHeight = ((float) newHeight) / height;        // create a matrix for the manipulation        Matrix matrix = new Matrix();        // resize the bit map        matrix.postscale(scaleWIDth,scaleHeight);        // recreate the new Bitmap        Bitmap resizedBitmap = Bitmap.createBitmap(bm,wIDth,height,matrix,false);        return resizedBitmap;         }

我使用该代码将图像缩放到屏幕,所以如果屏幕是320×480,图像将缩放到320并保持比例,我不在乎图像是否从底部离开屏幕.

所有它的工作正常,但尝试在xhdpi设备特别是三星galaxy Note 2与屏幕正好720×1280.

它在行中出现Out of Memory Exception崩溃:

Bitmap resizedBitmap = Bitmap.createBitmap(bm,false);

我不明白为什么,图像应该从720到720,但我的代码必须是非常糟糕的优化或其他东西.

我没有试过1080×1920设备,但它似乎也会崩溃.

在查看代码时,有人会看到一些不好的东西吗?

解决方法 使用此方法调整位图大小 –

Bitmap bm=decodeSampledBitmapFromPath(src,reqWIDth,reqHeight);

使用这个定义 –

public Bitmap decodeSampledBitmapFromPath(String path,int reqWIDth,int reqHeight) {final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodefile(path,options);options.inSampleSize = calculateInSampleSize(options,reqHeight);// Decode bitmap with inSampleSize setoptions.inJustDecodeBounds = false;Bitmap bmp = BitmapFactory.decodefile(path,options);return bmp;}}  public int calculateInSampleSize(BitmapFactory.Options options,int reqHeight) {final int height = options.outHeight;final int wIDth = options.outWIDth;int inSampleSize = 1;if (height > reqHeight || wIDth > reqWIDth) {    if (wIDth > height) {        inSampleSize = Math.round((float) height / (float) reqHeight);    } else {        inSampleSize = Math.round((float) wIDth / (float) reqWIDth);     } } return inSampleSize;}

如果您正在使用资源,则使用BitmapFactory的decodeResource方法替换方法.

public static Bitmap decodeSampledBitmapFromresource(Resources res,int resID,int reqHeight) {.........return BitmapFactory.decodeResource(res,resID,options);}
总结

以上是内存溢出为你收集整理的android – 在设备上创建位图时内存不足全部内容,希望文章能够帮你解决android – 在设备上创建位图时内存不足所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1121456.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存