Android View轉化Bitmap

來源:互聯網
上載者:User

標籤:android   截屏   bitmap   

   在View類中的onDraw方法的參數Canvas是View繪製的背景,要將View轉換為Bitmap實際上就是讓Canvas上的繪製操作繪製到Bitmap上。


   View轉化為Bitmap也稱為截屏,讓使用者看到的View視圖轉化為圖片的過程。


   關於View轉化Bitmap涉及到的View類中的方法有:


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


   下面是常見的幾個View截屏的樣本:

  

1.View轉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轉Bitmap,不帶狀態列

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

本文出自 “野馬紅塵” 部落格,請務必保留此出處http://aiilive.blog.51cto.com/1925756/1711443

Android View轉化Bitmap

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.