版权声明:本文为博主原创文章,未经博主允许不得转载。

今天遇到一个问题,一个Bitmap封装到BitmapDrawable中 ,BitmapDrawable drawable = new BitmapDrawable(bmp),

Bitmap.getWidth() != BitmapDrawable.getIntrinsicWidth().导致一些问题:

查看源代码,问题如下:

在BitmapDrawable中,给mBitmapWidth赋值时,要根据density缩放,其默认值是160,mdpi的情况:

mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;

而在Bitmap的density是240情况下,将缩放:

公式约等于为:drawableDensity * bmpWidth / bmpDensity  ======>>  160 * 72 / 240  ,所以getIntrinsicHeight()为48

在BitmapDrawable中:

  1. private void computeBitmapSize() {
  2. mBitmapWidth = mBitmap.getScaledWidth(mTargetDensity);
  3. mBitmapHeight = mBitmap.getScaledHeight(mTargetDensity);
  4. }
  private void computeBitmapSize() {
mBitmapWidth = mBitmap.getScaledWidth(mTargetDensity);
mBitmapHeight = mBitmap.getScaledHeight(mTargetDensity);
}
  1. @Override
  2. public int getIntrinsicWidth() {
  3. return mBitmapWidth;
  4. }
  5. @Override
  6. public int getIntrinsicHeight() {
  7. return mBitmapHeight;
  8. }
 @Override
public int getIntrinsicWidth() {
return mBitmapWidth;
} @Override
public int getIntrinsicHeight() {
return mBitmapHeight;
}
  1. private BitmapDrawable(BitmapState state, Resources res) {
  2. mBitmapState = state;
  3. if (res != null) {
  4. mTargetDensity = res.getDisplayMetrics().densityDpi;
  5. } else if (state != null) {
  6. mTargetDensity = state.mTargetDensity;
  7. } else {
  8. mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
  9. }
  10. setBitmap(state.mBitmap);
  11. }
private BitmapDrawable(BitmapState state, Resources res) {
mBitmapState = state;
if (res != null) {
mTargetDensity = res.getDisplayMetrics().densityDpi;
} else if (state != null) {
mTargetDensity = state.mTargetDensity;
} else {
mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
}
setBitmap(state.mBitmap);
}

在ButtonState中,mTargetDensity的值默认为:

int mTargetDensity = DisplayMetrics.DENSITY_DEFAULT;

注意:res == null时,且state != null时,mTargetDensity = state.mTargetDensity;

  1. /**
  2. * Create an empty drawable, setting initial target density based on
  3. * the display metrics of the resources.
  4. */
  5. public BitmapDrawable(Resources res) {
  6. mBitmapState = new BitmapState((Bitmap) null);
  7. mBitmapState.mTargetDensity = mTargetDensity;
  8. }
  9. /**
  10. * Create drawable from a bitmap, not dealing with density.
  11. * @deprecated Use {@link #BitmapDrawable(Resources, Bitmap)} to ensure
  12. * that the drawable has correctly set its target density.
  13. */
  14. @Deprecated
  15. public BitmapDrawable(Bitmap bitmap) {
  16. this(new BitmapState(bitmap), null);
  17. }
 /**
* Create an empty drawable, setting initial target density based on
* the display metrics of the resources.
*/
public BitmapDrawable(Resources res) {
mBitmapState = new BitmapState((Bitmap) null);
mBitmapState.mTargetDensity = mTargetDensity;
} /**
* Create drawable from a bitmap, not dealing with density.
* @deprecated Use {@link #BitmapDrawable(Resources, Bitmap)} to ensure
* that the drawable has correctly set its target density.
*/
@Deprecated
public BitmapDrawable(Bitmap bitmap) {
this(new BitmapState(bitmap), null);
}
  1. /**
  2. * Create drawable from a bitmap, setting initial target density based on
  3. * the display metrics of the resources.
  4. */
  5. public BitmapDrawable(Resources res, Bitmap bitmap) {
  6. this(new BitmapState(bitmap), res);
  7. mBitmapState.mTargetDensity = mTargetDensity;
  8. }
    /**
     * Create drawable from a bitmap, setting initial target density based on
     * the display metrics of the resources.
     */
    public BitmapDrawable(Resources res, Bitmap bitmap) {
        this(new BitmapState(bitmap), res);
        mBitmapState.mTargetDensity = mTargetDensity;
    }

