drawable 的 intrinsicWidth ,intrinsicHeight 与分辨率

参考:图片大小与分辨率的研究

intrinsic 英 [ɪnˈtrɪnzɪk]
美 [ɪnˈtrɪnzɪk]
adj. 固有的; 内在的; 本身的

intrin-sic

Drawable

intrinsicWidth ,intrinsicHeight 为drawable属性

public abstract class Drawable {

    /**
     * Returns the drawable's intrinsic width.
     * <p>
     * Intrinsic width is the width at which the drawable would like to be laid
     * out, including any inherent padding. If the drawable has no intrinsic
     * width, such as a solid color, this method returns -1.
     *
     * @return the intrinsic width, or -1 if no intrinsic width
     */
    public int getIntrinsicWidth() {
        return -1;
    }

    /**
     * Returns the drawable's intrinsic height.
     * <p>
     * Intrinsic height is the height at which the drawable would like to be
     * laid out, including any inherent padding. If the drawable has no
     * intrinsic height, such as a solid color, this method returns -1.
     *
     * @return the intrinsic height, or -1 if no intrinsic height
     */
    public int getIntrinsicHeight() {
        return -1;
    }
}

测试


    <ImageView
        android:id="@+id/iv"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorAccent"
        android:contentDescription="简单的图片"
        android:src="@mipmap/guide" />

    <ImageView
        android:id="@+id/iv2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorAccent"
        android:contentDescription="加了padding"
        android:padding="10dp"
        android:src="@mipmap/guide" />

    <ImageView
        android:id="@+id/iv3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorAccent"
        android:contentDescription="scaleType : fitXY"
        android:scaleType="fitXY"
        android:src="@mipmap/guide" />

    <ImageView
        android:id="@+id/iv4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:background="@color/colorAccent"
        android:contentDescription="加了margin"
        android:src="@mipmap/guide" />
  • ic_launcher.png
xxxhdpixxhdpixhdpihdpimdpi
宽/高(px)192144967248
比例4321.51
  • 测试手机屏幕

密度: 320dp / xhdpi / 2.0x

屏幕分辨率:720 * 1440px

  • 测试图片

宽640px * 高1136px

所有的width/height/measuredWidth/measuredHeight获取都为200(px = 100dp * 2),仅与view布局宽高和手机分辨率有关。

intrinsicWidth,intrinsicHeight:与图片原始宽高,图片放置文件夹和手机分辨率有关。

xxxhdpixxhdpixhdpihdpimdpi
比例0.50.6711.32

宽640px * 高1136px 的图片 放在mipmap-xxxhdpi

==================第一个  intrinsicWidth: 320
==================第一个  intrinsicHeight: 568
==================第2个  intrinsicWidth: 320
==================第2个  intrinsicHeight: 568
==================第③个  intrinsicWidth: 320
==================第③个  intrinsicHeight: 568
==================第肆个  intrinsicWidth: 320
==================第肆个  intrinsicHeight: 568

宽640px * 高1136px 的图片 放在mipmap-xxhdpi

==================第一个  intrinsicWidth: 427
==================第一个  intrinsicHeight: 757
==================第2个  intrinsicWidth: 427
==================第2个  intrinsicHeight: 757
==================第③个  intrinsicWidth: 427
==================第③个  intrinsicHeight: 757
==================第肆个  intrinsicWidth: 427
==================第肆个  intrinsicHeight: 757

宽640px * 高1136px 的图片 放在mipmap-xhdpi

==================第一个  intrinsicWidth: 640
==================第一个  intrinsicHeight: 1136
==================第2个  intrinsicWidth: 640
==================第2个  intrinsicHeight: 1136
==================第③个  intrinsicWidth: 640
==================第③个  intrinsicHeight: 1136
==================第肆个  intrinsicWidth: 640
==================第肆个  intrinsicHeight: 1136

宽640px * 高1136px 的图片 放在mipmap-hdpi


==================第一个  intrinsicWidth: 853
==================第一个  intrinsicHeight: 1515
==================第2个  intrinsicWidth: 853
==================第2个  intrinsicHeight: 1515
==================第③个  intrinsicWidth: 853
==================第③个  intrinsicHeight: 1515
==================第肆个  intrinsicWidth: 853
==================第肆个  intrinsicHeight: 1515

宽640px * 高1136px 的图片 放在mipmap-mdpi

==================第一个  intrinsicWidth: 1280
==================第一个  intrinsicHeight: 2272
==================第2个  intrinsicWidth: 1280
==================第2个  intrinsicHeight: 2272
==================第③个  intrinsicWidth: 1280
==================第③个  intrinsicHeight: 2272
==================第肆个  intrinsicWidth: 1280
==================第肆个  intrinsicHeight: 2272

计算

参考:

Drawable之getIntrinsicWidth()和getIntrinsicHeight()以及获取drawable的原始宽度和高度

图片原始宽度 px= 图片所在文件夹对应dpi/系统dpi*getIntrinsicWidth()

图片原始高度px = 图片所在文件夹对应dpi/系统dpi*getIntrinsicHeight()

其中,系统dpi可以通过getResources().getDisplayMetrics().densityDpi获取

图片所在文件夹及对应dpixxxhdpixxhdpixhdpihdpimdpi
ic_launcher.png宽/高(px)192144967248
比例4321.51
密度值dpi640480320240160

计算图片intrinsicWidth:

xxxhdpi : 640 = 640/320 * intrinsicWidth = 2 * intrinsicWidth
xxhdpi : 640 = 480/320 * intrinsicWidth = 1.5 * intrinsicWidth
xhdpi : 640 = 320/320 * intrinsicWidth = 1 * intrinsicWidth
hdpi : 640 = 240/320 * intrinsicWidth = 0.75 * intrinsicWidth
mdpi : 640 = 160/320 * intrinsicWidth = 0.5 * intrinsicWidth

计算可发现跟我们测试的结果一致

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值