Android中关于ImageView网络获取的图片的缩放问题

公司的项目要求从网络获取的图片,显示时宽度与屏幕的宽度差不多,高度要按照比例来缩放。

   方法一。 用xml配置

用xml里面的配置宽度最大化,自动缩放:

<ImageView

android:id="@+id/imageview_node_photo"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:scaleType="fitCenter"

/>  

在代码里面:用 imageview_node_photo.setBackgroundDrawable(imageDrawable)  (说明:imageDrawable是网络获取的Drawable图片),发现宽度是拉伸了,但高度没有变; 用 imageview_node_photo.setImageDrawable(imageDrawable),发现宽度与高度都没有变; 就是把:android:scaleType="fitXY"效果也是差不多。 失败!

  方法二:用Bitmap图片的缩放

  用xml里面的配置宽度与高度内容包裹:

<ImageView

android:id="@+id/imageview_node_photo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>  

 写了一个图片的缩放函数:根据屏幕的宽度screenWidth按比例等到高度,再输出图片

public static Drawable resizeImage(Bitmap bitmap, int screenWidth) {

     int width = bitmap.getWidth();

     int height = bitmap.getHeight();

     int newWidth = screenWidth;

     int newHeight = screenWidth*height/width; // 根据屏幕的宽度,计算按比较缩放后的高度

     

     Log.i(TAG,"width图片原始宽度:" + String.valueOf(width));

     Log.i(TAG,"height图片原始高度:" + String.valueOf(height));   

     


     // calculate the scale

     float scaleWidth = ((float) newWidth) / width;

     float scaleHeight = ((float) newHeight) / height;


     // create a matrix for the manipulation

     Matrix matrix = new Matrix();

     // resize the Bitmap

     matrix.postScale(scaleWidth, scaleHeight);

     // if you want to rotate the Bitmap

     // matrix.postRotate(45);

     // recreate the new Bitmap

     Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

     Log.i(TAG,"width图片缩放后宽度:" + resizedBitmap.getWidth());

     Log.i(TAG,"height图片缩放后高度:" + resizedBitmap.getHeight());

     // make a Drawable from Bitmap to allow to set the Bitmap

     // to the ImageView, ImageButton or what ever

     return new BitmapDrawable(resizedBitmap);

   图片虽然比以前按比例是放大了点,但图片的显示宽度与屏幕宽度还是相差太远,我怀疑与手机的像素密度density有关系,看来还是行不通。(我试了一下,把屏幕的宽度与高度都再乘以2,虽然屏幕的宽度拉伸到了屏幕的宽度,但高度已经严重拉伸变形),失败!

 方法三:用ImageView的布局来缩放

  用xml里面的配置宽度与高度内容包裹,与方法二一样:

<ImageView

android:id="@+id/imageview_node_photo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/> 

   用代码修改ImageView的布局来缩放图片:

Bitmap bitmap = ((BitmapDrawable)imageDrawable).getBitmap(); // 说明:imageDrawable是网络获取的Drawable图片

int margin = 10 ; // 左右两边到壁的距离

if(PublicData.screenWidth >= 600) // PublicData.screenWidth是手机屏幕的宽度

{

margin = 30;

}else if(PublicData.screenWidth >= 480 )

{

margin = 20;

}else if( PublicData.screenWidth >= 320 )

{

margin = 15;

}else

{

margin = 12;

}

int drawableWidth = PublicData.screenWidth - margin; // margin是左右两边到壁的距离

int drawableHeight = drawableWidth *  bitmap.getHeight() / bitmap.getWidth();

Log.i(TAG,"PublicData.screenWidth=" + PublicData.screenWidth + " imageVieLayout.height=" + drawableWidth + " imageVieLayout.Width=" + drawableHeight );

LayoutParams params = new LayoutParams(drawableWidth, drawableHeight);

// layout里面的margin特性: public void setMargins(int left, int top, int right, int bottom)

params.setMargins(2, 20, 0, 0);

imageview_node_photo.setLayoutParams(params);

imageview_node_photo.setBackgroundDrawable(imageDrawable);


一切ok!搞定!(缺点:边距不好控制)


在《 Android中关于ImageView网络获取的图片的缩放问题》里面发现,最好的方法三还是有点小缺陷,就是边距不是很好控制。还是回到方法二来,问过一个做Android底层仿iphone界面的朋友,他说他们以前做图片的时候是要乘一个比例,找了他以前的代码也没有找到。我到网络上搜索了一下,发现应该与前面我的猜测像素密度density有关,网上找了如下代码:

DisplayMetrics metrics = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(metrics);

Log.i(TAG, "metrics.density=" + metrics.density + " metrics.densityDpi=" + metrics.densityDpi + " metrics.heightPixels=" + metrics.heightPixels + "\n metrics.scaledDensity=" + metrics.scaledDensity + " metrics.widthPixels=" + metrics.widthPixels + " metrics.xdpi=" + metrics.xdpi + "\n metrics.ydpi=" + metrics.ydpi );

  运行在nexus s上,metrics.density=1.5,修改方法二的代码: resizeImage(Bitmap bitmap, int screenWidth)里面的参数screenWidth传入为:屏幕像素PublicData.screenWidth * metrics.density,设置xml的边距,一切ok!


http://mantocom.i.sohu.com/blog/view/183027253.htm

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值