2016-03-04 94 views
1

我正在開發音頻錄製應用程序。在錄製過程中,我使用的是可視化器,就像普通的音頻錄製應用程序一樣。但是在特定的時間內,想要在畫布可視化器上繪製可繪製的是自定義類擴展視圖類)。如何在畫布上繪製自定義視圖android

像這個白色的可繪製圓點。

enter image description here

這裏是我的自定義視圖類。

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再次調用時將其刪除。 其實我想在畫布上畫一個像白點一樣的標記。

不知道爲什麼它刪除it.Please幫我

在此先感謝

回答

0

如果點onDraw有去除後再次調用,它會帶來與方式做你的for循環迭代?

+0

如果還有其他問題,請點擊[問問題](// stackoverflow.com/questions/ask)按鈕。 – manetsus