Android根据屏幕宽度缩放图片

对于图像的显示处理,之前关注过一篇文章:ImageView.ScaleType设置图解
http://blog.csdn.net/larryl2003/article/details/6919513,主要是通过android:scaleType来定义,
1. SetScaleType(ImageView.ScaleType.CENTER);按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示
2. SetScaleType(ImageView.ScaleType.CENTER_CROP);按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
3. setScaleType(ImageView.ScaleType.CENTER_INSIDE);将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽
4. setScaleType(ImageView.ScaleType.FIT_CENTER);把图片按比例扩大/缩小到View的宽度,居中显示
5. FIT_XY;不按比例缩放图片,目标是把图片塞满整个View。

但是,如果要让图片宽度填满屏幕宽度,而图片又不拉升变形,也就是让其适应屏幕宽度,而图片高度则相应的缩放。

 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.about_us);
        int bwidth = bitmap.getWidth();
        int bHeight = bitmap.getHeight();
        DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();


        int width = displayMetrics.widthPixels;
        int height = width * bHeight / bwidth;

        ViewGroup.LayoutParams para = about_us.getLayoutParams();
//        final float scale = getResources().getDisplayMetrics().density;
        para.height = height;
        about_us.setLayoutParams(para);

而对应的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize" />

<!--如果图片高度太高,超过屏幕高度的话建议使用scrollView-->
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/about_us"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/about_us"
            />
    </ScrollView>
</LinearLayout>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值