zoukankan      html  css  js  c++  java
  • 获取View的截图-将View转换为Bitmap对象

    开发中,有时候需要获取View的截图来做动画来达到动画流程的目的

    原理:将View的内容画到一个Bitmap画布上,然后取出

    下面封装了一个从View生成Bitmap的工具类

    /**
     * 将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();
         // 或者可以使用下面的方法
         // view.setDrawingCacheEnabled(true);
         // Bitmap bmp = Bitmap.createBitmap(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());
        }
    }
  • 相关阅读:
    hdu 2490 队列优化dp
    poj 1836 LIS变形
    hdu 3410 单调栈
    51nod 1437
    51nod 1215 单调栈/迭代
    51nod 1102 单调栈
    51nod 1272 思维/线段树
    51nod 1279 单调栈
    SpringMVC收藏
    今天接触枚举类型,感觉是C里面应该才有的东西
  • 原文地址:https://www.cnblogs.com/popfisher/p/5311353.html
Copyright © 2011-2022 走看看