項目背景:首先已經實現了一個折線圖,可能存在N個顏色,需要將這個折線圖存成bitmap,並去色,當做點擊按鈕的背景圖片,當點擊A按鈕時,折線圖被截圖變爲A的背景且整體色調是藍色,當點擊B按鈕時,A的背景整體色調爲灰色的。
基於
之後實現
如下圖是實現結果:
首先完成去色,上色,bitmapUtil工具類的實現:
/**
* @Title: getRecordImage
* @Description: 給bitmap去色
* 2014年9月5日
* </p>
* @param 需要處理的mBitmap
* @param 折線圖替換顏色 changeColor
* @param 被替換的顏色 oldColor
* @return Bitmap 去色後,上色完的圖片
* @author: aimee.zhang
*/
public static Bitmap getRecordImage(Bitmap mBitmap, int changeLineColor, int changeBgColor,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 = changeLineColor;
} else {
color =changeBgColor;// 折線圖背景色
}
// 將顏色值存在一個數組中 方便後面修改
mArrayColor[count] = color;
count++;
}
}
mBitmap = Bitmap.createBitmap(mArrayColor, mBitmapWidth, mBitmapHeight, Config.ARGB_8888);
return mBitmap;
}
/**
* @Title: subColor
* @Description: 回去顏色值重組
* <p>2014年10月11日 </p>
* @param @param color
* @param @return 設定文件
* @return String 返回類型
* @author: aimee.zhang
*/
public static String subColor(int color) {
String oldColor = "#" + Integer.toHexString(color);
return oldColor;
}
之後就是表示哪一個是當前點擊的按鈕,當移開至其他按鈕時,之前的按鈕顏色如何從藍色變成灰色。
邏輯:需要將一個LinearLayout(例子中爲:LL_lineChart)中內容截圖,存在本地,並將這張圖片作爲一個LinearLayout(例子中爲:LL_clickfirst,LL_clickcontent)的背景。
聲明兩個LinearLayout作爲標示(LL_clickfirst,LL_clickcontent),LL_clickcontent爲當前點擊的按鈕部分,LL_clickfirst前一個點擊的按鈕的背景,因此LL_clickcontent的背景顏色永遠爲藍色,LL_clickfirst背景顏色永遠爲灰色。
例如:
這四個按鈕編號1LL,2LL,3LL,4LL
實現:
private LinearLayout LL_clickfirst, LL_clickcontent;
int selectTag = 1, beforeSelectTag = 1;// 記錄點擊了哪個按鈕
聲明變量,初始值都爲第一個按鈕:
LL_clickfirst = 1;
LL_clickcontent = 1;
思路:
switch (v.getId()) {
case R.id.1:
LL_clickcontent =1LL; selectTag = 1;
break;
case R.id.2:
LL_clickcontent = 2LL; selectTag = 2;
break;
case R.id.3:
LL_clickcontent = 3LL; selectTag = 3;
break;
case R.id.4:
LL_clickcontent = 4LL; selectTag = 4;
break;
default:
break;
}
主方法中使用過程:(核心過程)
/**
* @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();
Bitmap bitmap = Bitmap.createBitmap(catchbitmap, 40, 0, catchbitmap.getWidth() - 40,
catchbitmap.getHeight() - 70);
if (catchbitmap != null) {
try {
boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); // 判斷sd卡是否存在
if (sdCardExist) // 如果SD卡存在,則獲取跟目錄
{
sdDir = Environment.getExternalStorageDirectory();// 獲取跟目錄
} else {
UIUtils.showToast(AppContentActivity.this, "SD卡內存不足", Toast.LENGTH_SHORT);
return;
}
File myCaptureFile = new File(sdDir + "/appcontent" + selectTag + ".png");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
BitmapUtil.getRecordImage(bitmap, Color.parseColor("#dddcdc"), Color.parseColor("#e6e6e6"), lineColors)
.compress(Bitmap.CompressFormat.JPEG, 80, bos);// 將LL_lineChart區域截圖,並保存
bos.flush();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
BitmapDrawable blueBitmap = new BitmapDrawable(BitmapUtil.getRecordImage(bitmap,
Color.parseColor("#d2e5fb"), Color.parseColor("#e5f2fa"), lineColors));
LL_clickfirst.setBackgroundDrawable(getGrayBitmap(beforeSelectTag));
LL_clickcontent.setBackgroundDrawable(blueBitmap);
beforeSelectTag = selectTag;
LL_clickfirst = LL_clickcontent;
} else {
UIUtils.showToast(AppContentActivity.this, "截圖失敗", Toast.LENGTH_SHORT);
Log.i("CACHE_BITMAP", "DrawingCache=null");
}
}
/**
* @Title: getGrayBitmap
* @Description: 獲取保存在本地的灰色折線圖
* <p>
* 2014年9月10日
* </p>
* @param tag是標示
* 獲取圖片的名稱
* @param @return 返回sd卡中保存的圖片
* @return BitmapDrawable 返回類型
* @author: aimee.zhang
*/
private BitmapDrawable getGrayBitmap(int tag) {
BitmapDrawable GrayBitmap = new BitmapDrawable(BitmapFactory.decodeFile(sdDir + "/appcontent" + tag + ".png"));
return GrayBitmap;
}