Android實(shí)現(xiàn)分享長圖并且添加全圖水印
Android實(shí)現(xiàn)分享長圖并且添加全圖水印
前言:
長圖一般是ScrollView和ListView。
我們需要取得這兩個(gè)控件的完整顯示的圖片。原理很簡單,搞一張和控件長寬一致的畫布(就是創(chuàng)建一個(gè)高寬相等的bitmap)。然后調(diào)用控件的draw方法把自己畫到畫布上去。
分別貼出兩個(gè)控件的長圖獲取方法
/** * 截取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 分享的長圖 * @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; }
再來個(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; }
其實(shí)我是想說:由于最近被注入了個(gè)對(duì)象,依賴性比較強(qiáng),所以這段時(shí)間很少寫博客了,后面補(bǔ)上。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
android全屏去掉title欄的多種實(shí)現(xiàn)方法
android全屏去掉title欄包括以下幾個(gè)部分:實(shí)現(xiàn)應(yīng)用中的所有activity都全屏/實(shí)現(xiàn)單個(gè)activity全屏/實(shí)現(xiàn)單個(gè)activity去掉title欄/自定義標(biāo)題內(nèi)容/自定義標(biāo)題布局等等感興趣的可參考下啊2013-02-02Android開發(fā)中ImageLoder進(jìn)行圖片加載和緩存
這篇文章主要介紹了Android開發(fā)中ImageLoder進(jìn)行圖片加載和緩存的相關(guān)資料,需要的朋友可以參考下2016-04-04Android編程四大組件之BroadcastReceiver(廣播接收者)用法實(shí)例
這篇文章主要介紹了Android編程四大組件之BroadcastReceiver(廣播接收者)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了BroadcastReceiver的功能.定義,用法及相關(guān)使用技巧,需要的朋友可以參考下2016-01-01Android 讀取sdcard上的圖片實(shí)例(必看)
下面小編就為大家?guī)硪黄狝ndroid 讀取sdcard上的圖片實(shí)例(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03Android studio刪除Android項(xiàng)目方法
在本篇內(nèi)容里我們給大家介紹的是關(guān)于Android studio刪除Android項(xiàng)目方法和步驟,需要的可以學(xué)習(xí)下。2018-12-12Android 判斷當(dāng)前網(wǎng)絡(luò)是否可用簡單實(shí)例
這篇文章主要介紹了Android 判斷當(dāng)前網(wǎng)絡(luò)是否可用簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06Android內(nèi)存優(yōu)化操作方法梳理總結(jié)
這篇文章主要介紹了Android 內(nèi)存優(yōu)化知識(shí)點(diǎn)梳理總結(jié),Android 操作系統(tǒng)給每個(gè)進(jìn)程都會(huì)分配指定額度的內(nèi)存空間,App 使用內(nèi)存來進(jìn)行快速的文件訪問交互,長時(shí)間如此便需要優(yōu)化策略,文章分享優(yōu)化知識(shí)點(diǎn)總結(jié),需要的朋友可以參考一下2022-11-11Android開發(fā)中判斷手機(jī)是否安裝了QQ或者微信
這篇文章主要介紹了Android開發(fā)中判斷手機(jī)是否安裝了QQ或者微信的相關(guān)資料,需要的朋友可以參考下2017-01-01