美文网首页
Android-longPicture

Android-longPicture

作者: erki_stwee | 来源:发表于2018-07-18 22:54 被阅读110次
Android-longPicture

最近项目中使用到了长图功能。以下是我的实现思路:将要生成长图的内容放到一个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();
                        }
                    }
                }

相关文章

  • Android-longPicture

    最近项目中使用到了长图功能。以下是我的实现思路:将要生成长图的内容放到一个ScrollView中然后把获取到Scr...

网友评论

      本文标题:Android-longPicture

      本文链接:https://www.haomeiwen.com/subject/lvgapftx.html