其中,BitmapDrawable(Bitmap bmp)已经被弃用,如果使用 BitmapDrawable(Bitmap bmp,Resources res)构造函数

在DisplayMetrics:

  1. public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
 public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;

在Bitmap中:

  1. /**
  2. * Convenience method that returns the width of this bitmap divided
  3. * by the density scale factor.
  4. *
  5. * @param targetDensity The density of the target canvas of the bitmap.
  6. * @return The scaled width of this bitmap, according to the density scale factor.
  7. */
  8. public int getScaledWidth(int targetDensity) {
  9. return scaleFromDensity(getWidth(), mDensity, targetDensity);
  10. }
  11. /**
  12. * Convenience method that returns the height of this bitmap divided
  13. * by the density scale factor.
  14. *
  15. * @param targetDensity The density of the target canvas of the bitmap.
  16. * @return The scaled height of this bitmap, according to the density scale factor.
  17. */
  18. public int getScaledHeight(int targetDensity) {
  19. return scaleFromDensity(getHeight(), mDensity, targetDensity);
  20. }
  21. /**
  22. * @hide
  23. */
  24. static public int scaleFromDensity(int size, int sdensity, int tdensity) {
  25. if (sdensity == DENSITY_NONE || sdensity == tdensity) {
  26. return size;
  27. }
  28. // Scale by tdensity / sdensity, rounding up.
  29. return ( (size * tdensity) + (sdensity >> 1) ) / sdensity;
  30. }
   /**
* Convenience method that returns the width of this bitmap divided
* by the density scale factor.
*
* @param targetDensity The density of the target canvas of the bitmap.
* @return The scaled width of this bitmap, according to the density scale factor.
*/
public int getScaledWidth(int targetDensity) {
return scaleFromDensity(getWidth(), mDensity, targetDensity);
} /**
* Convenience method that returns the height of this bitmap divided
* by the density scale factor.
*
* @param targetDensity The density of the target canvas of the bitmap.
* @return The scaled height of this bitmap, according to the density scale factor.
*/
public int getScaledHeight(int targetDensity) {
return scaleFromDensity(getHeight(), mDensity, targetDensity);
} /**
* @hide
*/
static public int scaleFromDensity(int size, int sdensity, int tdensity) {
if (sdensity == DENSITY_NONE || sdensity == tdensity) {
return size;
} // Scale by tdensity / sdensity, rounding up.
return ( (size * tdensity) + (sdensity >> 1) ) / sdensity;
}

如此,只有做如下改动:

方法一:

BitmapDrawable bmpDrawable = new BitmapDrawable(bmp,getResources);

方法二:

BitmapDrawable bmpDrawable = new BitmapDrawable(bmp);

bmpDrawable.setTargetDensity(getResources().getResources().getDisplayMetrics());

借鉴: http://blog.csdn.net/jason_wks/article/details/8283224

