gpt4 book ai didi

java - FrameLayout 类的子类不会重绘 Canvas 屏幕?

转载 作者:行者123 更新时间:2023-12-02 01:04:05 26 4
gpt4 key购买 nike

当我使用 FrameLayout Class(GameView) 来使用 addView(object) 以便我可以使用 ontouchlistener() 但它只绘制 Rocket code> 和 shooter (用于绘制的类,全部在 GameView 中调用),但它没有重新绘制它(请参阅底部的摘要)

GameView.java

//Package and Imports
public class GameView extends FrameLayout implements View.OnTouchListener {

public GameView(Context context, AttributeSet attrs) {
super(context,attrs);
mContext = context;
startTime = (int)(SystemClock.elapsedRealtime());
cannon = new shooter(Color.BLUE,mContext); bullets = new ArrayList<> (); explosions =new ArrayList<>(); item = new Rocket( Color.RED,mContext);
addView(item);
addView(cannon);
cannon.setOnTouchListener(this);
for (int i = 0; i < bullets.size(); i++) {
addView(bullets.get(i));
bullets.get(i).setOnTouchListener(this);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
for (int i = bullets.size() - 1; i >= 0; i--) {
if (//if bullet is touched) {
if (bullets.size() > 0) {
bullets.subList(i, bullets.size()).clear();
} } }
if (//if cannon is touched)) {
cannon.move();
}
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int time = (int)(SystemClock.elapsedRealtime() - startTime);
drawGameBoard(canvas);
if((time/6000)%2==0) // Code to alter cannon color at every 6000 sec which also didn't work
{
if(cannon.paint.getColor()!=Color.BLUE) {
cannon.paint.setColor(Color.BLUE);
cannon.invalidate();
}
}
else {
if(cannon.paint.getColor()!=Color.RED) {
cannon.paint.setColor(Color.RED);
cannon.invalidate();
}
}
invalidate();
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

width = w;
height = h;

cannon.setBounds(0,0,width,height);
item.setBounds(0,0,width,height);
for (int i = 0; i < bullets.size(); i++ ) {
bullets.get(i).setBounds(0,0,width,height);
}

}

public void drawGameBoard(Canvas canvas) {


cannon.draw(canvas);
for (int i = bullets.size() - 1; i >= 0; i--) {
if (bullets.get(i) != null) {
bullets.get(i).draw(canvas);

if (!bullets.get(i).move()) {
bullets.remove(i);
}
}
}


for (int i = explosions.size() - 1; i >= 0; i--) {
if (explosions.get(i) != null) {
if (!explosions.get(i).draw(canvas)) {
explosions.remove(i);
}
}
}

if (item!= null) {
item.draw(canvas);
RectF guyRect = item.getRect();
for (int i = bullets.size() - 1; i >= 0; i--) {
if (RectF.intersects(guyRect, bullets.get(i).getRect())) {
explosions.add(new Blast(Color.RED,mContext, item.getX(), item.getY()));
item.reset();
bullets.remove(i);
break;
}
}

if (!item.move()) {
item = null;
}
}
}
// Whenever the user shoots a bullet, create a new bullet moving upwards
public void shootCannon() {

bullets.add(new Goliyaan(cannon.paint.getColor() ,mContext, cannon.getPosition(), (float) (height-100)));

}
}

Shooter(cannon) 类用于在底部绘制一门大炮<--仅绘制此(有效)

//package and imports
public class shooter extends View {
//declared variables as used
public shooter(int color, Context c) {
super(c);
//initialized paint objects
}
public void setBounds(int lx, int ly, int ux, int uy) {
lowerX = lx;
lowerY = ly;
upperX = ux;
upperY = uy;

}
public void move() {
//moves cannon
invalidate();
}
public float getPosition()
{
return shootPoint;
}
public int shooterY(){ return (int)top;}
public void draw(Canvas canvas)
{
super.draw(canvas);
//draws a cannon(drawRect and circle)
}
}

SameWay Goliyaan(Bullets) 类扩展 View
/* 实际上在印地语口语中 goliyaan 的意思是子弹*/

package and import
public class Goliyaan extends View {
//DECLARED VARIABLES AS USED
// Constructor
public Goliyaan(int color, Context c, float startx, float starty) {
super(c);
//INITIALIZed
}

//Here is some less important functions like setbounds,getters and rect(use for collision detection)
public boolean move() {
// Get new (x,y) position
y -= stepY;
if (y - radius < 0) {
return false;
}
else
return true;
}

// draw the bullet on the canvas
public void draw(Canvas canvas) {
super.draw(canvas);

canvas.drawCircle(x, y, radius, paint);
}
}

您是否需要火箭(元素)类

//Package and Imports
public class Rocket extends View

// INITIALIZED VARIABLES HERE AS USED
// Constructor
public Rocket(int color, Context c) {
super(c);
mContext = c;
// create a bitmap from the supplied image
aliens = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(c.getResources(),
R.drawable.rocket),dst,dst, false);

}

public void setBounds(int lx, int ly, int ux, int uy) {
lowerX = lx;
lowerY = ly;
upperX = ux;
upperY = uy;

x = (float) ((upperX-50)*Math.random());
y = 0;
}

public boolean move() {
// Get new (x,y) position. Movement is always in vertical direction downwards
y += stepY;

if (y + 50 > upperY) {
reset();
return true;
}
else
return true;
}


public void reset() {
x = (float) ((upperX-50)*Math.random());
y = 0;
}

// Returns the rectangle enclosing the Rocket. Used for collision detection
public RectF getRect() {
return new RectF(x,y,x+50,y+50);
}

// HERE IS getX() and getY() Function

public void draw(Canvas canvas) {
super.draw(canvas);
canvas.drawBitmap(aliens, x, y, null);
}
}

总结

  1. 我有 GameView 类(自定义 View ),可以绘制大炮、子弹、元素和爆炸(Blast 类)
  2. 但是在扩展 FrameLayout 类(早期 View 类)以使用 addView 和 ontouchlistener 之后只有它绘制但不移动意味着没有 invalidate()
  3. 在此之前,当我扩展 View 类并且未使用 addView(..)
  4. 时,所有内容均已绘制并移动
  5. 此外,我不需要 addView(items) 但当我删除它时,它没有绘制 Rocket
  6. 随意忽略,但如果可以的话请帮助我

最佳答案

您需要在 FrameLayout 的构造函数中调用 setWillNotDraw(false),否则其 onDraw 将不会被调用。

关于java - FrameLayout 类的子类不会重绘 Canvas 屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60279002/

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