Android将自定义View保存为Bitmap图片

Android将自定义View保存为Bitmap图片

 private void createPdfImg() {
        //创建待分享的view
        View view = LayoutInflater.from(getContext()).inflate(R.layout.ecg_pdf_view, null);
        PdfEcgWaveView waveView = view.findViewById(R.id.ecg_wave_view2);
        PdfEcgWaveView.DATA_PRE_SECOND = sampleRate;
        getAndSavePdfImg(view, waveView, 1);
    }

    int page;// 分页

    private void getAndSavePdfImg(View view, PdfEcgWaveView waveView, int i) {
        waveView.setGain(gain);
        waveView.setPagerSpeed(paperSpeed + 1);
        Log.d("列表大小", ecgWaveList.size() + "");
        //采样率是512个数据/秒,我们取8秒生成一张图,从而得到512*8=4096
        int dataSize = sampleRate * 8;
        page = (int) Math.ceil((double) ecgWaveList.size() / dataSize);
        List pdfItem = new ArrayList<>();
        for (int j = 0; j < page; j++) {
            pdfItem.clear();
            for (int k = j * dataSize; k < dataSize + dataSize * j; k++) {
                if (k < ecgWaveList.size()) {
                    pdfItem.add(ecgWaveList.get(k));
                }
            }
            waveView.preparePoints(pdfItem);
            layoutView(view, waveView, pdfItem);
            getAndSaveBitmap(view, j);
        }
        Log.d("ecgsize", ecgWaveList.size() + "");

    }

    protected void layoutView(View v, PdfEcgWaveView waveView, List ecgItem) {
        int a = (int) (sampleRate * 8 * ecgWaveView.getDataSpacing());
        Log.d("viewSize", ecgItem.size() + "");
        Log.e("bitmap", "a=" + a + "-dataSpacing=" + ecgWaveView.getDataSpacing() + "-sampleRate=" + sampleRate);
        v.layout(0, 0, a, SizeUtils.dp2px(180));
        int measuredWidth = View.MeasureSpec.makeMeasureSpec(a, View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(SizeUtils.dp2px(180), View.MeasureSpec.EXACTLY);
        v.measure(measuredWidth, measuredHeight);
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    }

    /**
     * 创建和保存bitmap
     */
    protected void getAndSaveBitmap(View v, int i) {
        Log.e("bitmap","width="+v.getWidth()+"height="+v.getHeight());
        Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
                Bitmap.Config.ARGB_8888);
        // 利用bitmap生成画布
        Canvas canvas = new Canvas(bitmap);
        // 把view中的内容绘制在画布上
        v.draw(canvas);
        saveBitmap(bitmap, i);
    }
    
	/**
     * 保存并压缩图片
     *
     * @param bitmap
     */
    public void saveBitmap(Bitmap bitmap, int i) {
        Log.d("hehe", "保存图片");
        singleThreadExecutor.execute(new Runnable() {
            @Override
            public void run() {
                String path = Constants.PDF_IMG_ROTE_PATH + "/img" + i + ".png";
                File file = new File(path);
                try {
                    FileOutputStream stream = new FileOutputStream(file);
                    //压缩图片
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
                    //释放资源
                    stream.flush();
                    stream.close();
                } catch (Exception e) {
                    disProgressDialog();
                    ToastUtil.showLongToast(StringUtils.getString(R.string.failed_report));
                    Log.d("hehe", e.getLocalizedMessage());
                }
            }
        });
    }

exg_pdf_view.xml

"1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@color/write"
    android:layout_height="@dimen/dp_200">

    android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/write"
        app:largeGridColor="#ff0022"
        app:smallGridColor="#ff0033" />

        android:id="@+id/ecg_wave_view2"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/dp_200"
            app:scrollable="true"
            app:waveColor="@color/black"
            app:waveStrokeWidth="@dimen/dp_1" />

你可能感兴趣的:(代码块,Bitmap,LayoutInFlater,inflate)