如何在android中的canvas自定义视图上绘制drawable

Nis*_*mad 5 android canvas view android-custom-view

我正在开发录音应用程序。在录音期间,我使用像普通录音应用程序一样的可视化工具。但在特定时间,我想在画布可视化工具上绘制可绘制对象(即自定义类扩展 View 类)。

就像这个白色的可绘制点。

在此处输入图片说明

这是我的自定义视图类。

public class VisualizerView extends View {
private static final int LINE_WIDTH = 1; // width of visualizer lines
private static final int LINE_SCALE = 75; // scales visualizer lines
private List<VisuallizerItem> visuallizerItems; // amplitudes for line lengths
private int width; // width of this View
private int height; // height of this View
private Paint linePaint; // specifies line drawing characteristics
//Boolean isImage=false;

// constructor
public VisualizerView(Context context, AttributeSet attrs) {
    super(context, attrs); // call superclass constructor
    linePaint = new Paint(); // create Paint for lines
    linePaint.setColor(getResources().getColor(R.color.visualizerColor)); // set color to green
    linePaint.setStrokeWidth(LINE_WIDTH); // set stroke width
}

// called when the dimensions of the View change
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    width = w; // new width of this View
    height = h; // new height of this View
    visuallizerItems = new ArrayList<VisuallizerItem>();
}

// clear all amplitudes to prepare for a new visualization
public void clear() {
    visuallizerItems.clear();
}

// add the given amplitude to the amplitudes ArrayList
public void addAmplitude(VisuallizerItem visuallizerItem) {
    // isImage=false;
    visuallizerItems.add(visuallizerItem); // add newest to the amplitudes ArrayList

    // if the power lines completely fill the VisualizerView
    if (visuallizerItems.size() * LINE_WIDTH >= width) {
        visuallizerItems.remove(0); // remove oldest power value
    }
}

public void addAmplitudeImage(VisuallizerItem visuallizerItem) {
    //isImage=true;
    visuallizerItems.add(visuallizerItem); // add newest to the amplitudes ArrayList

    // if the power lines completely fill the VisualizerView
    if (visuallizerItems.size() * LINE_WIDTH >= width) {
        visuallizerItems.remove(0); // remove oldest power value
    }
}



// draw the visualizer with scaled lines representing the amplitudes
@Override
public void onDraw(Canvas canvas) {
    int middle = height / 2; // get the middle of the View
    float curX = 0; // start curX at zero

    // for each item in the amplitudes ArrayList
    for (VisuallizerItem power : visuallizerItems) {
        if (power.isImage == -100) {
            float scaledHeight = power.amplitude / LINE_SCALE; // scale the power
            curX += LINE_WIDTH; // increase X by LINE_WIDTH

            // draw a line representing this item in the amplitudes ArrayList
            canvas.drawLine(curX, middle + scaledHeight / 2, curX, middle
                    - scaledHeight / 2, linePaint);
        } else {//**Here i'm trying to draw mark on canvas** 
            power.isImage = -100;
            //TODO draw attachment image here
            Paint p = new Paint();
            Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.yellow);
            p.setColor(Color.RED);
            canvas.drawBitmap(b, visuallizerItems.size(), middle, p);

        }
    }
}


}
Run Code Online (Sandbox Code Playgroud)

此代码添加一次白色标记,但在 onDraw 再次调用时将其删除。实际上我想在画布上画一个标记,比如白点上方。

不知道为什么会删除它。请帮帮我

提前致谢