Drawable的getIntrinsicHeight()和getIntrinsicWidth()的更多相关文章

  1. 玩转Android之Drawable的使用

    Drawable天天用,可你是否对Drawable家族有一个完整的认知?今天我们就来系统的学习一下Drawable的使用. 1.概述 用过Drawable的筒子都知道Drawable有很多种,有的时候 ...

  2. android的Drawable详解

    Drawable简介 Drawable有很多种,用来表示一种图像的概念,但他们又不完全是图像,他们是用过颜色构建出来的各种图像的表现形式.Drawable一般都是通过xml来定义的 ,当然我们也可以通 ...

  3. [转]自定义Drawable实现灵动的红鲤鱼动画(上篇)

    此篇中的小鱼动画是模仿国外一个大牛做的flash动画,第一眼就爱上它了,简约灵动又不失美学,于是抽空试着尝试了一下,如下是我用Android实现的效果图:   小鱼儿 由于整个绘制分析过程比较繁琐所以 ...

  4. Android 自定义列表指示器

    在联系人界面 可以看到这种界面 手指快速滑动右边滑动条时 可以显示相应的字母图标 android里提供了android.widget.SectionIndexer这个接口去实现该效果 可是只能显示字母 ...

  5. 星座物语APP

    效果图: 这里的后台管理用的是duducat,大家可以去百度看说明.图片,文字都在duducat后台服务器上,可以自己修改的.(PS:图片这里是随便找的) http://www.duducat.com ...

  6. Android静态图片人脸识别的完整demo(附完整源码)

    Demo功能:利用android自带的人脸识别进行识别,标记出眼睛和人脸位置.点击按键后进行人脸识别,完毕后显示到imageview上. 第一部分:布局文件activity_main.xml < ...

  7. Android Drawable之getIntrinsicWidth()和getIntrinsicHeight()

              在Android的开发中,凡是需要画图的地方大都离不开类Drawable.Android的官方文档中介绍这个类就是被设计用来表示可以被画的东西.A Drawable is a ge ...

  8. Drawable: getIntrinsicWidth()和getIntrinsicHeight()方法的使用误区

    经常会使用上述两个API来获取ImageView中显示图片的大小,但是在某些情况下,这两个API返回的大小可能与原图的大小不一致,比如原图大小是72*72,分别把原图放置在xhdpi,xxhdpi,x ...

  9. Android Drawable 那些不为人知的高效用法

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43752383,本文出自:[张鸿洋的博客] 1.概述 Drawable在我们平时的 ...

随机推荐

  1. JAVA实现图的邻接表以及DFS

    一:定义邻接表结构储存图 package 图的遍历; //邻接表实现图的建立 //储存边 class EdgeNode { int index; // 习惯了用index,其实标准写法是(adjVer ...

  2. [你必须知道的.NET]第十九回:对象创建始末(下)

    本文将介绍以下内容: 对象的创建过程 内存分配分析 内存布局研究 接上回[第十八回:对象创建始末(上)],继续对对象创建话题的讨论>>> 2.2 托管堆的内存分配机制 引用类型的实例 ...

  3. 【严蔚敏】【数据结构题集(C语言版)】1.17 求k阶斐波那契序列的第m项值的函数算法

    已知k阶斐波那契序列的定义为 f(0)=0,f(1)=0,...f(k-2)=0,f(k-1)=1; f(n)=f(n-1)+f(n-2)+...+f(n-k),n=k,k+1,... 试编写求k阶斐 ...

  4. python开发学习-day05(正则深入、冒泡排序算法、自定义模块、常用标准模块)

    s12-20160130-day05 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  5. 易普优APS应用案例:线束行业生产计划排产

    一.线束行业生产现状 (1)产品种类以及标准繁多,生产计划难协调 线束行业的生产,虽然原材料不多,但线束产品却多达几万种.一般线束企业,虽然不是每个月都生产数万种产品,但每月生产的产品品种在300种以 ...

  6. spring_150908_hibernate_id_sequence

    1.新建java工程:spring_150908_hibernate_id_sequence,添加相关jar包(spring.hibernate.ibatis)如下图所示: 2.实现实体类DogPet ...

  7. 双缓冲解决控制台应用程序输出“闪屏”(C/C++,Windows)

    使用 C 语言编写游戏的小伙伴们想必起初都要遇到这样的问题,在不断清屏输出数据的过程中,控制台中的输出内容会不断地闪屏.出现这个问题的原因是程序对数据处理花掉的时间影响到了数据显示,或许你可以使用局部 ...

  8. SpringBoot入门系列

    集合redis,mysql,测试例子 http://blog.csdn.net/lxhjh/article/details/51764604

  9. JSP中的9大内置对象四大域与servlet里的三大域

    九大内置对象 隐式对象 说明 out 转译后对应JspWriter对象,其内部关联一个PringWriter对象 request 转译后对应HttpServletRequest/ServletRequ ...

  10. ARGB 8888 内存大小

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 4字节  argb 各占8个bit