Android-longPicture

图片发自简书App

最近项目中使用到了长图功能。以下是我的实现思路:将要生成长图的内容放到一个ScrollView中然后把获取到ScrollView的真实宽高绘制一个Bitmap出来。

获取Bitmap根据ScrollView的真实宽高

ScrollView只有一个孩子可以通过scrollView.getChildAt(0).getHeight()来获取真实高度.

        hight += scrollView.getChildAt(0).getHeight();
        scrollView.getChildAt(0).setBackgroundColor(
                Color.parseColor("#ffffff"));
        bitmap = Bitmap.createBitmap(scrollView.getWidth(), hight,
                Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        scrollView.draw(canvas);
        return bitmap;

一定要在onResume()之后去获取ScrollView的宽高,不然会获取失败

保存到相应位置

                String path = Environment.getExternalStorageDirectory() + "/longPic.jpg";
                File file = new File(path);
                if (file.isDirectory()) {//如果是目录不允许保存
                    return;
                }
                FileOutputStream outputStream = null;
                try {
                    outputStream = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
                    outputStream.flush();
                 
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally{
                    if(outputStream != null) {
                        try {
                            outputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1: 获取控件宽高 控件View有getHeight()和getwidth()方法可以获取宽高,但是如果直接在on...
    自由人是工程师阅读 5,902评论 0 0
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,946评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,325评论 25 709
  • ·以黄少天的成长为线索 ·原著向 ·以原著向为基础揣测私设如海 ·原著分析理论在暑假继续摸扎身于学习的海洋无法自拔...
    张张扬扬阅读 4,421评论 0 1
  • 二月春雷初声响,划破长空振云霄。和风细雨夜飘飘,天随人愿喜来到。(2018年二月春雨夜.林心有感)
    乡村居士阅读 1,774评论 0 1

友情链接更多精彩内容