简体   繁体   中英

Ondraw method is not being called

I have my main class like so:

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;
}

}

my ondraw method is inside table and it looks like this:

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);
}   
}

When this is run nothing is being Draw. I want to be able to draw this over and over as i change what should be drawn if there is any other way to do this that does not use a none callable class.

Seems like the table View hasn't been added to anything that's being drawn. Try something like this in your main activity's onCreate:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM