將View存成bitmap,去色,作爲button背景

項目背景:首先已經實現了一個折線圖,可能存在N個顏色,需要將這個折線圖存成bitmap,並去色,當做點擊按鈕的背景圖片

如下圖是實現結果:


要實現的就是紅色框圖部分

首先,建立一個util類:

/**
<span style="white-space:pre">	</span> * @Title: getRecordImage
<span style="white-space:pre">	</span> * @Description: 給bitmap去色
<span style="white-space:pre">	</span> *               2014年9月5日
<span style="white-space:pre">	</span> *               </p>
<span style="white-space:pre">	</span> * @param 需要處理的mBitmap
<span style="white-space:pre">	</span> * @param 替換顏色 changeColor
<span style="white-space:pre">	</span> * @param 被替換的顏色 oldColor
<span style="white-space:pre">	</span> * @return Bitmap  去色後,上色完的圖片
<span style="white-space:pre">	</span> * @author: aimee.zhang
<span style="white-space:pre">	</span> */
public class BitmapUtil {
	public static Bitmap getRecordImage(Bitmap mBitmap, int changeColor, String[] oldColor) {
		int mBitmapWidth = mBitmap.getWidth();
		int mBitmapHeight = mBitmap.getHeight();
		List<String> oldColorList = Arrays.asList(oldColor);
		int mArrayColor[] = new int[mBitmapWidth * mBitmapHeight];
		int count = 0;
		for (int i = 0; i < mBitmapHeight; i++) {
			for (int j = 0; j < mBitmapWidth; j++) {
				// 獲得Bitmap 圖片中每一個點的color顏色值
				int color = mBitmap.getPixel(j, i);
				if (oldColorList.contains(subColor(color))) {
					color = changeColor;
				} else {
					color = Color.parseColor("#e6e6e6");//折線圖背景色
				}
				// 將顏色值存在一個數組中 方便後面修改
				mArrayColor[count] = color;
				count++;
			}
		}
		
		mBitmap = Bitmap.createBitmap(mArrayColor, mBitmapWidth, mBitmapHeight, Config.ARGB_8888);
		return mBitmap;
	}

	public static String subColor(int color) {
		String oldColor = "#"+ Integer.toHexString(color);
		return oldColor;

	}

}


在主方法中使用如下:


/**
	 * @Title: ChartBitmap
	 * @Description: 獲取LL_lineChart的截圖,保存bitmap,將截圖去色,再在保留的輪廓中填上設計顏色 2014年9月5日
	 * @author: aimee.zhang
	 */
	private void ChartBitmap() {
		LL_lineChart.destroyDrawingCache();
		LL_lineChart.setDrawingCacheEnabled(true);
		LL_lineChart.buildDrawingCache();
		Bitmap catchbitmap = LL_lineChart.getDrawingCache();

		try {
			File sdDir = null;
			boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); // 判斷sd卡是否存在
			if (sdCardExist) // 如果SD卡存在,則獲取跟目錄
			{
				sdDir = Environment.getExternalStorageDirectory();// 獲取跟目錄
			}
			File myCaptureFile = new File(sdDir + "/aa.jpg");
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
			LL_lineChart.getDrawingCache().compress(Bitmap.CompressFormat.JPEG,
					 80, bos);//將LL_lineChart區域截圖,並保存
			bos.flush();
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (catchbitmap != null) {
			Bitmap bitmap = Bitmap.createBitmap(catchbitmap, 40, 0, catchbitmap.getWidth() - 40,
					catchbitmap.getHeight() - 70);
			LL_appserver_respTime.setBackgroundDrawable(new BitmapDrawable(BitmapUtil.getRecordImage(bitmap,
					Color.parseColor("#dddcdc"), lineColors)));//將LL_appserver_respTime的背景設置成經過去色後上色的bitmap
		} else {
			Log.i("CACHE_BITMAP", "DrawingCache=null");
		}

	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章