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

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

css 复制代码
 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<Float> 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<Float> 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

css 复制代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <com.kl.commonbase.views.ecg.EcgBackgroundView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/write"
        app:largeGridColor="#ff0022"
        app:smallGridColor="#ff0033" />

        <com.kl.commonbase.views.ecg.PdfEcgWaveView
            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" />
</FrameLayout>
相关推荐
这孩子叫逆3 个月前
Redis实战(使用Scan,Lua脚本,一次扣多个库存,多线程并发使用,并发获取分布式锁,BItMap实现签到和在线统计)
java·redis·bitmap·scan
菠萝加点糖5 个月前
Android 中处理 RGB24 格式数据
android·bitmap
surpassLiang5 个月前
基于BitMap的工作日间隔计算
数据库·bitmap·bitset·工作日计算·postgesql
三村阿明6 个月前
LayoutInflater加载流程
android·layoutinflater·view·layout·inflater
xiangxiongfly9157 个月前
Android 通过View生成Bitmap
android·bitmap
一叶知秋临9 个月前
Redis基本概念
redis·bitmap·zset·geo·hyperlog
NPE~9 个月前
Golang基于Redis bitmap实现布隆过滤器(完结版)
开发语言·redis·后端·缓存·golang·bitmap·布隆过滤器
吕氏春秋i1 年前
Android Glide从网络加载图片 点击可放大查看效果
android·glide·图片放大·bitmap
bcbobo21cn1 年前
C# Bitmap类学习1
开发语言·c#·bitmap·setpixel