在View類中的onDraw方法的參數(shù)Canvas是View繪制的背景,要將View轉(zhuǎn)換為Bitmap實際上就是讓Canvas上的繪制操作繪制到Bitmap上。
View轉(zhuǎn)化為Bitmap也稱為截屏,讓用戶看到的View視圖轉(zhuǎn)化為圖片的過程。
關(guān)于View轉(zhuǎn)化Bitmap涉及到的View類中的方法有:
protected void onDraw(Canvas canvas) public void buildDrawingCache() public void destroyDrawingCache() public Bitmap getDrawingCache() public void setDrawingCacheEnabled(boolean enabled)
下面是常見的幾個View截屏的示例:
1.View轉(zhuǎn)Bitmap
public final Bitmap screenShot(View view) { if (null == view) { throw new IllegalArgumentException("parameter can't be null."); } view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; }
2. Activity轉(zhuǎn)Bitmap,不帶狀態(tài)欄
public final Bitmap screenShot(Activity activity) { if (null == activity) { throw new IllegalArgumentException("parameter can't be null."); } View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap b1 = view.getDrawingCache(); Rect frame = new Rect(); view.getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; Point point = new Point(); activity.getWindowManager().getDefaultDisplay().getSize(point); int width = point.x; int height = point.y; Bitmap b2 = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return b2; }
3. ScrollView轉(zhuǎn)長Bitmap(類似錘子便簽的截長圖)
public final Bitmap screenShot(ScrollView scrollView) { if (null == scrollView) { throw new IllegalArgumentException("parameter can't be null."); } int height = 0; Bitmap bitmap; for (int i = 0, s = scrollView.getChildCount(); i < s; i++) { height += scrollView.getChildAt(i).getHeight(); scrollView.getChildAt(i).setBackgroundResource(android.R.drawable.screen_background_light); } bitmap = Bitmap.createBitmap(scrollView.getWidth(), height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); scrollView.draw(canvas); return bitmap; }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
文章名稱:[Android學(xué)習(xí)筆記二]View轉(zhuǎn)化Bitmap-創(chuàng)新互聯(lián)
分享鏈接:http://vcdvsql.cn/article18/cssedp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、軟件開發(fā)、定制網(wǎng)站、響應(yīng)式網(wǎng)站、網(wǎng)站營銷、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容