Android 获取图片真实宽高

解析图片资源尺寸
本文介绍了一种通过Android系统的方法来获取图片资源的真实宽度和高度。首先使用`BitmapFactory.Options`设置仅读取边界信息,然后调整采样大小为1并关闭仅读取边界模式,最后通过`DensityUtil`工具类转换dp到像素单位,并记录了日志输出信息。
 Resources res = mContext.getResources();
                    BitmapFactory.Options opts = new BitmapFactory.Options();
                    opts.inJustDecodeBounds = true;
                    BitmapFactory.decodeResource(res, R.drawable.home_common_icon_default, opts);
                    opts.inSampleSize = 1;
                    opts.inJustDecodeBounds = false;

                    int w = opts.outWidth;
                    int h = opts.outHeight;
                    LogUtil.e("+++++++++++++++++++++++++++++++++++++++++++++++++:");
                    LogUtil.e("w:" + w);
                    LogUtil.e("h:" + h);
                    int width = DensityUtil.dip2px(mContext, w / 2);
                    int height = DensityUtil.dip2px(mContext, h / 2);
                    LogUtil.e("width:" + width);
                    LogUtil.e("height:" + height);
                    LogUtil.e("getDensity:" + DensityUtil.getDensity(mContext));
                    LogUtil.e("+++++++++++++++++++++++++++++++++++++++++++++++++:");

因为默认获取的不是真实宽高,是缩放比例的宽高

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值