gpt4 book ai didi

android - 如何在 View 上绘制自定义 View (例如圆形)?

转载 作者:行者123 更新时间:2023-11-29 15:41:24 26 4
gpt4 key购买 nike

对于我的 Pong 克隆,我正在尝试将类 Ball 中的自定义 View 绘制到我在类 GamePanel 中的 View 上。但是,在运行时屏幕上看不到它。

public class Ball extends View {

private float posX;
private float posY;
private float radius;

private Paint paint;

public Ball(Context context, float posX, float posY, float radius){
super(context);
this.posX = posX;
this.posY = posY;
this.radius = radius;
setWillNotDraw(false);
init();

}

private void init(){

paint = new Paint();
paint.setColor(Color.WHITE);
}

@Override
protected void onDraw(Canvas canvas) {

canvas.drawCircle(posX, posY, radius, paint);

}

现在这是我的类 GamePanel。

public class GamePanel extends View implements GestureDetector.OnGestureListener{

private Context context;

//Screen height and width in pixels
private static int width;
private static int height;

private MainThread thread;

private Rect paddle1;
private Rect paddle2;

private Ball ball;

private Paint paint;

//Starting position paddle1
private int xPos1;
private int yPos1;

// Starting position paddle2
private int xPos2;
private int yPos2;

//Width and height of paddles
private int pWidth;
private int pHeight;

private GestureDetectorCompat gestureDetector;

public GamePanel(Context context) {

super(context);

this.context = context;

//Information we will need for positioning our objects inside the panel
DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();

width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;

//Make the GamePanel focusable
setFocusable(true);

// Thread needs information about the game panel
thread = new MainThread(this);

pWidth = 100;
pHeight = height/2;

xPos1 = width/15;
yPos1 = 0;

xPos2 = width - xPos1 - pWidth;
yPos1 = 0;

paddle1 = new Rect(xPos1, yPos1, xPos1 + pWidth, yPos1 + pHeight);
paddle2 = new Rect(xPos2, yPos2, xPos2 + pWidth, yPos2 + pHeight);

paint = new Paint();
paint.setColor(Color.WHITE);

ball = new Ball(context, 300, 300, 300);

setBackgroundColor(Color.BLACK);

this.gestureDetector = new GestureDetectorCompat(context, this);

thread.setRunning(true);
thread.start();
}


@Override
protected void onDraw(Canvas canvas) {

canvas.drawRect(paddle1, paint);
canvas.drawRect(paddle2, paint);

}

我尝试在我的 MainThread 线程、GamePanel 的构造函数和 Ball 的构造函数中调用 invalidate() 和 postInvalidate()。球永远不会被绘制到屏幕上。

正如您所看到的,我已经在 Pong 中遇到了这个问题,因此我在 GamePanel 中创建了两个非常难看的矩形。

如何封装矩形和圆形(球),以便它仍然可以在 Canvas 上绘制?

最佳答案

您正在 GamePanel View 中创建 Ball(circle) View 。仅仅因为您正在创建一个 View 并不意味着它会被绘制在屏幕上。您需要将 View 附加到布局。简单的解决方案是将您的 GamePanel 扩展到 Ball

public class GamePanel extends Ball {

}

然后在GamePanelonDraw()函数中添加super.onDraw();

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(); //Will draw the circle before drawing rectangle
canvas.drawRect(paddle1, paint);
canvas.drawRect(paddle2, paint);
}

如果将 super.onDraw() 放在 drawRect 下方,则在绘制矩形后绘制圆。

You also need to understand the basic concept of adding the view to layout.

关于android - 如何在 View 上绘制自定义 View (例如圆形)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39622898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com