Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8298191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:40:51+00:00 2026-06-08T15:40:51+00:00

I try to display one from list of bitmaps during onDraw. When i’m passing

  • 0

I try to display one from list of bitmaps during onDraw.
When i’m passing list to the canvas all are display and stay in their places.
When I pass one random bitmaps it’s redrawing canvas all the time.
All works when i’m using public void drawEnemy(Canvas canvas) but not exactly like I want when using public void drawEn(Canvas canvas).
I want to display one random bitmap, then after a few seconds, delete it and display other bitmap. I think the problem is how I implemented onDrow() method. It’s redrawing canvas all the time.

Activity:

public class NewGameActivity extends Activity{
NewGame newgame;
public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
        // Landscape mode
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        // no title
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // content Newgame.java
        newgame = new NewGame(this);
        setContentView(newgame);
 }

Thread:

public class MainThread extends Thread{

private SurfaceHolder surfaceHolder;
private NewGame screen;

public MainThread(SurfaceHolder surfaceHolder, NewGame ekran) {
    super();
    this.surfaceHolder = surfaceHolder;
    this.screen= screen;
}

private boolean running;
public void setRunning(boolean running) {
    this.running = running;
}

@Override
public void run() {
    Canvas canvas;
    while (running) {
        canvas = null;
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                this.screen.onDraw(canvas);
            }
        } finally {
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }

}

}

SurfaceView:

public class NewGame extends SurfaceView implements SurfaceHolder.Callback{

private MainThread thread;
private EnemyManager manager;

public NewGame(Context context) {
    super(context);
     getHolder().addCallback(this);
     thread = new MainThread(getHolder(), this);
     manager = new EnemyManager();
    // TODO Auto-generated constructor stub
//adding enemy
     Enemy e1 = new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.card), 1);
     Enemy e2 = new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.horse), 2);
     EnemyLocation l1 = new EnemyLocation(60, 180);
     EnemyLocation l2 = new EnemyLocation(60, 50);
     manager.AddEnemy(e1, l1);
     manager.AddEnemy(e2, l2);
     setFocusable(true);
}

@Override
protected void onDraw(Canvas canvas) {
     canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.saloon), 0, 0, null);
        manager.drawEn(canvas);

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    thread.setRunning(true);
    thread.start();
}


@Override
public void surfaceDestroyed(SurfaceHolder holder) {
     thread.setRunning(false);
     thread.stop();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        manager.handleActionDown((int)event.getX(), (int)event.getY());
    }
    return true;
}

}

EnemyManager:

public class EnemyManager {
private ArrayList<Enemy> enemyList;
private ArrayList<Enemy> suspects;
private Enemy cow;
private String message;
private int suspectID;
private Random rnd;


public String getMessage() {
    return message;
}

public EnemyManager(){
    enemyList = new ArrayList<Enemy>();
    suspects = new ArrayList<Enemy>();
}


public void AddEnemy(Enemy enemy, EnemyLocation loc){
    // set x,y enemy localization
    enemy.setX(loc.getX());
    enemy.setY(loc.getY());
    enemyList.add(enemy);
}

public void clearEnemy() {
    enemyList.clear();
}

// message if enemy touched
public void handleActionDown(int x, int y) {
    for (Enemy enemy: enemyList) {
        if (enemy.wasTouched(x, y)) {
            message = enemy.getId();
            return;
        }
    }
}

public void PrepareEnemy(){
    suspectID = enemyList.get(rnd.nextInt(enemyList.size()+1)).getId();
    suspects = new ArrayList<Enemy>();
    suspects.add(getSuspectByID(suspectID));
}

private Enemy SingleEnemy(){
    Double i = 1 + Math.random() * ((enemyList.size()-1)+1);
    cow = getSuspectByID(i.intValue());
    return cow;
}

private Enemy getSuspectByID(int suspectID) {
    for (Enemy s: enemyList) {
        if (s.getId() == suspectID) {
            return s;
        }
    }
    return null;
}


public void drawEn(Canvas canvas){
    try {
        Enemy k = SingleEnemy();
        canvas.drawBitmap(cow.picture, cow.x, cow.y, null);
    } catch (Exception e) {
        // TODO: handle exception
    }
}

// draw enemy
public void drawEnemy(Canvas canvas) {
    try {
        for (Enemy enemy: enemyList) {
            canvas.drawBitmap(enemy.picture, enemy.x, enemy.y, null);
        }
    } catch (Exception e) {
        // TODO: handle exception
    }

}

}

das

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:40:54+00:00Added an answer on June 8, 2026 at 3:40 pm

    As for as understand you are trying to do something like this (if it’s not, please correct me):
    This is rendering the canvas with all components:

    • Draw background
    • Draw enemy

    To “refresh” the canvas you simply do something like this:

    • Draw background

    Update

    To pause the rendering you could do something like this:

    int lastUpdateTime;
    int delayTime = 2000; 2 seconds
    
    if(System.currenttimeMillis() > lastUpdateTime + delayTime) {
    // Finished waiting
    }
    

    You should only define lastUpdateTime when you want to wait and not in every iteration.

    NB: Don’t call Thread.sleep() in a rendering thread!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to get one data column from a MS-Access table and display it
I'm try display data from column points , SELECT * from table ORDER by
Can any one tell me how to retrieve image from RSS feed and display
I'm trying to select a value from a textbox that display a list of
I am using the following code to try and display in a time in
I am a total beginner to Objective C. Currently I try to display a
I would like to display an ajax loader icon when user try to submit
Is it possible to display images next to items in FlyoutAnchor submenu? I try
So I have a 1920x1080 display, and when I try to run two xcode
Since yesterday, I try to run a sample of code to display a chart

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.