将View存成bitmap,去色,作为LinearLayout背景,选中时为蓝色背景,未选中为灰色背景

来源:互联网 发布:学而知不足 编辑:程序博客网 时间:2024/06/05 00:30

项目背景:首先已经实现了一个折线图,可能存在N个颜色,需要将这个折线图存成bitmap,并去色,当做点击按钮的背景图片,当点击A按钮时,折线图被截图变为A的背景且整体色调是蓝色,当点击B按钮时,A的背景整体色调为灰色的。

基于

将View存成bitmap,去色,作为button背景

之后实现

如下图是实现结果:


首先完成去色,上色,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;}





0 0
原创粉丝点击