需求截图出如下长图片:

已经写成一个类可直接使用,直接上代码:

/** * 项目名称:测试截屏 * 类描述:截屏 * 创建人:ZhaoGL * 创建时间:2016/4/26 13:47 */public class ScreenShot {    private static String TAG = "ScreenShot";    /** * 获取指定Activity的截屏,保存到png文件 * @param activity * @return */    public static Bitmap takeScreenShot(Activity activity) {        // View是你需要截图的View        View view = activity.getWindow().getDecorView();        view.setDrawingCacheEnabled(true);        view.buildDrawingCache();        Bitmap b1 = view.getDrawingCache();        // 获取状态栏高度        Rect frame = new Rect();        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);        int statusBarHeight = frame.top;        System.out.println(statusBarHeight);        // 获取屏幕长和高        int width = activity.getWindowManager().getDefaultDisplay().getWidth();        int height = activity.getWindowManager().getDefaultDisplay()                .getHeight();        // 去掉标题栏        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height                - statusBarHeight);        view.destroyDrawingCache();        savePic(b, "/sdcard/screen_test.png");        return b;    }    /** * 保存到sdcard * @param b * @param strFileName */    public static void savePic(Bitmap b, String strFileName) {        FileOutputStream fos = null;        try {            fos = new FileOutputStream(strFileName);            if (null != fos) {                b.compress(Bitmap.CompressFormat.PNG, 90, fos);                fos.flush();                fos.close();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    /** * 把View对象转换成bitmap * */    public static Bitmap convertViewToBitmap(View view) {        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());        view.buildDrawingCache();        Bitmap bitmap = view.getDrawingCache();        return bitmap;    }    public static Bitmap getViewBitmap(View v) {        v.clearFocus();        v.setPressed(false);        boolean willNotCache = v.willNotCacheDrawing();        v.setWillNotCacheDrawing(false);        int color = v.getDrawingCacheBackgroundColor();        v.setDrawingCacheBackgroundColor(0);        if (color != 0) {            v.destroyDrawingCache();        }        v.buildDrawingCache();        Bitmap cacheBitmap = v.getDrawingCache();        if (cacheBitmap == null) {            return null;        }        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);        v.destroyDrawingCache();        v.setWillNotCacheDrawing(willNotCache);        v.setDrawingCacheBackgroundColor(color);        return bitmap;    }    /** * 截取scrollview的屏幕 * * */    public static Bitmap getBitmapByView(ScrollView scrollView) {        int h = 0;        Bitmap bitmap = null;        // 获取listView实际高度        for (int i = 0; i < scrollView.getChildCount(); i++) {            h += scrollView.getChildAt(i).getHeight();            scrollView.getChildAt(i).setBackgroundColor(                    Color.parseColor("#ffffff"));        }        Log.d(TAG, "实际高度:" + h);        Log.d(TAG, " 高度:" + scrollView.getHeight());        // 创建对应大小的bitmap        bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,                Bitmap.Config.ARGB_8888);        final Canvas canvas = new Canvas(bitmap);        scrollView.draw(canvas);        return bitmap;    }    /** * ListView 截屏 * @param listView * @param context * @return */    public  static Bitmap createBitmap(ListView listView, Context context){        int titleHeight,width, height, rootHeight=0;        Bitmap bitmap;        Canvas canvas;        int yPos = 0;        int listItemNum;        List<View> childViews = null;        width = getDisplayMetrics(context)[0];//宽度等于屏幕宽        ListAdapter listAdapter = listView.getAdapter();        listItemNum = listAdapter.getCount();        childViews = new ArrayList<View>(listItemNum);        View itemView;        //计算整体高度:        for(int pos=0; pos < listItemNum; ++pos){            itemView = listAdapter.getView(pos, null, listView);            //measure过程            itemView.measure(View.MeasureSpec.makeMeasureSpec(width, View. MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));            childViews.add(itemView);            rootHeight += itemView.getMeasuredHeight();        }        height = rootHeight;        // 创建对应大小的bitmap        bitmap = Bitmap.createBitmap(listView.getWidth(), height,                Bitmap.Config.ARGB_8888);        //bitmap = BitmapUtil.createBitmap(width, height, Bitmap.Config.ARGB_8888);        canvas = new Canvas(bitmap);        Bitmap itemBitmap;        int childHeight;        //把每个ItemView生成图片,并画到背景画布上        for(int pos=0; pos < childViews.size(); ++pos){            itemView = childViews.get(pos);            childHeight = itemView.getMeasuredHeight();            itemBitmap = viewToBitmap(itemView,width,childHeight);            if(itemBitmap!=null){                canvas.drawBitmap(itemBitmap, 0, yPos, null);            }            yPos = childHeight +yPos;        }        canvas.save(Canvas.ALL_SAVE_FLAG);        canvas.restore();        return bitmap;    }    private static Bitmap viewToBitmap(View view, int viewWidth, int viewHeight){        view.layout(0, 0, viewWidth, viewHeight);        view.buildDrawingCache();        Bitmap bitmap = view.getDrawingCache();        return bitmap;    }    /** * 压缩图片 * @param image * @return */    public static Bitmap compressImage(Bitmap image) {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);        int options = 100;        // 循环判断如果压缩后图片是否大于100kb,大于继续压缩        while (baos.toByteArray().length / 1024 > 100) {            // 重置baos            baos.reset();            // 这里压缩options%,把压缩后的数据存放到baos中            image.compress(Bitmap.CompressFormat.JPEG, options, baos);            // 每次都减少10            options -= 10;        }        // 把压缩后的数据baos存放到ByteArrayInputStream中        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());        // 把ByteArrayInputStream数据生成图片        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);        return bitmap;    }    /** * 获取屏幕分辨率 * * @param context * @return */    public static final Integer[] getDisplayMetrics(Context context) {        DisplayMetrics dm = new DisplayMetrics();        dm = context.getApplicationContext().getResources().getDisplayMetrics();        int screenWidth = dm.widthPixels;        int screenHeight = dm.heightPixels;        return new Integer[]{screenWidth, screenHeight};    }    /** * 保存图片并插入图库 * @param context * @param bmp * @param dir 文件夹 */    public static void saveImageToGallery(Context context, Bitmap bmp,String dir) {        // 首先保存图片        File appDir = new File(Environment.getExternalStorageDirectory(), dir);        if (!appDir.exists()) {            appDir.mkdir();        }        String fileName = System.currentTimeMillis() + ".jpg";        File file = new File(appDir, fileName);        try {            FileOutputStream fos = new FileOutputStream(file);            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);            fos.flush();            fos.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        // 其次把文件插入到系统图库        try {            MediaStore.Images.Media.insertImage(context.getContentResolver(),                    file.getAbsolutePath(), fileName, null);        } catch (FileNotFoundException e) {            e.printStackTrace();        }        // 最后通知图库更新        String path = appDir + fileName;        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));    }}

调用方法很简单,下面放上例子:
例子代码下载

更多相关文章

  1. Android(安卓)定位的实现
  2. Android(安卓)版本更新的实现
  3. Java乔晓松-android中获取手机视频的缩略图
  4. Android中dip(dp)与px之间单位转换 dip2px dp转px 无context算法
  5. Android(安卓)开源框架选择
  6. 使用FileProvider共享文件
  7. Android(安卓)studio设置背景图片
  8. Android(安卓)ImageView设置图片原理
  9. getDrawable()缓存带来的问题

随机推荐

  1. Android環境搭建后新建AVD沒有可選target
  2. 【干货推荐】Android市场今非昔比,Android
  3. Android处理图片OOM的若干方法小结
  4. Android中TextView调用setText无效不显示
  5. Android 点九图机制讲解及在聊天气泡中的
  6. 【Android 开发】: Android客户端与服务
  7. 一起Talk Android吧(第二百六十六回:Androi
  8. 【Android】最新主流新闻app功能实现。仿
  9. Android百度地图——在地图上标注已知GPS
  10. Android图像识别扫名片识别技术SDK