所以基本上我正在开发一个创建带有SurfaceView的圆圈的应用程序。我想在每次按“创建”按钮时在SurfaceView内生成一个新圆圈。每个新圆圈将具有随机速度,颜色,起始位置以及唯一的序列号。创建一个具有随机颜色和起始位置的圆很容易,但是当我点击创建按钮时,我在绘制圆形时遇到了一些麻烦。每当我尝试在创建按钮侦听器中创建一个圆圈时,它就会崩溃。任何帮助都将不胜感激。谢谢。
package com.dwolford.project8;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import java.util.ArrayList;
import java.util.Random;
public class Main extends Activity implements Runnable {
private Button create;
private Button destroy;
private Button quit;
private SurfaceView surface;
private SurfaceHolder holder;
private boolean locker=true;
private Thread thread;
private int canvasWidth = 0;
private int canvasHeight = 0;
private int xCoordinate = 0;
private int yCoordinate = 0;
public static Canvas canvas;
public boolean createBall = false;
ArrayList<Balls> ballList = new ArrayList<Balls>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surface = (SurfaceView) findViewById(R.id.surfaceView);
holder = surface.getHolder();
create = (Button)findViewById(R.id.create);
create.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Balls ball = new Balls();
ballList.add(ball);
//create ball with random color, speed, beginning, direction, and incremented sequence number
}
});
destroy = (Button)findViewById(R.id.destroy);
destroy.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ballList.remove(0);
}
});
quit = (Button)findViewById(R.id.quit);
quit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
thread = new Thread(this);
thread.start();
}
@Override
public void run() {
while(locker){
//checks if the lockCanvas() method will be success,and if not, will check this statement again
if(!holder.getSurface().isValid()){
continue;
}
/** Start editing pixels in this surface.*/
canvas = holder.lockCanvas();
//ALL PAINT-JOB MAKE IN draw(canvas); method.
draw(canvas);
// End of painting to canvas. system will paint with this canvas,to the surface.
holder.unlockCanvasAndPost(canvas);
}
}
private void draw(Canvas nCanvas) {
Paint paint = new Paint();
canvas = nCanvas;
canvasWidth = canvas.getWidth();//Get width of canvas for ball positioning
canvasHeight = canvas.getHeight();//Get height of canvas for ball positioning
canvas.drawColor(Color.WHITE);
for(int i = 0; i < ballList.size(); i++)
{
paint.setColor((ballList.get(i).getColor()));
canvas.drawCircle((ballList.get(i).getXPosition()),(ballList.get(i).getYPosition()), 30, paint);
}
}
@Override
protected void onPause() {
super.onPause();
pause();
}
private void pause() {
locker = false;
while(true){
try {
thread.join();
} catch (InterruptedException e) {e.printStackTrace();
}
break;
}
thread = null;
}
@Override
protected void onResume() {
super.onResume();
resume();
}
private void resume() {
//RESTART THREAD AND OPEN LOCKER FOR run();
locker = true;
}
public class Balls
{
int currentSequenceNum = 0;
int color;
public Balls(){
calculatePosition();
setColor();
currentSequenceNum = currentSequenceNum +1;//Increment sequence number
}
/**
* Calculates the random starting x and y coordinates of the ball within the canvas
*/
private void calculatePosition() {
Random xRand = new Random();
Random yRand = new Random();
xCoordinate = xRand.nextInt((canvasWidth)+1);
yCoordinate = yRand.nextInt((canvasHeight)+1);
}
/**
* Sets the color of this circle
*/
public void setColor()
{
Random rand = new Random();
Paint paint = new Paint();
color = Color.argb(255, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
paint.setColor(color);
//canvas.drawCircle(xCoordinate, yCoordinate, 30, paint);
}
public int getColor()
{
return color;
}
/**
* Gets the starting x position of this ball
* @return
*/
public int getXPosition()
{
return xCoordinate;
}
public int getYPosition()
{
return yCoordinate;
}
}
}
这是xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create"
android:id="@+id/create"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/quit"
android:layout_toStartOf="@+id/quit" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DESTROY"
android:id="@+id/destroy"
android:layout_alignTop="@+id/create"
android:layout_toRightOf="@+id/quit"
android:layout_toEndOf="@+id/quit" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QUIT"
android:id="@+id/quit"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<SurfaceView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/surfaceView"
android:layout_below="@+id/create"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/quit"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
答案 0 :(得分:2)
类DrawingView扩展了SurfaceView {
private final SurfaceHolder surfaceHolder;
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public DrawingView(Context context) {
super(context);
surfaceHolder = getHolder();
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if (surfaceHolder.getSurface().isValid()) {
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLACK);
canvas.drawCircle(event.getX(), event.getY(), 50, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
return false;
}
}
在按钮点击事件上调用它 setContentView(new DrawingView(this));