2014-11-15 23 views
0

我目前正在构建一个Android应用程序,它基于难度设置(容易产生具有边界的5x5迷宫,中等8x8,难11x11)产生迷宫。迷宫由黑色方块组成,其位置构成从左上角到右下角等路径以及其他方向的路径以及迷宫周围的一个方形边界。如果有一个黑色的方块,你不能朝那个方向移动,等等......目前,只有界面正在构建,而不是任何用户交互(我将在后面添加)。代码也有点混乱 - 我意识到这一点,并将解决问题后将其整理出来。自定义可绘制视图不显示在Android应用程序

这个问题是迷宫不显示 - 只是一个白色的空白!我为主界面使用XML文件和活动,并以迷宫的可绘制形式使用自定义视图。

这是在游戏中创建界面的活动/类。此处使用的Level类包含有关当前得分/迷宫/用户位置等的数据。已经过测试,我非常有信心它能正常工作。

public class InGameInterface extends Activity { 

    Level level;; 
    MazeView maze; 

    private boolean[][] currentMaze = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.in_game_interface); 

     level = new Level(); 

     currentMaze = level.getNewMaze(); 
     loadMazeDisplay(); 
    } 

    private void loadMazeDisplay() 
    { 
     maze = new MazeView(this, level, currentMaze); 

     RelativeLayout mazeHolder = (RelativeLayout) findViewById(R.id.maze); 
     LayoutParams lP = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 

     mazeHolder.addView(maze, lP); 
    } 
} 

下面是我试图插入自定义视图中的XML文件中的相关部分。该<缺少,因为堆栈溢出这里不喜欢他们。

RelativeLayout 
    android:id="@+id/maze" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="7dp" 
    android:layout_marginRight="15dp" 
    android:layout_weight="2" > 
RelativeLayout 

下面是MazeView构造并应显示迷宫。

public class MazeView extends View 
{ 
private Activity context; 
private Level level; 
private boolean mazeIsPath[][] = null; 

private Integer widthInPixels = 0, heightInPixels = 0; 
private Integer cellWidthInPixels = 0, cellHeightInPixels = 0; 
private Paint cell, ball, background, target; 

public MazeView(Context context, Level level, boolean[][] maze) 
{ 
    super(context); 
    this.context = (Activity) context; 
    this.level = level; 
    mazeIsPath = maze; 

    cell = new Paint(); 
    cell.setColor(getResources().getColor(R.color.textColor)); 

    ball = new Paint(); 
    ball.setColor(getResources().getColor(R.color.splashBackground)); 

    background = new Paint(); 
    background.setColor(getResources().getColor(R.color.gameInterfaceBackground)); 

    target = new Paint(); 
    target.setColor(getResources().getColor(R.color.buttonBackground)); 

    widthInPixels = this.getWidth(); 
    heightInPixels = this.getHeight(); 

    setFocusable(true); 
    this.setFocusableInTouchMode(true); 
} 

protected void onSizeChanged(Integer newW, Integer newH, Integer oldW, Integer oldH) 
{ 
    widthInPixels = newW; 
    heightInPixels = newH; 

    super.onSizeChanged(newW, newH, oldW, oldH); 
} 

protected void onDraw(Canvas canvas) 
{ 
    Integer counterX = 0, counterY = 0; 

    canvas.drawRect(0, 0, widthInPixels, heightInPixels, background); 

    for(counterX = 0; counterX < level.getWidth(); counterX++) 
    { 
     for(counterY = 0; counterY < level.getHeight(); counterY++) 
     { 
      if(mazeIsPath[counterX][counterY] != true) 
      { 
       canvas.drawRect((float)(cellWidthInPixels * counterX), (float)(cellHeightInPixels * counterX), (float)(cellWidthInPixels * (counterX + 1)), (float)(cellHeightInPixels * (counterY + 1)), cell); 
      } 
     } 
    } 

    canvas.drawCircle((float)((cellWidthInPixels * level.getCurrentX()) + (0.5f*(cellWidthInPixels))), (float)((cellHeightInPixels * level.getCurrentX()) + (0.5f*(cellHeightInPixels))), (float)(cellHeightInPixels * (2f/3f)), ball); 
    canvas.drawCircle((float)((cellWidthInPixels * level.getFinalX()) + (0.5f*(cellWidthInPixels))), (float)((cellHeightInPixels * level.getFinalX()) + (0.5f*(cellHeightInPixels))), (float)(cellHeightInPixels * (4f/5f)), target); 
} 
} 

我想知道是否有人能指出我出错的地方以及如何解决它!


第一修正案的onDraw():

