簡體   English   中英

如何在Android中的畫布自定義視圖上繪制可繪制對象

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

我正在使用音頻錄制應用程序。在錄制過程中,我像普通的音頻錄制應用程序一樣使用可視化工具。但是在特定時間想要在畫布可視化工具上繪制可繪制對象(這是自定義類,擴展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);

        }
    }
}


}

該代碼一次添加白色標記,但是在再次調用onDraw時將其刪除。 其實我想在畫布上像白點上面畫一個標記。

不知道為什么將其刪除。請幫助我

提前致謝

如果在再次調用onDraw之后刪除了點,這是否與for循環的迭代方式有關?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM