溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

[Android學(xué)習(xí)筆記二] View轉(zhuǎn)化Bitmap

發(fā)布時(shí)間:2020-07-10 10:48:19 來(lái)源:網(wǎng)絡(luò) 閱讀:2162 作者:secondriver 欄目:移動(dòng)開(kāi)發(fā)

   在View類(lèi)中的onDraw方法的參數(shù)Canvas是View繪制的背景,要將View轉(zhuǎn)換為Bitmap實(shí)際上就是讓Canvas上的繪制操作繪制到Bitmap上。


   View轉(zhuǎn)化為Bitmap也稱為截屏,讓用戶看到的View視圖轉(zhuǎn)化為圖片的過(guò)程。


   關(guān)于View轉(zhuǎn)化Bitmap涉及到的View類(lèi)中的方法有:


   protected void onDraw(Canvas canvas)
   public void buildDrawingCache()
   public void destroyDrawingCache()
   public Bitmap getDrawingCache()
   public void setDrawingCacheEnabled(boolean enabled)


   下面是常見(jiàn)的幾個(gè)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)長(zhǎng)Bitmap(類(lèi)似錘子便簽的截長(zhǎng)圖)


 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;
    }


向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI