2014-05-18 283 views
2

我正在做一個簡單的鼓組應用程序,用戶可以點擊按鈕製作相應的聲音,並在畫布上隨機繪製一個圓圈。對於畫布,我創建了一個名爲CanvasView的自定義視圖,然後將該CanvasView實現到MainActivity中,這樣每次使用MainActivity中的OnClickListener單擊按鈕時,都可以控制該視圖。
但是,我不知道如何使用OnClickListener實際控制(畫一個圓圈)CanvasView。我需要這部分
幫助這是我在MainActivity類代碼:
Android:在自定義畫布上繪製圓形按鈕,點擊

public class MainActivity extends Activity { 
//setup variables for soundpool 
. 
. 
. 
//implements CanvasView 

CanvasView mCanvasView; 

//set up soundID for each sound 
. 
. 
. 
//setup buttons 
Button mKick; 
. 
. 
. 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
//set up soundpools 
. 
. 
. 

//example, the kick drum button 
//kick drum 
    mKick = (Button) findViewById(R.id.kick); 
    mKick.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     soundPool.play(kick, leftVolume, rightVolume, priority, no_loop, normal_playback_rate); 

//   mCanvasView.draw(CanvasView.mCanvas); 

    } 
}); 

這是我的代碼CanvasView

public class CanvasView extends View { 
public Paint mPaint; 
public static Canvas mCanvas; 
//constructor 
public CanvasView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mPaint = new Paint(); 
} 
//what I want to draw is here 
protected void onDraw(Canvas canvas) { 
    mCanvas = canvas; 
    super.onDraw(mCanvas); 
    canvas.drawColor(Color.GRAY); 
    mPaint.setColor(Color.BLUE); 
    mPaint.setStyle(Style.STROKE); 
    mPaint.setAntiAlias(true); 
    canvas.drawCircle(30, 30, radius, mPaint); 

這是我在MainActivity佈局XML

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/GridLayout1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:columnCount="3" 
android:orientation="horizontal" 
android:rowCount="5" 
tools:context=".GridXMLActivity" > 

<Button 
    android:id="@+id/kick" 
    android:layout_width="78dp" 
    android:layout_height="wrap_content" 
    android:layout_column="1" 
    android:layout_gravity="center_horizontal|fill_vertical" 
    android:layout_row="3" 
    android:text="Kick" /> 

<com.example.virtualdrumset.CanvasView 
    android:id="@+id/canvasView1" 
    android:layout_width="143dp" 
    android:layout_height="169dp" 
    android:layout_column="1" 
    android:layout_gravity="left|top" 
    android:layout_row="0" /> 

對於任何不便或錯誤,我深表歉意。這是我第一次提問。

+0

我沒有正確理解你的問題。 「我不知道如何使用OnClickListener控制CanvasView(在一個圓上畫),你是什麼意思。每次點擊按鈕時你想繪製一個圓圈?像第二次點擊按鈕時畫第二個圓圈,點擊按鈕時第三次畫圓圈第三次? –

+0

是的,我想要的是每次單擊按鈕時繪製一個圓形,但每次都會刷新一次。 – user3648721

+0

查看我的回答 –

回答

2

您不應該直接從該活動調用onDraw函數canvas。相反,在畫布視圖中創建一個公共方法,並在那裏編寫邏輯,最後請撥invalidate。這要求onDraw方法和內容根據您的新邏輯再次在屏幕上繪製。

如果你想在每一次點擊按鈕,你可以做這樣的事情

public class CanvasView extends View { 
    public Paint mPaint; 
    public static Canvas mCanvas; 
    private int mPivotX = 0; 
    private int mPivotY = 0; 
    private int radius = 60; 

    //constructor 
    public CanvasView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mPaint = new Paint(); 
    } 

    public void drawCircle() { 

     int minX = radius * 2; 
     int maxX = getWidth() - (radius *2); 

     int minY = radius * 2; 
     int maxY = getHeight() - (radius *2); 

     //Generate random numbers for x and y locations of the circle on screen 
     Random random = new Random(); 
     mPivotX = random.nextInt(maxX - minX + 1) + minX; 
     mPivotY = random.nextInt(maxY - minY + 1) + minY; 

     //important. Refreshes the view by calling onDraw function 
     invalidate(); 

    } 

    //what I want to draw is here 
    protected void onDraw(Canvas canvas) { 
     mCanvas = canvas; 
     super.onDraw(mCanvas); 
     canvas.drawColor(Color.GRAY); 
     mPaint.setColor(Color.BLUE); 
     mPaint.setStyle(Style.STROKE); 
     mPaint.setAntiAlias(true); 
     canvas.drawCircle(mPivotX, mPivotY, radius, mPaint); 
    } 
} 

然後onButtonClick

mKick.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     soundPool.play(kick, leftVolume, rightVolume, priority, no_loop, normal_playback_rate); 

      mCanvasView.drawCircle() 

    } 

希望它可以幫助在屏幕上任意位置要繪製一個圓!

+0

謝謝,但是當我點擊時,應用程序崩潰了,錯誤日誌是指按鈕 – user3648721

+0

的onclicklistener什麼是異常?你能發佈例外的完整LogTrace嗎? –

+0

你在onCreate中初始化了'mCanvasView'嗎? 'mCanvasView =(CanvasView)findViewById(R.id.canvasView1);' –