標籤:post sdn config content java bit 依賴 des ddc
尊重他人的勞動成果。轉載請標明出處:http://blog.csdn.net/gengqiquan/article/details/65938021。 本文出自:【gengqiquan的部落格】
領導近期認為攜程的截屏產生長圖分享效果比較好,所以我們也加了個。產品認為分享出去的長圖須要加公司品牌浮水印,於是我們也加了個。嗯,事件起因就是這樣。
長圖通常是ScrollView和ListView。
我們須要取得這兩個控制項的完整顯示的圖片。
原理非常easy,搞一張和控制項長寬一致的畫布(就是建立一個高寬相等的bitmap)。然後調用控制項的draw方法把自己畫到畫布上去。
分別貼出兩個控制項的長圖擷取方法
/** * 截取scrollview的螢幕 **/ public static Bitmap getScrollViewBitmap(ScrollView scrollView) { int h = 0; Bitmap bitmap; for (int i = 0; i < scrollView.getChildCount(); i++) { h += scrollView.getChildAt(i).getHeight(); } // 建立相應大小的bitmap bitmap = Bitmap.createBitmap(ScreenUtils.getScreenWidth(scrollView.getContext()), h, Bitmap.Config.ARGB_4444); final Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.parseColor("#f2f7fa")); scrollView.draw(canvas); return bitmap; }
/** * listview **/ public static Bitmap getListViewBitmap(ListView listView, String picpath) { int h = 0; Bitmap bitmap; // 擷取listView實際高度 for (int i = 0; i < listView.getChildCount(); i++) { h += listView.getChildAt(i).getHeight(); }listView.getHeight()); // 建立相應大小的bitmap bitmap = Bitmap.createBitmap(listView.getWidth(), h, Bitmap.Config.RGB_565); final Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); listView.draw(canvas); // 測試輸出 FileOutputStream out = null; try { out = new FileOutputStream(picpath); } catch (FileNotFoundException e) { e.printStackTrace(); } try { if (null != out) { bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); } } catch (IOException e) { } return bitmap; }
奉送個擷取詳細view的顯示圖的方法
/** * 產生某個view的圖片 * * @author gengqiquan * @date 2017/3/20 上午10:34 */ public static Bitmap getViewDrawingCacheBitmap(View view) { view = view.getRootView(); if (!view.isDrawingCacheEnabled()) { view.setDrawingCacheEnabled(true); } view.destroyDrawingCache(); view.buildDrawingCache(); Bitmap bm = view.getDrawingCache(); view.setDrawingCacheEnabled(false); return bm; }
再奉送個產生某個LinearLayout圖片的方法
/** * 產生某個LinearLayout的圖片 * * @author gengqiquan * @date 2017/3/20 上午10:34 */ public static Bitmap getLinearLayoutBitmap(LinearLayout linearLayout) { int h = 0; // 擷取LinearLayout實際高度 for (int i = 0; i < linearLayout.getChildCount(); i++) { linearLayout.getChildAt(i).measure(0, 0); h += linearLayout.getChildAt(i).getMeasuredHeight(); } linearLayout.measure(0, 0); // 建立相應大小的bitmap Bitmap bitmap = Bitmap.createBitmap(linearLayout.getMeasuredWidth(), h, Bitmap.Config.RGB_565); final Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); linearLayout.draw(canvas); return bitmap; }
完了產品肯定會讓你在以下或者上面加上公司的logo圖片的。嗯。好人做到低,再送個拼接圖片的方法
/***拼接圖片 * @param first 分享的長圖 * @param second 公司logo圖*@author gengqiquan*@date 2017/3/25 下午4:56*/ public static Bitmap add2Bitmap(Bitmap first, Bitmap second) { float scale = ((float) first.getWidth()) / second.getWidth(); second = ImageUtil.scaleImg(second, scale); int width = first.getWidth(); int height = first.getHeight() + second.getHeight(); Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); Canvas canvas = new Canvas(result); canvas.drawBitmap(first, 0, 0, null); canvas.drawBitmap(second, 0, first.getHeight(), null); return result; }
再來個加入全圖浮水印的方法
/** * @param first 原始圖 * @param mark 浮水印圖 * @author gengqiquan * @date 2017/3/25 下午4:58 */ public static Bitmap waterMark(Bitmap first, Bitmap mark) { float scale = ((float) first.getWidth()) / mark.getWidth(); mark = ImageUtil.scaleImg(mark, scale); int height = first.getHeight(); Canvas canvas = new Canvas(first); int h = 0; while (h < height + mark.getHeight()) { canvas.drawBitmap(mark, 0, h, null); h = h + mark.getHeight(); } return first; }
坑爹的chrome有bug。一在這個位置點換行接向上按鍵就崩潰。寫個部落格崩潰了七八次。近期真的是運氣不好。
事實上我是想說:因為近期被注入了個對象,依賴性比較強。所以這段時間非常少寫部落格了,後面補上。
有什麼建議的能夠留言喔
假設我的部落格對您有協助。請留言鼓舞下或者點個贊吧!
我建了一個QQ群(群號:121606151),用於大家討論交流Android技術問題。有興趣的能夠加下。大家一起進步。
android產生分享長圖而且加入全圖浮水印