Android實現(xiàn)分享長圖并且添加全圖水印
Android實現(xiàn)分享長圖并且添加全圖水印
前言:
長圖一般是ScrollView和ListView。
我們需要取得這兩個控件的完整顯示的圖片。原理很簡單,搞一張和控件長寬一致的畫布(就是創(chuàng)建一個高寬相等的bitmap)。然后調(diào)用控件的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(); } // 創(chuàng)建對應(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實際高度 for (int i = 0; i < listView.getChildCount(); i++) { h += listView.getChildAt(i).getHeight(); } listView.getHeight()); // 創(chuàng)建對應(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); // 測試輸出 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); // 創(chuàng)建對應(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)品肯定會讓你在下面或者上面加上公司的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 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; }
其實我是想說:由于最近被注入了個對象,依賴性比較強,所以這段時間很少寫博客了,后面補上。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
android全屏去掉title欄的多種實現(xiàn)方法
android全屏去掉title欄包括以下幾個部分:實現(xiàn)應(yīng)用中的所有activity都全屏/實現(xiàn)單個activity全屏/實現(xiàn)單個activity去掉title欄/自定義標題內(nèi)容/自定義標題布局等等感興趣的可參考下啊2013-02-02Android開發(fā)中ImageLoder進行圖片加載和緩存
這篇文章主要介紹了Android開發(fā)中ImageLoder進行圖片加載和緩存的相關(guān)資料,需要的朋友可以參考下2016-04-04Android編程四大組件之BroadcastReceiver(廣播接收者)用法實例
這篇文章主要介紹了Android編程四大組件之BroadcastReceiver(廣播接收者)用法,結(jié)合實例形式較為詳細的分析了BroadcastReceiver的功能.定義,用法及相關(guān)使用技巧,需要的朋友可以參考下2016-01-01Android 判斷當前網(wǎng)絡(luò)是否可用簡單實例
這篇文章主要介紹了Android 判斷當前網(wǎng)絡(luò)是否可用簡單實例的相關(guān)資料,需要的朋友可以參考下2017-06-06Android內(nèi)存優(yōu)化操作方法梳理總結(jié)
這篇文章主要介紹了Android 內(nèi)存優(yōu)化知識點梳理總結(jié),Android 操作系統(tǒng)給每個進程都會分配指定額度的內(nèi)存空間,App 使用內(nèi)存來進行快速的文件訪問交互,長時間如此便需要優(yōu)化策略,文章分享優(yōu)化知識點總結(jié),需要的朋友可以參考一下2022-11-11