没有调用Ondraw方法

时间:2013-06-14 22:04:55

标签: java android eclipse ondraw

我的主要课程是这样的:

package projects.game.spinners;
import java.util.ArrayList;
import java.util.Collections;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.Menu;

public class MainActivity extends Activity
{

public Set set;
public Deal deal= new Deal();
public Player Name = new Player();
public computer comp1= new computer();
public computer comp2= new computer();
public computer comp3= new computer();
private Table table;
public Canvas canvas;

//public ArrayList<Domino> deck = set.getSet();
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    set = new Set(this);
    table = new Table(this);
    ArrayList<Domino> deck = set.getSet();
    canvas = new Canvas();

    Collections.shuffle(deck);
    //deal.deal(Name, comp1, comp2, comp3, deck);
    //deck = deal.getDeck();
    table.playDomino(deck.get(0), 0, 0);
    table.invalidate();

    //table.onDraw(this);
    //Bitmap dsd = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

我的ondraw方法在table中,它看起来像这样:

package projects.game.spinners;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;
import android.widget.Toast;

public class Table extends View 
{
ArrayList<Position> table;
Context ctx;
Bitmap draw;

public Table(Context context)
{
    super(context);
    ctx=context;
    table=new ArrayList<Position>();
    draw = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
    this.setWillNotDraw(false);

}

public int getPossiblePlays()
{
    return 0;
}

public void playDomino(Domino domino, int x , int y)
{
    table.add(new Position(domino,x,y));

}

@Override
public void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    Toast.makeText(ctx,"Tag Already Done", Toast.LENGTH_LONG).show();
    canvas.drawBitmap(draw, 0,0, null);
    canvas.drawBitmap(table.get(0).getDomino().getImage(), 0,0, null);
}   
}

当这个运行时,没有什么是Draw。我希望能够一遍又一遍地绘制这个,因为我改变应该绘制的内容,如果有任何其他方法可以做到这一点,不使用无可调用的类。

1 个答案:

答案 0 :(得分:0)

似乎表格尚未添加到正在绘制的任何内容中。在主要活动的onCreate中尝试这样的事情:

table = new Table(this);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.IdOfYourMainLayoutFromYourManifest);
layout.addView(table);