您好,登錄后才能下訂單哦!
Android實(shí)現(xiàn)分享長(zhǎng)圖并且添加全圖水印
前言:
長(zhǎng)圖一般是ScrollView和ListView。
我們需要取得這兩個(gè)控件的完整顯示的圖片。原理很簡(jiǎn)單,搞一張和控件長(zhǎng)寬一致的畫(huà)布(就是創(chuàng)建一個(gè)高寬相等的bitmap)。然后調(diào)用控件的draw方法把自己畫(huà)到畫(huà)布上去。
分別貼出兩個(gè)控件的長(zhǎng)圖獲取方法
/** * 截取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(); } // 創(chuàng)建對(duì)應(yīng)大小的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實(shí)際高度 for (int i = 0; i < listView.getChildCount(); i++) { h += listView.getChildAt(i).getHeight(); } listView.getHeight()); // 創(chuàng)建對(duì)應(yīng)大小的bitmap bitmap = Bitmap.createBitmap(listView.getWidth(), h, Bitmap.Config.RGB_565); final Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); listView.draw(canvas); // 測(cè)試輸出 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; }
奉送個(gè)獲取具體view的顯示圖的方法
/** * 生成某個(gè)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; }
再奉送個(gè)生成某個(gè)LinearLayout圖片的方法
/** * 生成某個(gè)LinearLayout的圖片 * * @author gengqiquan * @date 2017/3/20 上午10:34 */ public static Bitmap getLinearLayoutBitmap(LinearLayout linearLayout) { int h = 0; // 獲取LinearLayout實(shí)際高度 for (int i = 0; i < linearLayout.getChildCount(); i++) { linearLayout.getChildAt(i).measure(0, 0); h += linearLayout.getChildAt(i).getMeasuredHeight(); } linearLayout.measure(0, 0); // 創(chuàng)建對(duì)應(yīng)大小的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; }
完了產(chǎn)品肯定會(huì)讓你在下面或者上面加上公司的logo圖片的,嗯。好人做到低,再送個(gè)拼接圖片的方法
/** *拼接圖片 * @param first 分享的長(zhǎng)圖 * @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; }
再來(lái)個(gè)添加全圖水印的方法
/** * @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 width = first.getWidth(); int height = first.getHeight(); Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); Canvas canvas = new Canvas(result); canvas.drawBitmap(first, 0, 0, null); int h = 0; while (h < height + mark.getHeight()) { canvas.drawBitmap(mark, 0, h, null); h = h + mark.getHeight(); } return result; }
坑爹的chrome有bug。一在這個(gè)位置點(diǎn)換行接向上按鍵就崩潰。寫(xiě)個(gè)博客崩潰了七八次。最近真的是運(yùn)氣不好。
其實(shí)我是想說(shuō):由于最近被注入了個(gè)對(duì)象,依賴(lài)性比較強(qiáng),所以這段時(shí)間很少寫(xiě)博客了,后面補(bǔ)上。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。