码迷,mamicode.com
首页 > 其他好文 > 详细

获取View的截图-将View转换为Bitmap对象

时间:2016-03-23 15:37:05      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

开发中,有时候需要获取View的截图来做动画来达到动画流程的目的,也有其他的很多用途

/**
 * 将View转换为Bitmap对象
 */
public class ViewToBitmapUtil {

    private static final String TAG = "ViewToBitmapUtil";

    public static Bitmap convertViewToBitmap(View view) {
        view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

    public static Bitmap convertViewToBitmap(View view, int bitmapWidth, int bitmapHeight) {
        Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
        view.draw(new Canvas(bitmap));
        return bitmap;
    }

    public static void getScreenRectOfView(View view, Rect outRect) {
        int pos[] = new int[2];
        view.getLocationOnScreen(pos);
        outRect.set(pos[0], pos[1], pos[0] + view.getWidth(), pos[1] + view.getHeight());
    }
}

 

获取View的截图-将View转换为Bitmap对象

标签:

原文地址:http://www.cnblogs.com/popfisher/p/5311353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!