框架布局类的子类不重绘画布屏幕?



当我使用FrameLayout类(GameView(来使用addView(object),这样我就可以使用ontouchlistener(),但它只绘制Rocketshooter(用于绘制的类,都在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)));
}
}

射手(加农炮(类用于在底部绘制加农炮<--只画这个

//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 Goliyan(子弹(类扩展视图
/*实际上在印地语中,goliyan的意思是子弹*/

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. 我有一个游戏视图类(自定义视图(,可以绘制大炮、子弹、物品和爆炸(爆炸类(
  2. 但是在扩展FrameLayout类(早期视图类(以使用addView和ontouchlistener之后只画不动就意味着没有invalidate()
  3. 在此之前,当我扩展View类并且不使用addView(..)时,所有的东西都被绘制和移动了
  4. 此外,我不需要addView(items),但当我删除它时,它没有绘制Rocket
  5. 可以忽略,但如果你能帮我

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

最新更新