无法使用canvas绘制两个类

[英]Cant get two classes to draw using canvas


Iam doing my first game for android.

我正在为Android做第一场比赛。

I want to do a very simple thing which is to have a background and a ball that will "spawn" in it.

我想做一个非常简单的事情,那就是有一个背景和一个会在其中“产生”的球。

So I have made my GameView:

所以我制作了我的GameView:

package com.example.newarkanoid;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class Tela extends View {
    Paint paint;
    int x,y;
    int lastx,lasty;
    Bola bola;

    public Tela(Context context, Bola BOLA) {
        super(context);
        paint = new Paint();
        x=0;lastx=0;
        y=0;lasty=0;
        bola = BOLA;
        bola.paint.setColor(Color.BLACK);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.WHITE);
        canvas.drawPaint(paint);
        bola.invalidate();
    }

    public boolean onTouchEvent(MotionEvent event) {
        x = (int)event.getX();
        y = (int)event.getY();
        if(lastx !=x || lasty !=y){
            lastx=x;
            lasty=y;
            bola.x = x;
            bola.y = y;
            bola.invalidate();
        }
    return false;
    }

}

Well, above is my MainDisplay, now i need a ball:

好吧,上面是我的MainDisplay,现在我需要一个球:

package com.example.newarkanoid;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class Bola extends View {
    Paint paint;
    float x,y,raio;

    public Bola(Context context, float x, float y, float raio) {
        super(context);
        this.x = x;
        this.y = y;
        this.raio = raio;
        paint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(x, y, raio, paint);
    }

}

So, I did like that cause my teacher told me, you dont have to call invalidate for the intire main display, you can call invalidate just for your ball, so i made my ball drawing code, also its properties.

所以,我确实喜欢这样,因为我的老师告诉我,你不必为衣服主显示器调用invalidate,你可以为你的球调用invalidate,所以我制作了我的球绘图代码,也是它的属性。

So as you can see in the code, when i click somewhere in the touchscreen my ball x and y will change to the click position, and then call invalidate.

因此,您可以在代码中看到,当我点击触摸屏中的某个位置时,我的球x和y将更改为单击位置,然后调用invalidate。

The thing is, the ball does not even appear when i create my mainDisplay, so I was wondering, is there something like a context problem? why my ball isnt drawn?

问题是,当我创建mainDisplay时,球甚至没有出现,所以我想知道,是否有类似上下文问题的东西?为什么我的球不被吸引?

Also, here is my MainActivity:

另外,这是我的MainActivity:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bola bola = new Bola(this,20,20,5);
        Tela t = new Tela(this,bola);

        setContentView(t);
    }

2 个解决方案

#1


0  

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    paint.setColor(Color.WHITE);
    canvas.drawPaint(paint);
    bola.invalidate();
}

when bola invalidate, bola will draw with its canvas, different the Tela's canvas. You can create a class, not extends view, with draw method

当bola无效时,bola会用它的画布绘制,不同的是Tela的画布。您可以使用draw方法创建类,而不是扩展视图

public void draw(Canvas canvas) {
    paint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(x, y, raio, paint);
}

and call in tela:

并打电话给特拉:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    paint.setColor(Color.WHITE);
    canvas.drawPaint(paint);
    bola.draw(canvas);
}

#2


0  

use bola.draw(canvas); in your onDraw()

使用bola.draw(画布);在你的onDraw()


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2012/11/01/3a663bc5b8d51ecd8b64ecb14b7a58c.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号