简体   繁体   English

如何在Android中的画布自定义视图上绘制可绘制对象

[英]How to draw drawable on canvas custom view in android

I'm working on audio recording app.During recording i'm using visualizer like normal audio recording apps.But on specific time a want to draw a drawable on canvas visualizer(that is custom class extend View class). 我正在使用音频录制应用程序。在录制过程中,我像普通的音频录制应用程序一样使用可视化工具。但是在特定时间想要在画布可视化工具上绘制可绘制对象(这是自定义类,扩展View类)。

Like this white drawable dot. 像这个白色的可绘制点。

在此处输入图片说明

Here is my Custom view class. 这是我的自定义视图类。

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);

        }
    }
}


}

This code add white mark once but remove it when onDraw calls again. 该代码一次添加白色标记,但是在再次调用onDraw时将其删除。 actually i want to draw a mark on canvas like above white dot. 其实我想在画布上像白点上面画一个标记。

Don't know why it remove it.Please help me 不知道为什么将其删除。请帮助我

Thanks in advance 提前致谢

如果在再次调用onDraw之后删除了点,这是否与for循环的迭代方式有关?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM