原理:讀取colorbar圖片乖订。將顏色變化存取到與colorbar高度相同的數(shù)組中mColorbar,只需讀取第一列顏色值即可具练。
獲取一張需要改變顏色值的bitmap. 將bitmap每個(gè)點(diǎn)的顏色值轉(zhuǎn)為灰度值乍构,再根據(jù)以下公式獲取該灰度值在colorbar的顏色數(shù)組colorarray中對(duì)應(yīng)的顏色值
代碼如下
1讀取colorbar
private int[] mColorbar;
???Bitmap mColorbitmap;
??? privatevoid readColorbar() {
?????????????????? byte[]bardata;
?????????????????? try{
??????????????????????????? InputStreamis = mContext.getResources().openRawResource(R.drawable.colorbar);
???????? ??????? ByteArrayOutputStream os = newByteArrayOutputStream(is.available());
???????? ??????? byte[] buffer = new byte[4096];
???????? ??????? int bytesRead;
???????? ??????? while ((bytesRead = is.read(buffer)) !=-1) {
???????? ??????????? os.write(buffer, 0, bytesRead);
???????? ??????? }
???????? ??????? is.close();
???????? ??????? bardata = os.toByteArray();
??????????????????????????? BitmapFactory.Optionsoptions = new BitmapFactory.Options();???????
??????????????????????????? options.inPreferredConfig= Bitmap.Config.RGB_565;???????
??????????????????????????? mColorbitmap= BitmapFactory.decodeByteArray(bardata, 0, bardata.length, options);
??????????????????????????? mColorbar = new
int [mColorbitmap.getHeight()];
??????????????????????????? for(int i = 0; i < mColorbitmap.getHeight(); i++) {
?????????????????????????????????????????????? int color =
mColorbitmap.getPixel(0, i);
?????????????????????????????????????????????? mColorbar[i]= color;
?????????????????????????????????????????????? intr = Color.red(color);
?????????????????????????????????????????????? intg = Color.green(color);
?????????????????????????????????????????????? intb = Color.blue(color);
?????????????????????????????????????????????? inta = Color.alpha(color);
?????????????????????????????????????????????? //Log.d(TAG,"readColorbar [" +i +"] r=" + r + "g=" + g+"b=" +b);
??????????????????????????? }
???????? ??? } catch (IOException e) {
??????????????????????????? Log.e(TAG,"load colorbar exception");
?????????????????? }
}
2.根據(jù)灰度值獲取顏色
Bitmap mBitmap = bitmap.copy(Bitmap.Config.RGB_565,true);??????????
?????????????????? for(int i = 0; i < bitmap.getHeight(); i++) {
???????????for (int j = 0; j < bitmap.getWidth(); j++) {
???????????????????????????????????? intcolor = bitmap.getPixel(j, i);
??????????????? /*
??????????????? int r = Color.red(color);
???????????????????????????????????? intg = Color.green(color);
???????????????????????????????????? intb = Color.blue(color);
???????????????????????????????????? inta = Color.alpha(color);
???????????????????????????????????? */
???????????????????????????????????? intr = (color&0x00FF0000)>>16;?
??????????????? int g =(color&0x0000FF00)>>8;?
??????????????? int b =(color&0x000000FF);?
??????????????? int grey =(int)((float)r*0.3+(float)g*0.59+(float)b*0.11);?
???????????????//grey =alpha|(grey<<16)|(grey<<8)|grey;
???????????????
//灰度值除以255,獲得當(dāng)前灰度比重扛点。再乘以colorbar高度哥遮。
??????????????? int
colorIndex = (grey* mColorbitmap.getHeight())/ 255 ;
??????????????? mBitmap.setPixel(j, i,
mColorbar[colorIndex]);
???????????????????????????????????? //Log.d(TAG,"onPicFrameChanged [j,i] grey = " + grey + "colorindex = "+ colorIndex);
??????????????????????????? ??? //Log.d(TAG, "onPicFrameChanged[" + j +"," + i +"] r=" + r + "g=" + g+"b=" +b);
??????????????????????????? }
?????????????????? }