我正在做一个简单的鼓组应用,用户可以点击按钮发出相应的声音,并在画布上的随机位置画一个圆圈。对于画布,我创建了一个名为CanvasView的自定义视图,然后我将CanvasView实现到MainActivity中,因此每次使用MainActivity中的OnClickListener单击按钮时,我都可以控制该视图。
但是,我不知道如何使用OnClickListener实际控制(画一个圆圈)CanvasView。我需要这方面的帮助
这是我的MainActivity类代码:
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 :(得分:2)
您不应直接从活动中调用onDraw
的{{1}}功能。而是在画布视图中创建一个公共方法并在那里编写逻辑并在最后调用canvas
。这会调用invalidate
方法,并根据您的新逻辑再次在屏幕上绘制内容。
如果您希望每次点击按钮时在屏幕上的随机位置绘制圆圈,您可以执行以下操作
onDraw
然后onButtonClick
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);
}
}
希望它有所帮助!