FrameLayout Class 的 SubClass 不重绘 canvas 屏幕?
SubClass of FrameLayout Class doesn't redraw canvas screens?
当我使用 FrameLayout Class(GameView) 来使用 addView(object)
以便我可以使用 ontouchlistener()
但它只绘制 Rocket
和 shooter
(classes 用于绘制,全部在 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) Class 用于在底部绘制一门大炮<-- 只有这个被绘制(有效)
//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(子弹)class 扩展视图
/* 实际上在印地语口语中 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);
}
}
你可能需要火箭(物品)class
//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);
}
}
总结
- 我有 GameView Class(自定义视图)绘制大炮、子弹、物品和爆炸(Blast class)
- 但扩展FrameLayout后class(早期视图class)使用addView和ontouchlistener
只画不动表示没有
invalidate()
它
- 在此之前,当我扩展 View class 并且没有使用
addView(..)
时,所有内容都被绘制和移动
- 此外,我不需要
addView(items)
但是当我删除它时,它没有绘制 Rocket
- 请随意忽略,但如果可以请帮助我
您需要在 FrameLayout
的构造函数中调用 setWillNotDraw(false)
,否则不会调用其 onDraw
。
当我使用 FrameLayout Class(GameView) 来使用 addView(object)
以便我可以使用 ontouchlistener()
但它只绘制 Rocket
和 shooter
(classes 用于绘制,全部在 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) Class 用于在底部绘制一门大炮<-- 只有这个被绘制(有效)
//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(子弹)class 扩展视图
/* 实际上在印地语口语中 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);
}
}
你可能需要火箭(物品)class
//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);
}
}
总结
- 我有 GameView Class(自定义视图)绘制大炮、子弹、物品和爆炸(Blast class)
- 但扩展FrameLayout后class(早期视图class)使用addView和ontouchlistener
只画不动表示没有
invalidate()
它 - 在此之前,当我扩展 View class 并且没有使用
addView(..)
时,所有内容都被绘制和移动
- 此外,我不需要
addView(items)
但是当我删除它时,它没有绘制Rocket
- 请随意忽略,但如果可以请帮助我
您需要在 FrameLayout
的构造函数中调用 setWillNotDraw(false)
,否则不会调用其 onDraw
。