安卓 ImageView 以屏幕宽度为宽度,按照比例显示图片

先看xml文件中的imageview空间属性设置:

 1 <ImageView 
 2                android:layout_width="wrap_content"
 3                android:layout_height="wrap_content"
 4                android:scaleType="centerCrop"
 5                android:layout_gravity="center_horizontal"
 6                android:layout_marginLeft="20dip"
 7                android:layout_marginRight="20dip"
 8                android:layout_marginTop="10dip"
 9                android:layout_marginBottom="10dip"
10                android:contentDescription="@string/about"
11                android:id="@+id/productimg"
12                />

Activity中的部分代码:

1 String imgNetUrl =Utils.PICIP+"/iot-hmi-web/iot/" + jo.optString("imagePosition").replace("\", "/");
2                 productImg.setVisibility(View.VISIBLE);
3                 int w = this.getWindowManager().getDefaultDisplay().getWidth();
4                 
5                 System.out.println("imgNetUrl = "+imgNetUrl);
6                 getNetImg(imgNetUrl,productImg,w);

int w = this.getWindowManager().getDefaultDisplay().getWidth();是获取屏幕的宽度,然后下载图片

 1 private void getNetImg(String urlStr,final ImageView v1,final int screenWith) {
 2             asyncImageLoader.loadDrawable(urlStr, new ImageCallback(){
 3                  @Override
 4                  public void onLoaded(Drawable imageDrawable) {
 5                      if(imageDrawable != null){
 6                          v1.setImageDrawable(imageDrawable);//先将图片显示出来
 7                          int dw = v1.getDrawable().getBounds().width();//获取显示图片的宽度
 8                          int dh = v1.getDrawable().getBounds().height();//获取显示图片的高度
 9                         int screenHeight = 0;//屏幕的宽度是通过方法接受过来的,而高度是要按比例计算出来
10                          if (dw!=0) {
11                              screenHeight = (screenWith)*dh/dw;//计算出来要显示的图片的高度
12                             LayoutParams params = new LayoutParams(screenWith, screenHeight);//设置按照什么形式的宽和高丽展示图片
13                             v1.setLayoutParams(params);//为控件设置属性
14                             int wOffset = dip2px(ProductInfoActivity.this,20);//将dip单位转换成px
15                             int hOffset = dip2px(ProductInfoActivity.this,10);
16                             v1.setPadding(wOffset, hOffset, wOffset, hOffset);//设置图片对于显示位置的间距
17                         }
18                          System.out.println("dw = "+dw+"....dh="+dh);
19                      }
20                  }
21 
22                 @Override
23                 public void onError(Exception e) {
24                     Log.i("EntryActivity", ""+e);
25                 }
26             });
27         
28     }

还有一个是dip转换成px的方法:

1 public static int dip2px(Context context, float dipValue){
2         final float scale = context.getResources().getDisplayMetrics().density;
3         return (int)(dipValue * scale + 0.5f);
4     } 

这个方法是从网上down下来的。网址:http://www.cnblogs.com/error404/archive/2011/11/03/2234165.html

原文地址:https://www.cnblogs.com/SeawinLong/p/4178822.html