protected void onDraw(Canvas canvas) 
{ 
    Integer counterX = 0, counterY = 0; 
    widthInPixels = new Integer(canvas.getWidth()); 
    heightInPixels = new Integer(canvas.getHeight()); 

    canvas.drawRect(0, 0, widthInPixels, heightInPixels, background); 

    for(counterX = 0; counterX < level.getWidth(); counterX++) 
    { 
     for(counterY = 0; counterY < level.getHeight(); counterY++) 
     { 
      if(mazeIsPath[counterX][counterY] != true) 
      { 
       canvas.drawRect((float)(cellWidthInPixels * counterX), (float)(cellHeightInPixels * counterX), (float)(cellWidthInPixels * (counterX + 1)), (float)(cellHeightInPixels * (counterY + 1)), cell); 
      } 
     } 
    } 

    canvas.drawCircle((float)((cellWidthInPixels * level.getCurrentX()) + (0.5f*(cellWidthInPixels))), (float)((cellHeightInPixels * level.getCurrentX()) + (0.5f*(cellHeightInPixels))), (float)(cellHeightInPixels * (2f/3f)), ball); 
    canvas.drawCircle((float)((cellWidthInPixels * level.getFinalX()) + (0.5f*(cellWidthInPixels))), (float)((cellHeightInPixels * level.getFinalX()) + (0.5f*(cellHeightInPixels))), (float)(cellHeightInPixels * (4f/5f)), target); 

    Log.w("onDraw width", widthInPixels.toString()); 
    Log.w("onDraw height", heightInPixels.toString()); 
    Integer cWidth = new Integer(canvas.getWidth()); 
    Integer cHeight = new Integer(canvas.getHeight()); 
    Log.w("canvas width", cWidth.toString()); 
    Log.w("cavas height", cHeight.toString()); 
} 
+0

只是一个快速的猜测...打印heightinpixels的值,widthinpixels中的onDraw VS canvas.getWidth和canvas.getHeight ..也..这个白色你说,是第一个drawrect的背景吗?另外..你在哪里初始化cellwidthinpixels? – rupps

+0

感谢您的回复 - 我尝试将背景更改为灰色,但仍未显示! 另外widthInpixels和heightInPixels都是0,canvas的宽度是739,canvas高度是670.我改变了onDraw()方法的代码,如上所述,没有任何效果 - 仍然没有出现! –

回答

0

我说,算了一下widthInPixels & heightInPixels因为它们所依赖的布局。要使用它们,你必须实现onMeasure(这可能是有趣的)。但是,由于您的观点非常简单,只需使用canvas.getWidth()getHeight()中的值即可。尝试以下,看看它的工作原理

private static final int CELLS_PER_SCREEN = 5; // the number of cells that fit on a view 

protected void onDraw(Canvas canvas) { 

    int totalWidth=canvas.getWidth(), 
     totalHeight=canvas.getHeight(), 
     cellWidthInPixels=totalWidth/CELLS_PER_SCREEN, 
     cellHeightInPixels=totalHeight/CELLS_PER_SCREEN; 

    canvas.drawRect(0, 0, totalWidth, totalHeight, background); 

    for (int counterX = 0, xlen=level.getWidth(); counterX < xlen; counterX++) { 
     for (int counterY = 0, ylen=level.getHeight(); counterY < ylen; counterY++) { 
      if (!mazeIsPath[counterX][counterY]) { 
       canvas.drawRect(
        (float)(cellWidthInPixels * counterX), 
        (float)(cellHeightInPixels * counterX), 
        (float)(cellWidthInPixels * (counterX + 1)), 
        (float)(cellHeightInPixels * (counterY + 1)), cell 
       ); 
      } 
     } 
    } 

    canvas.drawCircle((float)((cellWidthInPixels * level.getCurrentX()) + (0.5f*(cellWidthInPixels))), (float)((cellHeightInPixels * level.getCurrentX()) + (0.5f*(cellHeightInPixels))), (float)(cellHeightInPixels * (2f/3f)), ball); 
    canvas.drawCircle((float)((cellWidthInPixels * level.getFinalX()) + (0.5f*(cellWidthInPixels))), (float)((cellHeightInPixels * level.getFinalX()) + (0.5f*(cellHeightInPixels))), (float)(cellHeightInPixels * (4f/5f)), target); 

    Log.w("onDraw width", widthInPixels.toString()); 
    Log.w("onDraw height", heightInPixels.toString()); 
    Integer cWidth = new Integer(canvas.getWidth()); 
    Integer cHeight = new Integer(canvas.getHeight()); 
    Log.w("canvas width", cWidth.toString()); 
    Log.w("cavas height", cHeight.toString()); 
} 

也有一些建议供的onDraw:OnDraw中被频繁调用,以尽可能优化是很重要的。

  • 使用int而不是整数,Integer是一个对象,而int只是一个快速原始类型。
  • 预先计算的东西容易重复使用,特别是浮点乘法。你drawCircles()做了很多数学,只有拥有cellWidthInPixels改变......所以计算它们一次,每次cellWidthInPixels改变
+0

我必须说,你是一个传奇! :D非常感谢你现在的作品 - 把我花了2天的时间来解决这个问题...... –

+0

:D cool!祝你好运,看起来有趣! – rupps