1.android picture.drawå ç¨å¤å¤§å
å
2.BitMapåçä¸å®ç°
3.android å¦ä½å°drawableä¸çå¾çä¿åå°ç³»ç»ç¸åä¸
android picture.drawå ç¨å¤å¤§å å
ããAndroidçsetImageViewBitmapï¼Bitmap bmï¼æ¹æ³çæºç ä¸æ²¡æ建ç«æ°çbitmapï¼èæ¯ç¨å¼ç¨çæ¹å¼æ¥ä½¿ç¨bmçï¼è¿å°±å¯¼è´bm被å¤å¤å¼ç¨ï¼ãããé£ä¸å¹ å¾çå ç¨çå å大æ¦å¤å¤§å¢ï¼è¿ç¯åæ主è¦æ¯è®°å½äºæå¨æµè¯ä¸å¯å¾çå ç¨åèæ°æ¶æ¥é çä¸äºèµæåæµè¯ç»æã
ãã1.å¾çç表示æ¹æ³
ããAndroidçBitmap.Configç»åºäºbitmapçä¸ä¸ªåç´ æ对åºçåå¨æ¹å¼ï¼æRGB_ï¼ARGB_ï¼ARGB_ï¼ALPHA_8åç§ãRGB_表示çæ¯çº¢ç»¿èä¸è²åå«ç¨5,安卓android inflate源码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çé¿å®½æå åèæ¯åå¾çç尺寸ç¸ä¸è´çã
BitMapåçä¸å®ç°
æ¯è¾ç»å ¸çé®é¢æ¯ï¼ å¨åªè½å¤ä½¿ç¨2Gçå åä¸ï¼å¦ä½å®æ以ä¸æä½ï¼â ï¼å¯¹äº¿ä¸ªä¸éå¤çæ´æ°è¿è¡æåºã
â¡ï¼æ¾åºäº¿ä¸ªæ°åä¸éå¤çæ°åã
æ 论æ¯æåºè¿æ¯æ¾éå¤çæ°åé½éè¦å°è¿äº¿ä¸ªæ°åå å ¥å°å åä¸å¨å»è¿è¡æä½ï¼å¾ææ¾ï¼é¢ç®ç»åºç2Gå åéå¶è¯´æäºå¨è¿æ ·çåºæ¯ä¸æ¯ä¸è½å¤å°æææ°é½å å ¥å°å åä¸ç
* 4/ï¼* * ï¼ = 3.G
é£ä¹è¿æ¶åå°±éè¦ç¨å° BitMapç»æäº
bitMap使ç¨ä¸ä¸ªbit为0/1ä½ä¸ºmapçvalueæ¥æ è®°ä¸ä¸ªæ°åæ¯å¦åå¨,è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 å¦ä½å°drawableä¸çå¾çä¿åå°ç³»ç»ç¸åä¸
android å°drawableä¸çå¾çä¿åå°ç³»ç»ç¸åä¸çåçæ¯è¾ç®åï¼è·åå°çbitmapï¼ç¶åéè¿çcompressæ¹æ³åå°ä¸ä¸ªfileoutputstreamä¸. åéç¥MediaScannerServiceæå¾çæ件å å ¥å°±å¯ä»¥äº.
ä¿åå¾ççæ ¸å¿ä»£ç å¦ä¸ï¼
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.icon);
MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, name, "");
æè
FileOutputStream fos = openFileOutput("image", Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.JPEG, , fos);
fos.flush();
fos.close();
//åéç³»ç»éç¥æ¶æ¯
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())))ï¼
å¦ä¸ç§æ¹æ³æ¯ç´æ¥ä½¿ç¨æ件æµè¯»åï¼
InputStream is = mContext.getResources().openRawResource(PicID);
FileOutputStream fos = new FileOutputStream(LogoFilePath);
byte[] buffer = new byte[];
int count = 0;
while((count=is.read(buffer)) > 0)
{
fos.write(buffer, 0, count);
}
fos.close();
is.close();
è¿éè¦æ³¨æç®å½æéé®é¢ï¼å¨åºç¨ç¨åºAndroidManifest.xmlä¸çmanifestèç¹ä¸å å ¥android:sharedUerId="android.uid.system"è¿ä¸ªå±æ§ãç¶åæ¾å¨æºç ç¯å¢ä¸ç¼è¯ï¼å¹¶éè¿adb install çæ¹å¼è¿è¡å®è£ ãmkæ件ä¸çå±æ§æ¹ä¸ºLOCAL_CERTIFICATE :=platformã