zoukankan      html  css  js  c++  java
  • 当Bitmap的宽度大于ImageView的最大显示宽度时对ImageView的宽高重新计算来适应Bitmap的宽高(转)

    当bitmap的宽大于ImageView显示控件的宽度时,如果不对ImageView的宽高重新计算,那么Imageview的高度就会超过显示图片的高度,用户体验将会很差,所以我们需要重新计算显示控件的宽高,这里我们假定ImageView的最大宽度为屏幕的宽度。利用下面这个工具类就可以很好解决问题了:

    Android获取屏幕宽度和高度:
    // WindowManager manage=getWindowManager();
    // Display display=manage.getDefaultDisplay();
    // screenHeight=display.getHeight();
    // screenWidth=display.getWidth();

    InputStream is = getResources().openRawResource(R.drawable.sy);

    Bitmap mBitmap = BitmapFactory.decodeStream(is);
    LinearLayout.LayoutParams lp=getLayoutParams(mBitmap,screenWidth);
    imageView1.setLayoutParams(lp);
    imageView1.setImageBitmap(mBitmap);

    public static LinearLayout.LayoutParams getLayoutParams(Bitmap bitmap,int screenWidth){
    float rawWidth = bitmap.getWidth();
    float rawHeight = bitmap.getHeight();
    float width = 0;
    float height = 0;
    Log.i("hello", "原始图片高度:"+rawHeight+"原始图片宽度:"+rawWidth);
    Log.i("hello", "原始高宽比:"+(rawHeight/rawWidth));
    if(rawWidth > screenWidth){
    height = (rawHeight/rawWidth)*screenWidth;
    width = screenWidth;
    }else{
    width = rawWidth;
    height = rawHeight;
    }
    Log.i("hello", "处理后图片高度:"+height+"处理后图片宽度:"+width);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int)width, (int)height);
    return layoutParams;
    }

  • 相关阅读:
    进程间通讯----消息队列和共享内存方式的实现
    初探 Yii2 的测试模式 index-test.php
    nginx缓存功能的设置
    php五大运行模式CGI,FAST-CGI,CLI,ISAPI,APACHE模式
    workerman如何写mysql连接池
    Varnish 一般是放在 Nginx 前面还是后面的?
    关于PATH_INFO
    Java8 Lambda表达式
    synchronized的一些记录
    类和实例
  • 原文地址:https://www.cnblogs.com/lucoo/p/4048190.html
Copyright © 2011-2022 走看看