1.Bitmap使ç¨è¯¦è§£
2.BitMapåçä¸å®ç°
3.android picture.drawå ç¨å¤å¤§å
å
Bitmap使ç¨è¯¦è§£
ç¨å°çå¾çä¸ä» ä» å æ¬.pngã.gifã.9.pngã.jpgååç§Drawable系对象ï¼è¿å æ¬ä½å¾Bitmap
å¾ççå¤çä¹ç»å¸¸æ¯å½±åçä¸ä¸ªç¨åºçé«ææ§åå¥å£®æ§ã
为ä»ä¹ä¸ç´æ¥ç¨Bitmapä¼ è¾ï¼
ä½å¾æ件è½å¥½ï¼ä½æ¯éåç¼©æ ¼å¼ï¼å ç¨è¾å¤§åå¨ç©ºé´ã
Bitmap主è¦æ¹æ³æï¼è·åå¾å宽é«ãéæ¾ï¼å¤ææ¯å¦å·²éæ¾åæ¯å¦å¯ä¿®æ¹ï¼å缩ãå建å¶å®ä½å¾çåè½
ç¨äºä»ä¸åçæ°æ®æº(å¦æ件ãè¾å ¥æµãèµæºæ件ãåèæ°ç»ãæ件æ述符ç)解æãå建Bitmap对象
å 许æ们å®ä¹å¾ç以ä½ç§æ¹å¼å¦ä½è¯»å°å åã
æ¨èé è¯»ï¼ Android - Bitmap-å ååæ
注æäºé¡¹ï¼
decodeFileDescriptoræ¯decodeFileé«æ
æ¥çæºç å¯ä»¥ç¥é
æ¿æ¢æ
建议éç¨decodeStream代æ¿decodeResourceã
å 为BitmapFactory.decodeResource å è½½çå¾çå¯è½ä¼ç»è¿ç¼©æ¾ï¼è¯¥ç¼©æ¾ç®åæ¯æ¾å¨ java å±åçï¼æçæ¯è¾ä½ï¼èä¸éè¦æ¶è java å±çå åãå æ¤ï¼å¦æ大é使ç¨è¯¥æ¥å£å è½½å¾çï¼å®¹æ导è´OOMé误ï¼BitmapFactory.decodeStream ä¸ä¼å¯¹æå è½½çå¾çè¿è¡ç¼©æ¾ï¼ç¸æ¯ä¹ä¸å ç¨å åå°ï¼æçæ´é«ã
è¿ä¸¤ä¸ªæ¥å£åæç¨å¤ï¼å¦æ对æ§è½è¦æ±è¾é«ï¼ååºè¯¥ä½¿ç¨ decodeStreamï¼å¦æ对æ§è½è¦æ±ä¸é«ï¼ä¸éè¦ Android èªå¸¦çå¾çèªéåºç¼©æ¾åè½ï¼åå¯ä»¥ä½¿ç¨ decodeResourceã
æ¨èé 读ï¼[ BitmapFactory.decodeResourceå è½½å¾ç缩å°çåå å解å³æ¹æ³
canvasåMatrixå¯å¯¹Bitmapè¿è¡æ转ãæ¾ç¼©ã平移ãåéçæä½
å¯ä»¥ç¨Bitmap.onCreateBitmapãCanvasçclipRectåclipPathççæ¹å¼
æ¨èé è¯»ï¼ androidèªå®ä¹Viewå¦ä¹ 4--å¾ååªåä¸åæ¢
对åå§åBitmap对象è¿ç¨ä¸å¯è½åççOutOfMemoryå¼å¸¸è¿è¡äºæè·ãå¦æåçäºOutOfMemoryå¼å¸¸ï¼åºç¨ä¸ä¼å´©æºï¼èæ¯å¾å°äºä¸ä¸ªé»è®¤çBitmapå¾ã
å¦æä¸è¿è¡ç¼åï¼å°½ç®¡çå°çæ¯åä¸å¼ å¾çæ件ï¼ä½æ¯ä½¿ç¨BitmapFactoryç±»çæ¹æ³æ¥å®ä¾ååºæ¥çBitmapï¼æ¯ä¸åçBitmap对象ãç¼åå¯ä»¥é¿å æ°å»ºå¤ä¸ªBitmap对象ï¼é¿å å åç浪费ã
å¦æå¾çåç´ è¿å¤§ï¼ä½¿ç¨BitmapFactoryç±»çæ¹æ³å®ä¾åBitmapçè¿ç¨ä¸ï¼éè¦å¤§äº8Mçå å空é´ï¼å°±å¿ å®ä¼åçOutOfMemoryå¼å¸¸ã
å¯ä»¥å°å¾ç缩å°ï¼ä»¥åå°è½½å ¥å¾çè¿ç¨ä¸çå åç使ç¨ï¼é¿å å¼å¸¸åçã
æ¨èé 读ï¼
Bitmap详解ä¸Bitmapçå åä¼å
BitMapåçä¸å®ç°
æ¯è¾ç»å ¸çé®é¢æ¯ï¼ å¨åªè½å¤ä½¿ç¨2Gçå åä¸ï¼å¦ä½å®æ以ä¸æä½ï¼â ï¼å¯¹äº¿ä¸ªä¸éå¤çæ´æ°è¿è¡æåºã
â¡ï¼æ¾åºäº¿ä¸ªæ°åä¸éå¤çæ°åã
æ 论æ¯æåºè¿æ¯æ¾éå¤çæ°åé½éè¦å°è¿äº¿ä¸ªæ°åå å ¥å°å åä¸å¨å»è¿è¡æä½ï¼å¾ææ¾ï¼é¢ç®ç»åºç2Gå åéå¶è¯´æäºå¨è¿æ ·çåºæ¯ä¸æ¯ä¸è½å¤å°æææ°é½å å ¥å°å åä¸ç
* 4/ï¼* * ï¼ = 3.G
é£ä¹è¿æ¶åå°±éè¦ç¨å° BitMapç»æäº
bitMap使ç¨ä¸ä¸ªbit为0/1ä½ä¸ºmapçvalueæ¥æ è®°ä¸ä¸ªæ°åæ¯å¦åå¨,安卓通用ck解析源码èmapçkeyå¼æ£æ¯è¿ä¸ªæ°åæ¬èº«ã
ç¸æ¯äºä¸è¬çæ°æ®ç»æéè¦ç¨4个byteå»åå¨æ°å¼æ¬èº«ï¼ç¸å½äºæ¯èçäº 4*8ï¼1 = åçå å空é´
bitMapä¸ä¸å®è¦ç¨bitæ°ç»,å¯ä»¥ä½¿ç¨ int,longçççåºæ¬æ°æ®ç±»åå®ç°ï¼å ä¸ºå ¶å®è´¨é½æ¯å¨bitä½ä¸åæ°æ®ï¼ç¨åªç§ç±»ååªæ¯å³å®äºæç»å®ç°åºæ¥çBitMapçå ç½®æ°ç»ä¸å个å ç´ åæ¾æ°æ®çå¤å°
ä¾å¦ï¼javaä¸çBitSet使ç¨Longæ°ç»
BitMapçå®ç°å½ç¶å°ä¸äºä½è¿ç®ï¼å æ¥æç¡®å 个常è§ä½è¿ç®ï¼è¿æ¯å®ç°BitMapçåºç¡ï¼
set(bitIndex): æ·»å æä½
1 .ç¡®å®è¯¥æ°å¤äºæ°ç»ä¸çåªä¸ªå ç´ çä½ä¸
int wordIndex = bitIndex >> 5;
å 为æç¨çæ¯int[]å®ç°ï¼æ以è¿éå³ç§» 5 ä½ï¼2^5 = ï¼
2 .ç¡®å®ç¸å¯¹äºè¯¥å ç´ ä¸çä½ç½®å移
int bitPosition = bitIndex & ((1 << 5) - 1);
è¿éç¸å½äºæ¯ bitIndex % ï¼1<<5ï¼çå模è¿ç®ï¼å 为å½å模è¿ç®çé¤æ°æ¯2ç次å¹ï¼æ以å¯ä»¥ä½¿ç¨ä»¥ä¸çä½è¿ç®æ¥è®¡ç®ï¼æåæçï¼å¯¹æ¯HashMapç容é为ä»ä¹æ»æ¯2çå¹æ¬¡æ¹çé®é¢ï¼HashMapæ±ä¸æ æ¶ä¹æ¯ä½¿ç¨ hash&(n-1)ï¼
tips: ä½è¿ç®çä¼å 级æ¯ä½äº+,-çççï¼æ以è¦å ä¸æ¬å·,é²æ¢åçä¸å¯æè¿°çé误
3 .å°è¯¥ä½ç½®1
bits[wordIndex] |= 1 << bitPosition;
ç¸å½äºæ¯å°æå®ä½ç½®å¤çbitå¼ç½®1ï¼å ¶ä»ä½ç½®ä¿æä¸åï¼ä¹å°±æ¯å°ä»¥è¿ä¸ªbitIndex为keyçä½ç½®ä¸º1
tips: è¿éæ¯åèäºç½ä¸çåä½å¤§ä½¬çæç« ,åä½ + æä½æï¼å对æ¯äºä¸BitSetçæºç ï¼
words[wordIndex] |= (1L << bitIndex);
没æåä½æä½ï¼ç´æ¥|ï¼è¿ä¸¤ä¸ªä¸æ ·åï¼çæ¡å½ç¶æ¯ä¸æ ·ç
举个æ åï¼
1 << == 1<<
1L << ==1L<<
å³å¯¹äºintålongåæ°æ®ï¼ç´æ¥å·¦ç§»å ¶ä½æ°ç¸å½äºæ¯é带äºå¯¹å ¶çå模æä½
æ»ç»ï¼ä½¿ç¨Bit-mapçææ³ï¼æ们å¯ä»¥å°åå¨ç©ºé´è¿è¡å缩ï¼èä¸å¯ä»¥å¯¹æ°åè¿è¡å¿«éæåºãå»éåæ¥è¯¢çæä½ã
Bloom Fliteræ¯Bit-mapææ³çä¸ç§æ©å±ï¼å®å¯ä»¥å¨å 许ä½é误ççåºæ¯ä¸ï¼å¤§å¤§å°è¿è¡ç©ºé´å缩ï¼æ¯ä¸ç§æ¿é误çæ¢å空é´çæ°æ®ç»æ
å½ä¸ä¸ªå ç´ å å ¥å¸éè¿æ»¤å¨ä¸çæ¶åï¼ä¼è¿è¡åªäºæä½ï¼
å½æ们éè¦å¤æä¸ä¸ªå ç´ æ¯å¦åå¨äºå¸éè¿æ»¤å¨çæ¶åï¼ä¼è¿è¡åªäºæä½ï¼
ç¶åï¼ä¸å®ä¼åºç°è¿æ ·ä¸ç§æ åµï¼ä¸åçå符串å¯è½åå¸åºæ¥çä½ç½®ç¸åï¼å¯ä»¥éå½å¢å ä½æ°ç»å¤§å°æè è°æ´æ们çåå¸å½æ°æ¥éä½æ¦çï¼,å æ¤ï¼å¸éè¿æ»¤å¨å¯è½ä¼åå¨è¯¯å¤çæ åµ
æ»ç»æ¥è¯´å°±æ¯ï¼ å¸éè¿æ»¤å¨è¯´æ个å ç´ åå¨ï¼å°æ¦çä¼è¯¯å¤ãå¸éè¿æ»¤å¨è¯´æ个å ç´ ä¸å¨ï¼é£ä¹è¿ä¸ªå ç´ ä¸å®ä¸å¨ã
Bloom Filterçåºç¨: 常ç¨äºè§£å³ç¼åç©¿éçåºæ¯ã
android picture.drawå ç¨å¤å¤§å å
ããAndroidçsetImageViewBitmapï¼Bitmap bmï¼æ¹æ³çæºç ä¸æ²¡æ建ç«æ°çbitmapï¼èæ¯ç¨å¼ç¨çæ¹å¼æ¥ä½¿ç¨bmçï¼è¿å°±å¯¼è´bm被å¤å¤å¼ç¨ï¼ãããé£ä¸å¹ å¾çå ç¨çå å大æ¦å¤å¤§å¢ï¼è¿ç¯åæ主è¦æ¯è®°å½äºæå¨æµè¯ä¸å¯å¾çå ç¨åèæ°æ¶æ¥é çä¸äºèµæåæµè¯ç»æã
ãã1.å¾çç表示æ¹æ³
ããAndroidçBitmap.Configç»åºäºbitmapçä¸ä¸ªåç´ æ对åºçåå¨æ¹å¼ï¼æRGB_ï¼ARGB_ï¼ARGB_ï¼ALPHA_8åç§ãRGB_表示çæ¯çº¢ç»¿èä¸è²åå«ç¨5,6,5个æ¯ç¹æ¥åå¨ï¼ä¸ä¸ªåç´ å ç¨äº5+6+5=个æ¯ç¹ãRGB_表示红绿èååéæåå«ç¨8,8,8,8个æ¯ç¹æ¥åå¨ï¼ä¸ä¸ªåç´ å ç¨äº8+8+8+8=个æ¯ç¹ãè¿æ ·çè¯å¦æå¾çæ¯ä»¥RGB_è¯»å ¥çï¼é£ä¹å ç¨å åç大å°å°æ¯RGB_è¯»å ¥æ¹å¼ç2åã
ããé常æ们ç»Imagviewå è½½å¾çæ¯éè¿setDrawableæè å¨xmlæ件ä¸ç¨android:srcæ¥è®¾ç½®ï¼ä»BitmapFactory.Options.inPreferredConfigç说æææ¡£å¯ä»¥çåºï¼é»è®¤çå è½½å¾ç大å°çæ¹å¼æ¯ä»¥RGB_è¯»å ¥çã
ãã2.è·åbitmapå ç¨çåèæ°
ããAPI 以ä¸å¯ä»¥ç´æ¥è·åï¼ä»¥ä¸åæ¯éè¿è·åé«åè¡æå åèç¸ä¹å¾å°çã
ããprotected int sizeOf(Bitmap data) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) { return data.getRowBytes() * data.getHeight(); } else { return data.getByteCount(); } }
ãã3.以RGB_æ¹å¼è¯»å ¥å¾ç
ããpublic Bitmap readBitMap(Context context, int resId){ BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.RGB_; opt.inPurgeable = true; opt.inInputShareable = true; //è·åèµæºå¾ç InputStream is = context.getResources().openRawResource(resId); return BitmapFactory.decodeStream(is,null,opt); }
ãã4.è·åImageViewåå ¶ä¸drawableç大å°
ããè·åImageViewåå ¶ä¸drawable大å°éè¦å¨onWindowFocusChangedè·åï¼å¨oncreateä¸è¿åçç»ææ¯0.
ããpublic void onWindowFocusChanged(boolean hasFocus){ ImageView imageView=(ImageView)findViewById(R.id.test1); Log.v("Testresult","width= "+imageView.getWidth()+" height= " +imageView.getHeight()); Log.v("Testresult","drawawidth= "+imageView.getDrawable().getBounds().width()+ " drawableheight= " +imageView.getDrawable().getBounds().height()); }
ãã5.Oncreateï¼ï¼ä¸çæµè¯ä»£ç
ããprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView=(ImageView)findViewById(R.id.test1); Drawable drawable = imageView.getDrawable(); int bitmapWidth = drawable.getIntrinsicWidth(); //this is the bitmap's width int bitmapHeight = drawable.getIntrinsicHeight(); //this is the bitmap's height Log.v("Testresult","bitmapwidth= " +bitmapWidth+" bitmapHeight= "+bitmapHeight); Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) { Log.v("Testresult","bitmap bytes are"+bitmap.getRowBytes()*bitmap.getHeight()); } else { Log.v("Testresult", "bitmap bytes are"+bitmap.getByteCount()); } bitmap=readBitMap(this, R.drawable.pic__); //api ä¹ä¸å¯ä»¥ç´æ¥è·åbitmapæå ç¨çåèæ° if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) { Log.v("Testresult","bitmap bytes are"+bitmap.getRowBytes()*bitmap.getHeight()); } else { Log.v("Testresult", "bitmap bytes are"+bitmap.getByteCount()); } }
ãã6.æµè¯ç»æ
ããå ¶ä¸åå¾ç大å°æ¯*,ææºå±å¹çå辨çæ¯*.ä»å¾ä¸å¯ä»¥çåºï¼éç¨android:srcçå è½½æ¹å¼å ç¨çåèæ°æ¯éç¨RGB_å è½½æ¹å¼ç2åãä¸=2**åèã并ä¸è½ç¶å¨ææºä¸æ¾ç¤ºçæ¶åbitmap被缩å°æ¾ç¤ºäºï¼ä½æ¯drawableçé¿å®½æå åèæ¯åå¾çç尺寸ç¸ä¸è´çã