从另一个 Class 借鉴 Canvas
Draw on a Canvas from another Class
我正在尝试从视图中的 Asynctask 绘制多个矩形。在我的 MainActivity 中,我调用视图如下:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View bouncingBallView = new BouncingBallView2(this);
setContentView(bouncingBallView);
bouncingBallView.setBackgroundColor(Color.BLACK);
}
}
然后在我的 BouncingBallView
中创建 canvas:
public class BouncingBallView2 extends View {
private Box box;
final BouncingBallView2 context = this;
private AsyncTask<Void, Void, Void> task;
// Constructor
public BouncingBallView2(Context context) {
super(context);
box = new Box(Color.BLACK); // ARGB
task=new PackagesPosition();
task.execute();
}
// Called back to draw the view. Also called after invalidate().
@Override
protected void onDraw(Canvas canvas) {
// Draw the components
box.draw(canvas);
invalidate(); // Force a re-draw
}
// Called back when the view is first created or its size changes.
@Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
box.set(0, 0, w, h);
}
如您所见,我调用 AsyncTask
class,它也在 BouncingBall 中。 AsyncTask
获取一个框列表,每个框有四个值:x,y,width,height
。
盒子class的工作原理如下:
public class Box {
int xMin, xMax, yMin, yMax;
private Paint paint; // paint style and color
private Rect bounds;
public Box(int color) {
paint = new Paint();
paint.setColor(color);
bounds = new Rect();
}
public void set(int x, int y, int width, int height) {
xMin = x;
xMax = x + width - 1;
yMin = y;
yMax = y + height - 1;
// The box's bounds do not change unless the view's size changes
bounds.set(xMin, yMin, xMax, yMax);
}
public void draw(Canvas canvas) {
canvas.drawRect(bounds, paint);
}
}
我不知道如何从 AsyncTask
添加框,因为我无法直接访问 Canvas,你知道我应该如何进行吗?
任何帮助将不胜感激
我认为最简单的方法是通过 Box
class 的构造函数注入 canvas。不过,就我个人而言,我不会在 View
之外作画。我建议将您的画移到 BouncingBallView2
内并添加一个从您的 AsyncTask
调用的方法。此方法将控制动画序列并在视图上执行 invalidate()
。
我正在尝试从视图中的 Asynctask 绘制多个矩形。在我的 MainActivity 中,我调用视图如下:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View bouncingBallView = new BouncingBallView2(this);
setContentView(bouncingBallView);
bouncingBallView.setBackgroundColor(Color.BLACK);
}
}
然后在我的 BouncingBallView
中创建 canvas:
public class BouncingBallView2 extends View {
private Box box;
final BouncingBallView2 context = this;
private AsyncTask<Void, Void, Void> task;
// Constructor
public BouncingBallView2(Context context) {
super(context);
box = new Box(Color.BLACK); // ARGB
task=new PackagesPosition();
task.execute();
}
// Called back to draw the view. Also called after invalidate().
@Override
protected void onDraw(Canvas canvas) {
// Draw the components
box.draw(canvas);
invalidate(); // Force a re-draw
}
// Called back when the view is first created or its size changes.
@Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
box.set(0, 0, w, h);
}
如您所见,我调用 AsyncTask
class,它也在 BouncingBall 中。 AsyncTask
获取一个框列表,每个框有四个值:x,y,width,height
。
盒子class的工作原理如下:
public class Box {
int xMin, xMax, yMin, yMax;
private Paint paint; // paint style and color
private Rect bounds;
public Box(int color) {
paint = new Paint();
paint.setColor(color);
bounds = new Rect();
}
public void set(int x, int y, int width, int height) {
xMin = x;
xMax = x + width - 1;
yMin = y;
yMax = y + height - 1;
// The box's bounds do not change unless the view's size changes
bounds.set(xMin, yMin, xMax, yMax);
}
public void draw(Canvas canvas) {
canvas.drawRect(bounds, paint);
}
}
我不知道如何从 AsyncTask
添加框,因为我无法直接访问 Canvas,你知道我应该如何进行吗?
任何帮助将不胜感激
我认为最简单的方法是通过 Box
class 的构造函数注入 canvas。不过,就我个人而言,我不会在 View
之外作画。我建议将您的画移到 BouncingBallView2
内并添加一个从您的 AsyncTask
调用的方法。此方法将控制动画序列并在视图上执行 invalidate()
。