@act262
2017-03-15T11:20:40.000000Z
字数 1634
阅读 1197
Android
Resources
体系BitmapDrawable
是怎么用的,Drawable的状态共享问题Bitmap
体系系统默认用Bitmap.Config.ARGB_8888
加载图片,每个像素需要4个字节来存储,
所以16*16
像素的图片占用内存就是16 * 16 * 4 = 1024 Byte = 1 KB
也就是64*64
像素要16KB,160*160
像素要100KB...
Bitmap占用内存和图片的体积不是正相关的,图片有压缩率,可以压的很小,但是加载到内存时是需要还原到每一个像素的,所以图片体积小不等于占用内存少.
inJustDecodeBounds
和inSampleSize
来使用合适的宽高Bitmap.Config.RGB_565
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/ic_menu_normal" android:state_pressed="false" />
<item android:drawable="@mipmap/ic_menu_pressed" android:state_pressed="true" />
</selector>
改进方式:使用tint
着色模式,减少多套不同颜色的图片
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="@mipmap/ic_menu_normal" android:tint="#D2B978" />
</item>
<item android:drawable="@mipmap/ic_menu_normal" />
</selector>
限制:API >= 21(Android L)以后才原生支持tint
着色
兼容:使用DrawableCompat
,ViewCompat
方法代码动态设置
ImageView
可以使用tint着色修改图片的颜色,支持API>=9,详细参看使用 ImageView tint 属性
<!-- API 21 以下只支持单色 -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#f00"
android:src="@mipmap/ic_search" />
<!-- tint 使用多状态颜色(ColorStateList),API<21 不支持,会Crash -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@color/text_color"
android:src="@mipmap/ic_search" />
扩展:使用着色相关的属性,tint,tintBackground,tintForground,tintMode,etc.
Shape
替代一下简单,规则的图片