您的位置:首页 > 移动开发 > Android开发

android让图片等比例缩放

2017-01-13 16:38 337 查看
android中,很多时候需要将图片的宽度设置为屏幕宽度,然后让高度等比例缩放。我自己写了一下,效果是想要的那样,所以记下来分享给大家。

ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.img);   //资源文件中的一张图片
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();    //获取屏幕的宽度
imageView.measure(0,0);  //这一步很重要
int height = imageView.getMeasuredHeight() * width / imageView.getMeasuredWidth();  //这里就是将高度等比例缩放,其中imageView.getMeasuredHeight()和imageView.getMeasuredWidth()是计算图片本身的宽高
imageView.setLayoutParams(new ViewGroup.LayoutParams(width, height));


关于怎样测量控件的宽高,可以参考博客http://blog.csdn.net/shirly_yy/article/details/53535438
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 图片 缩放