当前位置:   article > 正文

自定义波形图View,LayoutInflater动态加载控件保存为本地图片

自定义波形图View,LayoutInflater动态加载控件保存为本地图片

效果图:
在这里插入图片描述

页面布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_145"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white">

    <TextView
        android:id="@+id/tv_index"
        android:layout_width="@dimen/dp_30"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="@color/common_btn_clicked"
        android:gravity="center"
        android:textColor="@color/white"
        android:textSize="@dimen/sp_14"
        tools:text="TH"/>

    <RelativeLayout
        android:id="@+id/rl_wave_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">

        <com.kl.common_base.view.wave.GridView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:bgColor="#f0d3a9"/>

        <com.kl.common_base.view.wave.PdfEcgWaveView
            android:id="@+id/wave_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible" />

        <TextView
            android:id="@+id/tv_file_create_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/dp_20"
            android:layout_marginTop="@dimen/dp_10"
            android:text=""
            android:textColor="@color/colorPrimary"
            android:textSize="@dimen/sp_12"
            tools:text="2019年6月26日11:54" />
    </RelativeLayout>
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

自定义波形图控件:

package com.kl.common_base.view.wave;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.kl.common_base.R;
import com.kl.common_base.utils.SizeUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class PdfEcgWaveView extends View {
    private List<Short> pointList = Collections.synchronizedList(new LinkedList<>());
    private Paint mPaint;
    private Paint mPaintLine;
    private int mWidth = 0;
    private int mHeight = 0;
    private int mCenterY = 0;
    public float points[];
    List<Short> nativeDatas = null;
    private int len = 0;
    private int index = 0;

    private int zoom;
    private float gapX = 0.2f;
    private int xSize = 0;
    private final int maxMillimeter = 25;
    private final int FILTER_SIZE = 50;
    private int sampleRate = 8000 / FILTER_SIZE;
    private int gain = 5;
    private final int maxMidScopeY = 0;
    private double screenTotalTime;
    private int[] lineArray = new int[]{7321, 8521, 9600, 10875};
    private Map<Integer, Integer> lineMap = new HashMap();
//    private List<Float> xList = new ArrayList<>();


    public PdfEcgWaveView(Context context) {
        super(context);
        Log.d("caowj", "55555555555555555555555555");
        initPaint();
    }

    public PdfEcgWaveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        Log.d("caowj", "66666666666666666666666666");
        initPaint();

    }

    public PdfEcgWaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.d("caowj", "77777777777777777777777777");
        initPaint();

    }

    private void initPaint() {
        Log.d("caowj", "initPaint--------------");
        if (mPaint == null) {
            mPaint = new Paint();
            mPaint.setColor(Color.BLACK);
            mPaint.setAntiAlias(true);
            mPaint.setStrokeWidth(2);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStyle(Paint.Style.STROKE);
        }
        if (mPaintLine == null) {
            mPaintLine = new Paint();
            mPaintLine.setColor(getContext().getResources().getColor(R.color.red));
            mPaintLine.setAntiAlias(true);
            mPaintLine.setStrokeWidth(2);
            mPaintLine.setStrokeCap(Paint.Cap.ROUND);
            mPaintLine.setStyle(Paint.Style.STROKE);
        }
    }

    @Override
    protected void onSizeChanged(int width, int height, int oldw, int oldh) {
        mHeight = getHeight();
        mCenterY = mHeight / 2;
        mWidth = getWidth();
//        Log.d("caowj", "onSizeChanged--------------"+mWidth+",,,"+mHeight+",,,"+mCenterY);

        zoom = height / maxMillimeter;
//        double speed = 0.04;
        double speed = (double) 2 / ((double) width / zoom);

        Log.e("caowj", "speed=" + speed);
        screenTotalTime = width / zoom * speed;
        gapX = (float) (this.mWidth / (sampleRate * screenTotalTime));
        xSize = Math.round(this.mWidth / gapX);
        points = new float[xSize * 4];
        Log.e("caowj", "初始化计算:zoom=" + zoom + ",screenTotalTime=" + screenTotalTime + ",gapX=" + gapX + ",widthSize=" + xSize);
        super.onSizeChanged(width, height, oldw, oldh);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.d("caowj", "onMeasure--------------" + widthMeasureSpec + ",,," + heightMeasureSpec + ",,," + mCenterY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.w("caowj", "onDraw-----------" + pointList.size());
        len = pointList.size();
        if (len >= 2) {
            index = xSize - len;
//            Log.d("caowj", "drawCube  len=" + len + ",widthSize=" + widthSize + ",index=" + index);
            for (int i = index + 1; i < xSize; i++) {
                float startX = (i - 1) * gapX;
                int mIndex = i - index - 1;
                points[i * 4] = startX;
                points[i * 4 + 1] = pointList.get(mIndex);
                points[i * 4 + 2] = i * gapX;
                points[i * 4 + 3] = pointList.get(i - index);

                if (lineMap.containsKey(mIndex)) {
                    Log.w("caowj", "找到需要绘制竖线的位置了:" + startX);
//                    xList.add(startX);
                    int padding = SizeUtils.dp2px(getResources().getDimension(R.dimen.dp_25));
                    canvas.drawLine(startX, padding, startX, mCenterY * 2 - padding, mPaintLine);
                }
            }
        }

        canvas.drawLines(points, mPaint);

        super.onDraw(canvas);
    }


    public void addWaveDataInVisiable(short[] waveData) {
        if (nativeDatas == null) {
            nativeDatas = new ArrayList<>();
        }

        for (int i = 0; i < waveData.length; i++) {
            short y = (short) Math.floor(calcRealMv(maxMidScopeY - waveData[i]) * gain * zoom + mCenterY);
            nativeDatas.add(y);
//            Log.e("caowj", "y=" + y);
        }
        Log.e("caowj", "nativeDatas 长度=" + nativeDatas.size());
        if (nativeDatas.size() >= 800) {
            addPointThreadExecutor(nativeDatas);
            nativeDatas = new ArrayList<>();
        }
    }

    private void addPointThreadExecutor(List<Short> nativeDatas) {
        if (nativeDatas == null) {
            return;
        }

        List<Short> dataList = nativeDatas;
        synchronized (pointList) {
            for (int i = 0; i < dataList.size(); i += FILTER_SIZE) {
                if (pointList.size() >= xSize && xSize > 0) {
                    pointList.remove(0);
                }

                pointList.add(dataList.get(i));

                for (int linePosition : lineArray) {
                    if (linePosition > i - FILTER_SIZE && linePosition <= i) {
                        lineMap.put(pointList.size(), linePosition);
                    }
                }
            }
        }
    }


    public void clear() {
        if (pointList != null) {
            pointList.clear();
        }
        if (nativeDatas != null) {
            nativeDatas.clear();
        }

        points = new float[xSize * 4];
        postInvalidate();
    }


    /**
     * mintti  计算真实毫伏值
     *
     * @param point
     * @return
     */
    private float calcRealMv(int point) {
        return (float) (point * 3.3 / 32767);
//        int magnification = 1000;//TODO 放大倍数
//        return (float) (point / magnification * 3.3 / 32767 * 1000);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
 public static void createWaveImage(Context context, ViewGroup parentView, AudioFile audioFile, String parentDir, String fileName, int index, boolean isLast) {
        Log.w("caowj", "createWaveImage");

        View rootView = LayoutInflater.from(context).inflate(R.layout.item_wave_view, null);
        PdfEcgWaveView waveView = rootView.findViewById(R.id.wave_view);
        TextView tvTitle = rootView.findViewById(R.id.tv_file_create_date);
        TextView tvIndex = rootView.findViewById(R.id.tv_index);
        tvIndex.setText("TL");
        String[] arr = fileName.split("\\.");
        String imageName = arr[0] + "(" + index + ")";
        String imgTitle = "";
        if (arr[0].startsWith("0_")) {
            imgTitle = StringUtils.getPositionAiResult(fileName);
        }
        if (TextUtils.isEmpty(imgTitle)) {
            imgTitle = imageName;
        }
        tvTitle.setText(imgTitle);

        int width = SizeUtils.getScreenWidth() * 2 / 3;
        int height = SizeUtils.dp2px(context.getResources().getDimension(R.dimen.dp_145));

        layoutView(rootView, width, height);

        audioFile.refreshPcmDataByPosition(index, 8000 * 2);
        waveView.addWaveDataInVisiable(audioFile.getData().clone());


        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        // 利用bitmap生成画布
        Canvas canvas = new Canvas(bitmap);
        // 把view中的内容绘制在画布上
        rootView.draw(canvas);// 触发draw()

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d("caowj", "生成Bitmap");
                boolean result = FileUtils.saveWaveBitmap(bitmap, parentDir, imageName + ".jpg");
            }
        }).start();

    }

    protected static void layoutView(View v, int w, int h) {
        v.layout(0, 0, w, h);
        int measuredWidth = View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY);
        v.measure(measuredWidth, measuredHeight);
        Log.w("caowj", measuredWidth + "--" + measuredHeight + ";;;" + v.getMeasuredWidth() + "---" + v.getMeasuredHeight());
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/805912
推荐阅读
相关标签
  

闽ICP备14008679号