FrameLayout类的子类不会重新绘制画布屏幕吗?

问题描述 投票:0回答:1

[当我使用FrameLayout Class(GameView)使用addView(object)以便可以使用ontouchlistener()时,它仅绘制Rocketshooter(用于绘制的类,都在GameView中调用),但它没有't重绘它(请参阅底部的摘要)

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)类扩展了视图/ *实际上,在印度语中,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类(自定义视图),可以绘制大炮,子弹,物品和爆炸(爆炸类)
  2. 但是在扩展FrameLayout类(早期视图类)以使用addView和ontouchlistener之后只有它绘制但不移动意味着没有invalidate()
  3. 在全部绘制并移动之前,当我扩展View类并且不使用addView(..)时>
  4. 而且,我不需要addView(items),但是当我删除它时,它并没有绘制Rocket
  5. 随意忽略,但如果可以,请帮助我

[当我使用FrameLayout类(GameView)使用addView(object)以便可以使用ontouchlistener()时,它仅绘制Rocket和Shooter(用于绘制的类,都在GameView中调用),但是它...] >

java android android-canvas android-framelayout android-viewgroup
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.