如何重新设置bimpat的大小?

问答小助手 2013-04-26 10:37:30
加精
原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/2244

问题描述:

在我的程序中我需要重新设置 bitmap的大小,设置成imageview的大小,下面的代码报错了,如何改正,重新设置 bitmap的大小?
Bitmap bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), aMatrix, false);
Bitmap.createScaledBitmap(bmp, MyImageview_wt, MyImageview_ht, false);
MyImageview.setImageBitmap(bmp);


解决方案:
使用下面的代码试试:

public static Bitmap resizeBitMapImage1(String filePath, int targetWidth,
int targetHeight) {
Bitmap bitMapImage = null;
// First, get the dimensions of the image
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
double sampleSize = 0;
// Only scale if we need to
// (16384 buffer for img processing)
Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
.abs(options.outWidth - targetWidth);
if (options.outHeight * options.outWidth * 2 >= 1638) {
// Load, scaling to smallest power of 2 that'll get it <= desired
// dimensions
sampleSize = scaleByHeight ? options.outHeight / targetHeight
: options.outWidth / targetWidth;
sampleSize = (int) Math.pow(2d,
Math.floor(Math.log(sampleSize) / Math.log(2d)));
}
// Do the actual decoding
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[128];
while (true) {
try {
options.inSampleSize = (int) sampleSize;
bitMapImage = BitmapFactory.decodeFile(filePath, options);
break;
} catch (Exception ex) {
try {
sampleSize = sampleSize * 2;
} catch (Exception ex1) {
}
}
}
return bitMapImage;
}
...全文
1045 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
阳光下的孩子 2013-05-07
  • 打赏
  • 举报
回复
Jobernowl 2013-05-06
  • 打赏
  • 举报
回复
感谢分享 辛苦了 先mark以后用到再回来看
xxxoos 2013-05-06
  • 打赏
  • 举报
回复
不错不错
为了木头 2013-05-05
  • 打赏
  • 举报
回复
奖励计划经济学院派在家庭主妇在此时间段你说明理由你
u010556885 2013-05-03
  • 打赏
  • 举报
回复
能帮忙解释一下这些代码吗?非常感谢!
赵Andy 2013-05-03
  • 打赏
  • 举报
回复
为什么没有解释?
dgsgyjpeje 2013-05-02
  • 打赏
  • 举报
回复
刚好用得上这些,谢谢楼主啊~~~太好了
怪人伽利略 2013-04-29
  • 打赏
  • 举报
回复
学习。。。。。。
u010489012 2013-04-28
  • 打赏
  • 举报
回复
u010495416 2013-04-28
  • 打赏
  • 举报
回复
好好哈。。。。。。////
永恒的吻 2013-04-27
  • 打赏
  • 举报
回复
谢谢 支持一下
u010477380 2013-04-27
  • 打赏
  • 举报
回复
麦田捕手 2013-04-27
  • 打赏
  • 举报
回复
标记收藏一些
u010484949 2013-04-27
  • 打赏
  • 举报
回复
谢谢 支持一下
u010475093 2013-04-27
  • 打赏
  • 举报
回复
henhao henhao henhao
u010484551 2013-04-27
  • 打赏
  • 举报
回复
很实用的代码.好东西
phoenier 2013-04-26
  • 打赏
  • 举报
回复
学习学习代码
u010463521 2013-04-26
  • 打赏
  • 举报
回复
这比较难
detective2005 2013-04-26
  • 打赏
  • 举报
回复
很实用的代码
  • 打赏
  • 举报
回复
需要用到的,mark一个
加载更多回复(6)

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