zoukankan      html  css  js  c++  java
  • view保存为图片

    一、概述

    简书、微博、便签等都有将文章保存为图片的功能。笔者臆测,此功能的实现原理如下。

    二、实现

    2.1将View保存成Bitmap对象

        方法1(亲测有效)
        private Bitmap makingView2Bitmap(View view) {
            if (view == null) {
                return null;
            }
            Bitmap screenshot;
            screenshot = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(screenshot);
            view.draw(c);
            return screenshot;
        }
    

    View成员方法getDrawingCache(),其源码如下(有问题,得出来的Bitmap始终为null)下次解决

        //android源码
        /**
         * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
         *
         * @return A non-scaled bitmap representing this view or null if cache is disabled.
         *
         * @see #getDrawingCache(boolean)
         */
        public Bitmap getDrawingCache() {
            return getDrawingCache(false);
        }
    

    参考文章
    http://www.tuicool.com/articles/RvIjeib
    http://blog.csdn.net/huangbiao86/article/details/9053429/
    http://www.2cto.com/kf/201602/488933.html

    2.2将Bitmap保存成本地文件(图片)

    这个问题比较简单,就是平常的保存文件,这里只放核心代码。如不知如何保存文件,请自行百度

        bm.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file));//file为保存文件
    

    三、特别说明

    1、Bitmap.CompressFormat.JPEG据说得到的背景是黑色的,PNG得到的是白色的
    2、TextView截取时,背景默认是黑色的(这也造成我第一次保存的时候,发现缩略图是好好的(白底黑字),但是点进去查看的时候是全黑(黑底黑字))
    在截取之前,最好做如下设置:

        mTvContent.setTextColor(getResources().getColor(R.color.black));
        mTvContent.setBackgroundColor(getResources().getColor(R.color.white));
    

    这样得到的图片就是正常的白底黑字

  • 相关阅读:
    OleView.exe:查看机器上的COM 组件。
    COM中导出GUID
    进程外组件以及进程间通信方式
    拼接多个 wchar_t *
    wstring to wchar_t*
    BSTR
    GetProcAddress 使用注意事项
    C++和.net的集合类对应
    COM的一些基本概念
    Error Lookup工具
  • 原文地址:https://www.cnblogs.com/neillee/p/5407417.html
Copyright © 2011-2022 走看看