Android Studio,用于管理动画的线程。画布不显示图形?

时间:2019-03-21 23:21:06

标签: java android canvas android-canvas draw

Android开发人员的新手。并且很难在代码中实现简单的画布绘制。我在下面列出了该应用程序的所有相关代码。查看我的“ CharacterSprite类:”以查找特定的问题。 >> 这是我的canvas.drawRect无法正常工作。.

  
      
  • 我不确定为什么吗?
  •   
  • 是否可能需要实施另一个   每个动画/绘画调用都有单独的类吗?
  •   
  • 我是否应该实现另一个线程来处理draw.Rect调用?
  •   

还有

  

第一次发布,所以这里什么都没有,任何建议都比   欢迎。

     

谢谢您的时间。   -亚当·C。

MainActivity:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(new GameView(this));
}

GameView类:

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

private CreatedMainThread thread;
private CharacterSprite characterSprite;


public GameView (Context context){
    super(context);

    getHolder().addCallback(this);
    thread = new CreatedMainThread(getHolder(), this);
    setFocusable(true);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
characterSprite.update();
}

@Override
public void surfaceCreated(SurfaceHolder holder){
    characterSprite = new CharacterSprite(BitmapFactory.decodeResource(getResources(), R.drawable.megumin_hat_icon));
    thread.setRunning(true);
    thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder){
    boolean retry = true;
    while (retry){
        try {
            thread.setRunning(false);
            thread.join();
        } catch (InterruptedException e){
            e.printStackTrace();
        }
        retry = false;
    }

}

public void update(){
    characterSprite.update();
}

@Override
public void draw(Canvas canvas){
    super.draw(canvas);
        if(canvas!=null) {

            // setting background color:
            Paint paint = new Paint();
            paint.setColor(Color.DKGRAY);
            canvas.drawColor(paint.getColor());

            // calling the draw method:
            characterSprite.draw(canvas);
        }
}
}

线程类:

public class CreatedMainThread extends Thread {

private SurfaceHolder surfaceHolder;
private GameView gameView;
private boolean running;
public static Canvas canvas;

public CreatedMainThread(SurfaceHolder surfaceHolder, GameView gameView){

    super();
    this.surfaceHolder = surfaceHolder;
    this.gameView = gameView;
}

@Override
public void run(){
    while (running){
        canvas = null;
        try
        {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                this.gameView.update();
                this.gameView.draw(canvas);
            }
        } catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (canvas != null)
            {
                try
                {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

}


public void setRunning(boolean isRunning){
    running = isRunning;
}
}

这是我的问题:

公共无效绘画(画布画布)方法 ,位图显示正常,但draw.rect不显示。

CharacterSprite类:

public class CharacterSprite {

private RectF rectF = new RectF();

private Bitmap sprite;
private int x,y;

private int xVel = 10;
private int yVel = 5;

private int screenW = Resources.getSystem().getDisplayMetrics().widthPixels;
private int screenH = Resources.getSystem().getDisplayMetrics().heightPixels;


public CharacterSprite(Bitmap img){
    sprite = img;
    x = 50;
    y = 50;

    //Setting rectangle:
    rectF.bottom = 20;
    rectF.top = 20;
    rectF.left = 20;
    rectF.right = 20;
}


public void draw(Canvas canvas){

    // Draw Rect:
    Paint paint = new Paint();
    paint.setColor(Color.BLUE);
    paint.setColor(paint.getColor());
    canvas.drawRect(rectF, paint);  // <<<<<-- Does not display!!! 

    // Draw Hat
    canvas.drawBitmap(sprite,x,y,null);
}

public void update() {

    if(x < 0 && y <0) {
        x = screenW / 2;
        y = screenH / 2;
    }else {
        x += xVel;
        y += yVel;

        if((x > screenW - sprite.getWidth()) || (x<0)){
            xVel = xVel*-1;
        }
        if((y > screenH - sprite.getHeight()) || (y<0)){
            yVel = yVel*-1;
        }
    }
}
}

0 个答案:

没有答案