轉載自 : http://blog.csdn.net/gengqiquan/article/details/65938021html
領導最近以爲攜程的截屏生成長圖分享效果比較好,因此咱們也加了個;產品以爲分享出去的長圖須要加公司品牌水印,因而咱們也加了個;嗯,事件原由就是這樣。
長圖通常是ScrollView和ListView。
咱們須要取得這兩個控件的完整顯示的圖片。原理很簡單,搞一張和控件長寬一致的畫布(就是建立一個高寬相等的bitmap)。而後調用控件的draw方法把本身畫到畫布上去。
分別貼出兩個控件的長圖獲取方法android
/** * 截取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的顯示圖的方法canvas
/** * 生成某個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圖片的,嗯。好人作到低,再送個拼接圖片的方法ui
/** *拼接圖片 * @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; }
再來個添加全圖水印的方法spa
/** * @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; }
另:.net
http://www.cnblogs.com/meieiem/archive/2012/08/15/2639543.html android 實現圖片加水印code